2

In vi, how to replace or remove the first character from line to line if it is the specified character #.

line 1
...
...
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
#   fastcgi_split_path_info ^(.+\.php)(/.+)$;
#   # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
#
#   # With php5-cgi alone:
#   fastcgi_pass 127.0.0.1:9000;
#   # With php5-fpm:
#   fastcgi_pass unix:/var/run/php5-fpm.sock;
#   fastcgi_index index.php;
#   include fastcgi_params;
#}
...
end of line
Xianlin
  • 1,139
  • 5
  • 20
  • 34

4 Answers4

5

As its vim there are a few ways to do this.

First Solution

gg4 ctrl+v gg16 x

gg4 to move the cursor onto the first character in line 8 (where the comment begins)
ctrl+v to switch into visual block mode, in this mode you can select columns of text
gg16 to move to the last line of the commented block, selecting the entire first column
x deleting everything you have selected

Second Solution

:4,16s/#//
this will simply remove the first occurrence of # in every line between and including 4 and 16. This command is particularly helpfull if your comment-hashes are not the first characters in that line but preceded by spaces.

Third Solution

is a slight modification of the second solution that is useful if you are using relative line numbers (means the lines are not numbered incrementally but indicating the offset to your current cursor position)

:.,.+5s/#//
removes the # for every line between your cursor position and 5 lines under your cursor position

Lazybensch
  • 506
  • 2
  • 5
2

This is your chance to learn How to use macro?.Here are the steps:

1. Place your cursor at start line
2. In normal mode type "q" followed by any letter (name of macro) (say "qa").
3. Now vim has started recording your macro.
4. Go to start of line by pressing "0"
5. Delete character under cursor by pressing "x"
6. Now go to next line by pressing "j".
7. Now press "q" to stop recording.

Now you have a macro named a which goes to start of line, deletes first char in that line and then goes to next line.

You can play macro using "@" followed by name of macro ("@a" in this case). Also you can play last played macro using "@@".

Suppose you want to do this for 20 lines. Run this macro 20 times using 20@a and first character of all those 20 lines will get deleted.

Ashwani
  • 1,938
  • 11
  • 15
1

The way I do which is quite handy is using <ctrl-v> (visual mode) and select the columns you want line by line. Then use x to delete the visually selected characters.

You could do the same to insert characters in the beginning of each line as well.

footbar
  • 11
  • 1
1

What you really want is a commenting plugin like commentary (which I use), Nerd Commenter, EnhCommentify, tComment, ..., etc.

If you want to skip the plugin then you use these quick n' dirty mappings inspired by commentary:

nnoremap <expr> gcc getline('.') =~ '^#' ? '0"_xw' : "gI#\<esc>w"
xnoremap <expr> gc ':norm! ' . (getline("'<") =~ '^#' ? '0"_x' : "gI#") . "\<cr>"
Peter Rincker
  • 43,539
  • 9
  • 74
  • 101