LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 09-12-2006, 08:43 AM   #1
noir911
Member
 
Registered: Apr 2004
Posts: 682

Rep: Reputation: Disabled
vim add # automatically


While working on vim I sometimes need to comment out (#) lines from, say, 4 to 20. It is very time consuming to add # in front of each line manually. Is there any way to automate the process where I can ask vim to add # in front of line, say, 2~20?
 
Old 09-12-2006, 08:55 AM   #2
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,826
Blog Entries: 15

Rep: Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668
In vim from command mode (as opposed to insert mode) type:

:2,20 s/^/#/

The above uses ex syntax. 2,20 means line 2 through 20. s means substitute /^ means look for the beginning of lines the /# means replace the beginning of the line (NOT the first character) with #. The final / terminates the substitution.
 
Old 09-12-2006, 08:56 AM   #3
noir911
Member
 
Registered: Apr 2004
Posts: 682

Original Poster
Rep: Reputation: Disabled
I found a solution to this problem: highlight the text using Ctrl-v and then I#<ESC> to insert # in front of each line.

Anyone else is using anything far better than this one?
 
Old 09-12-2006, 09:01 AM   #4
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981
try this vi command
Code:
:2,20s/^/#/
ack... spent too long playing... beaten to it.

you could easily wrap this in a macro i guess too

Last edited by acid_kewpie; 09-12-2006 at 09:03 AM.
 
Old 09-12-2006, 12:01 PM   #5
sleepyEDB
Member
 
Registered: Dec 2005
Location: /USA/MI/Detroit/home
Distribution: MEPIS, antiX, RHEL
Posts: 105

Rep: Reputation: 15
Thumbs up

I had this exact same problem yesterday...I'm thrilled to find the answer. Thanks!


sleepy
 
Old 09-12-2006, 12:17 PM   #6
smokybobo
LQ Newbie
 
Registered: Feb 2003
Posts: 29

Rep: Reputation: 15
I like to use the EnhCommentify vim script. With this installed, and in $HOME/.vimrc:

Code:
let g:EnhCommentifyTraditionalMode = 'no'
let g:EnhCommentifyFirstLineMode = 'no'
let g:EnhCommentifyUserBindings = 'no'
let g:EnhCommentifyUserMode = 'yes'
If I want to comment out the line the cursor is on, either in Insert Mode or Normal Mode, I type in \x, if I want to uncomment, \X

To (un)comment out a range, CTRL+v (or SHIFT+v as preferences dictate) in Normal Mode, select a range of lines, and then \x or \X as above.

But if you don't like the defaults, you can also select other modes and other keybindings to customize how to comment out things. Of course, other solutions work, but I like this script as I've found it gets in my way the least.
 
Old 09-13-2006, 09:04 AM   #7
sleepyEDB
Member
 
Registered: Dec 2005
Location: /USA/MI/Detroit/home
Distribution: MEPIS, antiX, RHEL
Posts: 105

Rep: Reputation: 15
Exclamation

smokybobo--

I've installed the EnhCommentify script into my plugin directory, and put the lines you listed in my ~/.vimrc file, but I get the following error when trying to use the script via your '\x' method:

Code:
 This filetype is currently _not_ supported!
Please consider contacting the author in order to add this filetype in future releases!
I get this error when trying to use the script on system files (with no extension) and text files (.txt). I've read throught the help file that came with the script, but everything seems to be in order.

I know you're not the maintaner of this script, but I thought I'd ask here first before contacting them.

Thanks,


sleepy
 
Old 09-13-2006, 11:02 AM   #8
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,826
Blog Entries: 15

Rep: Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668
I can't see how that would be easier than remembering the ex substitution syntax except for a few keystrokes if it is something you need to do frequently. Also if you were on a machine you hadn't created the script on it would be more to remember.

Ah well - each to his own.
 
Old 09-13-2006, 12:14 PM   #9
haertig
Senior Member
 
Registered: Nov 2004
Distribution: Debian, Ubuntu, LinuxMint, Slackware, SysrescueCD, Raspbian, Arch
Posts: 2,283

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
Usually I use marks and the substitute command:
Code:
(1) Move to the first line you want to comment out.  Type ma.  This creates a mark named "a".
(2) Move to the last line you want to comment out.  Type mb to create a mark named "b".
(3) Type :'a,'bs/^/#/
    (this says "from mark a to mark b, insert a # at the front of the line")
 
Old 09-13-2006, 12:21 PM   #10
haertig
Senior Member
 
Registered: Nov 2004
Distribution: Debian, Ubuntu, LinuxMint, Slackware, SysrescueCD, Raspbian, Arch
Posts: 2,283

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
Quote:
Originally Posted by haertig
Usually I use marks and the substitute command:
p.s. - Marks are useful in other situations as well. In a large file I'll be at some line that I know I'll want to come back to later. I type ma to mark it. Then I'll scroll all over the file to see whatever, and when I want to go back to that place I type 'a to move back to my mark. You can set many marks. Just name each one differently. If you use "ma" in two different places in the file, the second mark will just override the first.
 
Old 09-13-2006, 09:57 PM   #11
smokybobo
LQ Newbie
 
Registered: Feb 2003
Posts: 29

Rep: Reputation: 15
Quote:
Originally Posted by sleepyEDB
smokybobo--

I get this error when trying to use the script on system files (with no extension) and text files (.txt). I've read throught the help file that came with the script, but everything seems to be in order.
Yup, this is a limitation of the script; if it does not know what filetype a text file is it will not know what commenting scheme to use. For me though, as I program in several different programming/markup languages, it's indispensible though admittedly useless for things like configuration files.

As mentioned by acid_kewpie, if you don't mind writing new macros for every kind of comment style you would come across, you can implement something akin to the following in your $HOME/.vimrc which will stick a '# ' in front of the current line or a range of selected lines:

Code:
map!  ]#  <Esc>^i# <Esc>
vmap ]#  :s/^/# /<Enter>
map ]@  <Esc>:s/^# \?// <Esc>
map! ]@  <Esc>:s/^# \?// <Esc>
vmap ]@  :s/^# \?//<Enter>
In Insert Mode, ]# will stick '# ' in front of the current line. In Visual Mode, the same will do it for the lines that are selected. In Insert of Normal mode, ]@ will remove '#' from the front of the line, and the same for a range of lines in visual mode.

Last edited by smokybobo; 09-13-2006 at 10:09 PM.
 
Old 09-13-2006, 10:56 PM   #12
sleepyEDB
Member
 
Registered: Dec 2005
Location: /USA/MI/Detroit/home
Distribution: MEPIS, antiX, RHEL
Posts: 105

Rep: Reputation: 15
Thanks for the reply, smokybobo. Unfortunately, you and I use vim for different purposes. I don't program in any way, and use vim solely for editing configuration files, so I'll have to look into another method.

The aforementioned substitution method seems to work fine.


sleepy
 
  


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
add automatically icon for usb abd_bela Debian 2 01-29-2006 10:23 AM
Automatically mounting cds I add in a cd drive? 0mn1z10n Debian 5 10-29-2005 01:58 PM
automatically add route on boot? Loz Linux - Networking 1 11-22-2004 11:56 PM
Gnome 2.6: Automatically add games? technopasta Linux - Games 1 04-21-2004 08:35 PM
Script thats add users automatically embalmedlenin *BSD 1 09-14-2003 04:34 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 10:40 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration