开发者

Indent or comment several text lines with vi

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(flo开发者_开发问答or(1.0*(item-lmin)/width))
      if ind==nintervals:
          ind=ind-1
      print item,ind

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


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


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.


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/^/#


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


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


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.


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.


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.


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 #.


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜