LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware
User Name
Password
Slackware This Forum is for the discussion of Slackware Linux.

Notices



Reply
 
Search this Thread
Old 12-08-2007, 10:21 PM   #1
Furlinastis
Member
 
Registered: Dec 2004
Location: Ball of Confusion
Distribution: Slackware,Bluewhite64
Posts: 252

Rep: Reputation: 30
comment multiple lines in vim


I am editing xorg.conf and have a few sections that I would like to comment, but typing i then x then <escape> then j then h then i, etc is annoying. Especially when I want to comment sections that are 50 lines in a row but I don't want to delete them as I may want to use them in the future. How would I stick a comment at the beginning of 'x' number of lines in vim?
 
Old 12-08-2007, 10:26 PM   #2
agentc0re
Member
 
Registered: Apr 2007
Location: SLC, UTAH
Distribution: Slackware
Posts: 200

Rep: Reputation: 32
Quote:
Originally Posted by Furlinastis View Post
I am editing xorg.conf and have a few sections that I would like to comment, but typing i then x then <escape> then j then h then i, etc is annoying. Especially when I want to comment sections that are 50 lines in a row but I don't want to delete them as I may want to use them in the future. How would I stick a comment at the beginning of 'x' number of lines in vim?
LOL i just did a quick google search for vi comment multiple lines.
http://www.linuxweblog.com/comment-multiple-lines
Code:
vi comment multipul lines
remember, GIYF
 
Old 12-08-2007, 10:35 PM   #3
Furlinastis
Member
 
Registered: Dec 2004
Location: Ball of Confusion
Distribution: Slackware,Bluewhite64
Posts: 252

Original Poster
Rep: Reputation: 30
Hmm.. That's weird. I searched and I didn't get anything?

Anyway, thanks
 
Old 12-09-2007, 12:08 AM   #4
raconteur
Member
 
Registered: Dec 2007
Location: Slightly left of center
Distribution: slackware
Posts: 276
Blog Entries: 2

Rep: Reputation: 44
:x,y s/^/# /

The colon is command mode, obviously
x and y is a range of line numbers you want to affect
s is a sed substitution, the caret (^) means the beginning of the line
The sharp (#) is the comment character, again obviously
I put a space after the comment, but it isn't necessary, substitute any string that suits you.
 
Old 12-09-2007, 07:16 AM   #5
dive
Senior Member
 
Registered: Aug 2003
Location: UK
Distribution: Slackware
Posts: 3,211

Rep: Reputation: 293Reputation: 293Reputation: 293
Another way to do do this is to visually select the lines you want to comment with shift-v and up and down arrows, then type

:s/^/# /

Easier for blocks of fewer lines.

NB: When you press : vim will add '<,'> to the command line so the final command is:

:'<,'>s/^/# /

Last edited by dive; 12-09-2007 at 07:21 AM.
 
Old 12-09-2007, 07:36 PM   #6
bioe007
Member
 
Registered: Apr 2006
Location: lynnwood, wa - usa
Distribution: archlinux
Posts: 654

Rep: Reputation: 30
you don't have to type all this tons of stuff, just bind the substitution in your ~/.vimrc

Code:
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"       bindings
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
    ",v brings up my .vimrc
map ,v :sp $HOME/.vimrc<CR><C-W>_

    ",V reloads it -- making all changes active (have to save first)
map <silent> ,V :source $HOME/.vimrc<CR>:filetype detect<CR>:exe ":echo 'vimrc reloaded'"<CR>

    " comments
    " bash
noremap <silent> ,# :call CommentLineToEnd('# ')<CR>+
    " or c
noremap <silent> ,* :call CommentLinePincer('/* ', ' */')<CR>+
noremap <silent> ,/ :call CommentLineToEnd('// ')<CR>+
    " or vimrc
noremap <silent> ," :call CommentLineToEnd('" ')<CR>+

    " Make shift-insert work like in Xterm
map <S-Insert> <MiddleMouse>
map! <S-Insert> <MiddleMouse>
so for me, I can select lines and `,#` for bash, or whatever for c/c++

you should check out: http://www.vim.org/tips/index.php

there is tons of useful stuff there.
 
Old 12-09-2007, 08:03 PM   #7
BCarey
Senior Member
 
Registered: Oct 2005
Location: New Mexico
Distribution: Slackware
Posts: 1,490

Rep: Reputation: Disabled
...and to see the line numbers (for x and y) type ":set nu"

Brian
 
Old 12-10-2007, 07:54 AM   #8
dive
Senior Member
 
Registered: Aug 2003
Location: UK
Distribution: Slackware
Posts: 3,211

Rep: Reputation: 293Reputation: 293Reputation: 293
Or just bind F2 to 'set number!' which toggles on/off.
 
Old 12-11-2007, 01:50 PM   #9
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 70
If you want to get more fancy, you could use external text filters such as boxes or indent. Usually this is for things like C source files instead of xorg.conf files.

For example, if you type out this code:
Code:
This is the program header that explains some stuff about the program
itself and may have a long copyright clause in addition. Blah, blah,
blah. Blah, blah, blah, blah.

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.

This software is dedicated to my dear, sweet mother.

#include <stdio.h>
int main() { printf("Real code begins on line 20.\n"); return 0;}
And this type into ex:
Code:
:1,18!boxes
The result looks all pretty like so:
Code:
/*************************************************************************/
/* This is the program header that explains some stuff about the program */
/* itself and may have a long copyright clause in addition. Blah, blah,  */
/* blah. Blah, blah, blah, blah.                                         */
/*                                                                       */
/* This program is free software: you can redistribute it and/or modify  */
/* it under the terms of the GNU General Public License as published by  */
/* the Free Software Foundation, either version 3 of the License, or     */
/* (at your option) any later version.                                   */
/*                                                                       */
/* This program is distributed in the hope that it will be useful,       */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of        */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         */
/* GNU General Public License for more details.                          */
/*                                                                       */
/* You should have received a copy of the GNU General Public License     */
/* along with this program.  If not, see <http://www.gnu.org/licenses/>. */
/*                                                                       */
/* This software is dedicated to my dear, sweet mother.                  */
/*************************************************************************/

#include <stdio.h>
int main() { printf("Real code begins on line 20.\n"); return 0;}
If you happen to be using vim (and not plain vi/ex), you could use something like this script in stead/addition.
 
Old 12-11-2007, 02:01 PM   #10
raconteur
Member
 
Registered: Dec 2007
Location: Slightly left of center
Distribution: slackware
Posts: 276
Blog Entries: 2

Rep: Reputation: 44
Tee hee... when I posted in this thread, I knew it would grow quickly.

I love vi, my .exrc is pretty huge (I won't give the exact amount of lines, or post all of the aliases and functions, that would just feed the fire ) ... there are as many ways to do <insert action here> as there are people to do them.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
listing non-comment lines scbops Linux - Newbie 5 09-18-2007 09:55 PM
AWK/SED Multiple pattern matching over multiple lines issue GigerMalmensteen Programming 15 12-03-2006 06:08 PM
comment the lines in virtuertable will receive old emails in SquirrelMail ? kkeith Linux - Newbie 0 07-10-2006 06:37 AM
awk/gawk/sed - read lines from file1, comment out or delete matching lines in file2 rascal84 Linux - General 1 05-24-2006 10:19 AM
Is there a way to filter out all the comment lines in a configuration file... Akhran Linux - Newbie 3 12-07-2005 12:07 PM


All times are GMT -5. The time now is 02:49 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration