Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have learned from this Stack Overflow question that it is possible to use vi/vim to comment out a specified range of line numbers. For example, suppose I have the following bash script:

#!/bin/bash

This
is
my
very
very
great
script

Now suppose that I want to comment out line numbers 6 through 8 (which contain the words very, very, and great) using the # comment character. In vi/vim, I can simply type :6,8s/^/# to obtain the following:

#!/bin/bash

This
is
my
#very
#very
#great
script

which comments out lines 6 through 8.

My question is, is it possible to type a similar one liner that will remove the # comment character from lines 6 through 8 (but not any other commented lines in the file)?

Having said this, I realize that there is some debate about whether I am actually using vi or vim. In practice, I open a file script.sh with the command vi script.sh. Also, when I type the command which vi, I obtain /usr/bin/vi. Nevertheless, when I simply type vi and press Enter, I obtain this:

~                              VIM - Vi IMproved
~
~                               version 7.2.330
~                           by Bram Moolenaar et al.
~                 Vim is open source and freely distributable
~
~                           Sponsor Vim development!
~                type  :help sponsor<Enter>    for information
~
~                type  :q<Enter>               to exit
~                type  :help<Enter>  or  <F1>  for on-line help
~                type  :help version7<Enter>   for version info

which seems to suggest that I'm actually using vim. I am accessing a remote Ubuntu Linux cluster using SSH from my PC. I am not using a Ubuntu Linux GUI.

share|improve this question

7 Answers 7

up vote 13 down vote accepted

You can use:

:6,8s/^#//

But much easier is to use Block Visual selection mode: Go to beginning of line 6, press Ctrl-v, go down to line 8 and press x.

There is also "The NERD Commenter" plugin.

share|improve this answer
2  
NERD Commenter is the way to go here in my opinion! +1 for that –  user1146332 Jul 30 '13 at 16:46

vi is a symbolic link to vim in most GNU/Linux distribution so you're indeed using vim when you type vi.

To remove the comments, you can type: :6,8s/^#// or :6,8s/^\s*#// to discard some leading space before the # symbol.

share|improve this answer
1  
Thanks so much. It seems that there may be typos. Maybe it should be :6,8s/^#// and 6,8s/^\s*#//? –  Andrew Jul 30 '13 at 16:06

I know your question specifies using vi or vim but here are a few other options for doing this without having to manually open the file:

  • Perl

    perl -ne 'if($. <=8 && $. >= 6){s/^\s*#//;}print' foo.sh 
    
  • Perl version >=5.10

    perl -ne '$. ~~ [6..8] && s/^\s*#//;print' foo.sh 
    

    This will print out the contents of the file, you can either redirect to another (> new_file.sh) or use i to edit the file in place:

    perl -i -ne '$. ~~ [6..8] && s/^\s*#//;print' foo.sh 
    
  • sed

    sed '6,8 s/^ *#//' foo.sh
    

    Again, to make this edit the original file in place, use i:

    sed -i '6,8 s/^ *#//' foo.sh
    
  • awk/gawk etc:

    gawk '(NR<=8 && NR>= 6){sub("^ *#","")}{print}' foo.sh
    
  • Pure bash:

    c=1; while read line; do 
      if [ $c -ge 6 ] && [ $c -le 8 ]; then 
         echo "${line/\#/}"
      else 
         echo $line 
      fi
      let c++; done < foo.sh
    
share|improve this answer
1  
It's not so much a matter of "having to manually open the file", because usually you decide which lines to comment upon visual inspection, while editing :) But sure, it's a good answer for completeness. –  Paulo Almeida Jul 30 '13 at 17:09
2  
@PauloAlmeida you're right of course. I just thought it could be useful since the OP already knows the line numbers (because of the first command used to comment them) and, in any case, the tools I show can be applied to a variety of problems. –  terdon Jul 30 '13 at 18:02

You are probably using vim.tiny. In any case, you can remove the initial comments with:

:6,8s/^#//

Of course, if you insert them in a different way (for instance, with an extra space), you may need to remove whatever else is there. With full vim, visually selecting columns and inserting/deleting characters is an easier way to do the same thing.

share|improve this answer

Personally my Favourite way is using visual block mode

ctrl + v to enter visual block mode, the use the arrow keys or hjkl to select the lines and press x or del.

Want them back ?

ctrl + v make the selection, then I (capital i) #Esc

share|improve this answer

AFAIK, vi is usually a symlink of vim nowadays (try which vi or type vi and then follow the symlinks). Maybe, even /usr/bin/vi -> /etc/alternatives/vi -> /usr/bin/vim.basic.

Personally, to remove multiple comment lines, I prefer selecting a vertical block via CtrlV and deleting it. If you need to add a comment symbol to multiple lines, you could CtrlV, then ShiftI, type # and Esc, and a comment will be added to multiple lines.

share|improve this answer

The above answers, using

:6,8s/^#//

are perfect solution, but a bit cumbersome to type. This can be simplified by defining new commands in ~/.vimrc.

command -range=% C :<line1>,<line2>s/^/#/
command -range=% D :<line1>,<line2>s/^#//

And you can just type

:6,8C
:6,8D

to place/delete the command.

If you like visual mode, you can define maps

map <F7> :s/^/#/<CR>
map <F8> :s/^#//<CR>

Such that you only have to select a line range in visual mode, and press F7 and F8 to put and remove comments respectively.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.