2

I am new to Vim and I temporarily can't get away from my mouse.

One feature that I am missing from IDEs is that I can select multiple lines with my mouse and press one key to Indent(Tab key) or Comment(<C-M>) them.

The Vim's way as I know (<C-V> ;many j-key strokes;<Shift-I>,//,<ESC> ) seems too many key strokes for me.

Is there a vim script that enables me to Indent and Comment my code block in the IDE's way?

TomCaps
  • 2,497
  • 3
  • 22
  • 25

5 Answers5

2

In C++ code, for instance:

>i{

Will indent a {} block.

See:

Commenting is a bigger beast. It is probably best that you employ a plug-in for this, such as The NERD Commenter.

johnsyweb
  • 136,902
  • 23
  • 188
  • 247
2

Also, there are some ideas for comments here: Easily comment (C++) code in vim

And, using { and } to highlight blocks is a good replacement for "many j-key strokes"

Community
  • 1
  • 1
Chris J
  • 1,375
  • 8
  • 20
1

Enabling mouse support saves several keypresses.

:set mouse=a

Now when you drag your mouse over a selection, it'll drop you into 'Visual Mode'.

  1. From there you can simply use > and < for in/un-denting (or 2>, 3>)
  2. For commenting, you'll need to remember change from Visual Mode into 'Visual Block Mode' first (so press <C-V> as before). Then, I , // , <ESC> as before.

I reckon that saves plenty of keystrokes. Hope you agree.

laher
  • 8,860
  • 3
  • 29
  • 39
  • I'm using GVim on Windows, which has mswin.vim included. It drops me into "Selection Mode" rather than "Visual Mode" when I drag my mouse, and replace all my selection into a ">" when I press the `>` key. – TomCaps Dec 15 '11 at 04:50
  • bummer. I just installed gvim on Linux and it worked as expected. Hmmm, must be some wierd default option. Does it work if you do `` before the `>` ? – laher Dec 15 '11 at 05:18
  • mswin.vim replace with ,it works. But I have not quite escaped from the evil mouse. – TomCaps Dec 15 '11 at 05:28
  • @TomCaps: To get Vim to use Visual Mode for mouse try issuing command `:behave xterm`. You could put this in your vimrc if it works for you. I believe mswin.vim handles remapping of ctrl-v, ctrl-c, ctrl-x and mouse stuff is set by `:behave`. – Herbert Sitz Dec 15 '11 at 08:12
1

If you learn the habit of using whitespace lines at the appropriate times, most often

V}

will select exactly the block you wanted.

<C-v>}

does the same, but in virtual block selections. Note on windows, C-v may have gotten remapped to C-q

Indenting

>}         # indent until next whitespace line
>2}        # idem, but two blocks at once
V}o{       # expand visual selection both ways

.          # repeat last action

# so:
V}2j>      # indent following lines + two lines after next break
.          # twice

Commenting

If you do not have any plugins,

<C-v>}I//<Esc>       # comment
<C-v>}x.             # uncomment

If you have NerdCommenter, it would be

V}<Leader>cc         # to comment
V}<Leader>cs         # to comment in to /* sexy */ blocks
V}<Leader>cu         # to uncomment
sehe
  • 374,641
  • 47
  • 450
  • 633
0

If you have lines that have been highlighted by mouse just press > to indent them. To unindent them press <. These are keystrokes that relate to indenting and unindenting even without the mouse.

>> --- Indent current line

2>> --- Indent current line + line underneath it

3>> --- Indent current line + 2 subsequent lines

etc.

You can select lines easily without the mouse by pressing V (that's a capital v) and then using any of Vim's many "motion" commands (or just the arrow keys) to extend the selection. At any point you can then press > to indent the selection or < to unindent.

Herbert Sitz
  • 21,858
  • 9
  • 50
  • 54