LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   vim add # automatically (https://www.linuxquestions.org/questions/linux-software-2/vim-add-automatically-482733/)

noir911 09-12-2006 08:43 AM

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?

MensaWater 09-12-2006 08:55 AM

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.

noir911 09-12-2006 08:56 AM

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?

acid_kewpie 09-12-2006 09:01 AM

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

sleepyEDB 09-12-2006 12:01 PM

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


sleepy

smokybobo 09-12-2006 12:17 PM

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.

sleepyEDB 09-13-2006 09:04 AM

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

MensaWater 09-13-2006 11:02 AM

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.

haertig 09-13-2006 12:14 PM

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")


haertig 09-13-2006 12:21 PM

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.

smokybobo 09-13-2006 09:57 PM

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.

sleepyEDB 09-13-2006 10:56 PM

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


All times are GMT -5. The time now is 12:58 PM.