17

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?

serenesat
  • 4,611
  • 10
  • 37
  • 53
Open the way
  • 26,225
  • 51
  • 142
  • 196
  • 1
    The difference between vi and vim is discussed here: http://stackoverflow.com/questions/1159206/difference-between-vi-vim Just remember, these days (unless you're working on a crazy old platform) you have vim, not vi. Vim is usually installed with vi aliased to vim, and the `compatible` option makes it "vi compatible", which basically means turning off all the good features added in vim. – Cascabel Mar 17 '10 at 14:24
  • 1
    More on indenting lines in Vim here: http://stackoverflow.com/questions/2332340 – Brian Rasmussen Mar 17 '10 at 20:16

11 Answers11

41

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

isomorphismes
  • 8,233
  • 9
  • 59
  • 70
Sungguk Lim
  • 6,109
  • 7
  • 43
  • 63
  • 2
    Never mind I found it.. `ctrl+v` --> `l` --> `d` If there's a better I'd like to know. –  Aug 24 '12 at 08:03
  • 4
    When i press I (i capital) it go in insert mode on the first line of the bloc only. – Syl Sep 23 '15 at 12:38
  • 1
    And then you click 'ESC' and the character of the first line is copied to all the block. – xbello Oct 21 '16 at 10:50
  • @Syl make sure you're entering VISUAL BLOCK mode (using control+v) instead of VISUAL LINE (shift+v) or VISUAL (v). After you press escape the changes should be applied on every line. – Ali Jun 29 '18 at 20:33
15

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.

isomorphismes
  • 8,233
  • 9
  • 59
  • 70
Juliano
  • 39,173
  • 13
  • 67
  • 73
  • 2
    Btw, to add `//` instead of `#` in the beginning of the line, you just need to replace the `#` by `\/\/` like this `:'<,'>s/^/\/\//` after you did `Ctrl + v` (or `V`) and `:` on the lines you want to comment block. – Armfoot Jul 23 '15 at 14:39
6

type :set number. take note of the start and end line number of the block you want to comment. then do an address range substitution, eg

:12,17s/^/#

ghostdog74
  • 327,991
  • 56
  • 259
  • 343
  • Or use 'V' in normal mode to visual select one line, use 'j/k' to select multiple lines, and use ':<,>s/^/#' in ex mode. EDIT: Oops, just saw this is already mentioned in an earlier answer. – Arun M Mar 17 '10 at 14:39
  • This was the only solution I could get to work in Ubuntu 16.04. The ctrl+v options would all fail after the shift i. – hookenz Jul 20 '17 at 04:23
3

Lots of answers here, all with a theme. The best way to do it really depends on context (because context determines the easiest navigation method), so I'll make some assumptions about the context. If the section you want to indent or comment is a single paragraph (eg, you want to indent or comment everything from the cursor up to the next blank line), you can indent with:

>)

If the cursor is not on the start of the paragraph, but the section you want to indent is a single paragraph and the cursor is in the middle, use

>ip

Finally, suppose you want to indent a block of code delimited by {}, and the cursor is in the middle of that block. Use

>i{

To comment, in each case just replace the > with v and use the above commands to make a block selection, then do a text replace like s/^/#/.

The key is the navigation commands. I highly recommend

:help v_a
William Pursell
  • 204,365
  • 48
  • 270
  • 300
3

Similar to the accepted answer, but easier for blocks or paragraphs:

  1. Block lines: Ctrl + V
  2. Select paragraph: }
  3. Insert mode: I (uppercase i)
  4. Type character to insert: # (with space after char, no Enter!)
  5. Press: ESC

This should auto complete the character in all the selected block. Basically the diference with the accepted answer is that instead of using j to go down line by line you use } to select the whole paragraph (or you could use G for end of file, for ex.

Short version:

Ctrl + V + } + I + # + ESC

Leo Gallego
  • 372
  • 2
  • 11
2

I know there are a zillion answers here already explaining how to use > and < for indentation, so I'm not going to bother with that. With respect to the commenting, though, while you can do it quick and dirty with a block insert or a substitution, you can do way better with the NERD Commenter plugin. It provides commands to comment and uncomment in various ways, it knows what comment symbol to insert based on the syntax, and it can do pretty multi-line comments if the language supports them.

Cascabel
  • 479,068
  • 72
  • 370
  • 318
1

Select the lines using visual mode.

  • To indent once type >> or << to indent right or left respectively. To indent n times type n>> or n<<.

  • To comment out do replace the beginning of the line with the comment:

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

'<,'> means "from the beginning of the selection until the end.

s/^/#/ replaces the beginning of each line in the range with #, assuming # makes a line into a comment.

Nathan Fellman
  • 122,701
  • 101
  • 260
  • 319
0

Put your cursor on the first line, count how many lines should be indented, in the above example it's 5, then for hash (#) type :.,.+5%s/^\([ <tab>]*\)/#\1/<enter> or for a tab indentation, :.,.+5%s/^\([ <tab>]*\)/<tab>\1/<enter>, <tab> and <enter> are the tab and enter keys.

There are probably more elegant ways of doing this, but something like this is a quick-n-dirty thing.

Ed Griebel
  • 631
  • 9
  • 22
0

For commenting you could use VISUAL BLOCK selection (Ctrl-V) and select the beginnings of the lines, then press Shift-I and write one #. After pressing Esc all the lines get the #.

mkj
  • 2,761
  • 5
  • 24
  • 28
0

My usual solution is:

<ESC>
<q><a> => start a macro and save it as macro a
<^> => to get to the start of the line
<i> => insert mode
<#> => Add the #
<ESC> => End insert mode
<down> => Move to the next line
<q> => End macro

Then once:

<[at]><a> => repeat macro a

Then just repeat <[at]><[at]> (repeat last executed macro) until all lines are commented. You can just hold <[at]> and let keyboard repeat do the rest.

BTW: How do you write an [at] sign here without stackoverflow turning it into "> blockquote"?

To indent:

[shift] + [v] => line select mode
[down] => until all lines to indent are selected

then:

[>] => indent once

or:

[2..x][>] => indent 2..x levels
0

If you are using Python (or other languages that use # as comment), a faster way to comment multiple lines would be:

  1. Enter visual block mode (Ctrl+v) from the start of the line.
  2. Go down as needed (j multiple times).
  3. Replace space with # by pressing r then #.

To uncomment, do the same but for step three replace it with space.

cakraww
  • 2,493
  • 28
  • 30