Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there some pattern behind the many VIM commands?

Tags:

vim

editor

I have to add a VIM personality to an IDE. I never used VIM for more than the most basic edits and i'm now overwhelmed by the complexity of the command structure.

Is there any overall structure for the combination of counts moves and insert/delete commands? I just can't see the wood for the trees.

like image 947
Lothar Avatar asked Jan 12 '10 11:01

Lothar


3 Answers

Well, there is obviously a finger position pattern behind h, j, k, l.

The fact that ^ goes to the beginning of a line and $ goes to the end is patterned on common regular expression syntax.

Ctrl-F and Ctrl-B page forward and back, and that's fairly intuitive.

i inserts (before) and a appends (after the cursor). Similarly,
I inserts at the beginning of the line, and A appends at the very end.

> and < indent and outdent, respectively. That's also kind of intuitive.

But on the whole, many of the other commands are on whatever keys were left – it's hard to find an intuitive mapping between the letters of the alphabet and an editor's commands.

Repeat counts are always entered before a command, and mostly repeat the command that many times, but in some cases do something clever but analogous.

I think the secret to not going crazy over vi is to start out with only a small handful of commands. I have a lot of colleagues who don't know to do anything other than

  • move the cursor around using the arrow keys (you don't have to use h, j, k, l);
  • insert with i, delete with Del (you don't have to use x);
  • delete a line with dd
  • get out of input mode with Esc
  • get out of vi with :x (exit) or q! (quit, and throw away my changes!)

Because I'm much smarter, the additional commands I know and use are:

  • go to the top of the file with gg, the bottom with G.
    I can go to a specified line number with (line-number)G.
  • copy a line with y (yank), paste it with p
  • change a word with cw, the rest of the line with C
  • delete a word with dw, the rest of the line with D
  • I sometimes use . to repeat the last command, or u (undo) if I messed up.

When you have occasion to use other commands, you can teach them to yourself one by one as needed.

like image 119
Carl Smotricz Avatar answered Oct 24 '22 04:10

Carl Smotricz


This is a good article for explaining the VIM philosophy.

like image 31
Dan Dyer Avatar answered Oct 24 '22 05:10

Dan Dyer


I think the characteristic that better defines VIM in respect to other editors is its wide array of motion commands. The first thing to learn to fully use VIM is hitting the arrow keys as little as possible, and think at the text in terms of "blocks" like "a sentence" "a tag" "a word" "a group of brackets".

Say you have function foo($bar, $fooz) you can change the parameters by simply positioning your cursor anywhere inside the brackets and pressing ci) (mnemonic: change inner bracket). The same pattern applies to other commands: yank (y), delete (d) and so on.

I know this doesn't explain the whole "VIM philosophy" but combining normal mode commands with the vast amount of motion modifiers is what really made me see the light.

like image 2
Matteo Riva Avatar answered Oct 24 '22 04:10

Matteo Riva