tags:

views:

102

answers:

8

Hi,

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

Thanks

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

A: 

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
+4  A: 

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.

Juliano
Your command will insert a # at the start of the line, not the start of the text.
Ed Griebel
@Ed: done......
Juliano
+1: The only answer to actually use the built-in commands for indentation.
Jefromi
...or at least it was four minutes ago.
Jefromi
A: 

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
A: 

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
dbemerlin
A: 

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
+1  A: 

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
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 Mahapatra
A: 

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.

Jefromi
A: 

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