Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Indent or comment several text lines with vi

Tags:

vim

can vim or vim be used to comment or indent at the same time a number of lines? For instance:

for item in Lista:
    ind = int(floor(1.0*(item-lmin)/width))
    if ind==nintervals:
        ind=ind-1
    print item,ind

comment it to:

#for item in Lista:
    #ind = int(floor(1.0*(item-lmin)/width))
    #if ind==nintervals:
        #ind=ind-1
    #print item,ind

or indent it to:

  for item in Lista:
      ind = int(floor(1.0*(item-lmin)/width))
      if ind==nintervals:
          ind=ind-1
      print item,ind

P.D. Is relevant the difference between VI and VIM?

like image 675
Open the way Avatar asked Mar 17 '10 14:03

Open the way


People also ask

How do I comment a few lines in Vim?

vim-multiline-comment.md For commenting a block of text is almost the same: First, go to the first line you want to comment, press Ctrl``V , and select until the last line. Second, press Shift``I``#``Esc (then give it a second), and it will insert a # character on all selected lines.

How do I indent multiple lines in Linux?

To tab or add the indentation at multiple lines, try “shift+dot” i.e., “.” Shortcut once. You will see it will add an indentation of one character at each selected line from the start. If you want to add indentation without stopping, then you have to try the “.” Key from the keyword after using “shift+.”.


2 Answers

here is another way.

  1. block lines with ctrl+v
  2. Insert comment sign (//) with I
  3. escape with ESC

the key typing is

ctrl+vjjjjI//ESC

like image 94
Sungguk Lim Avatar answered Sep 28 '22 06:09

Sungguk Lim


To comment, press a capital V to enter VISUAL LINE mode, select all lines, then press : to enter command mode and use the command (note that VIM already include the '<,'>marks for you):

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

If you prefer hash marks near the text, and not near the left margin, the command is:

:'<,'>s/^\(\s*\)/\1#/

To indent, select the block the same, then type > to indent, < to unindent.

like image 37
Juliano Avatar answered Sep 28 '22 05:09

Juliano