Monday, June 26, 2006

vim modes

Modes are probably the most obscure part of vim. I do not like them. But I do not like even more Emacs and PC's editors approach with terribly long and complicated shortcuts.

VIM mode defines how pressed keys are interpreted. One can tell currect VIM mode by content of left bottom corner of screen. Command (normal) mode has no special marker. When in insert mode one would see there '--INSERT--'. Replace & visual modes have similar markers: '--REPLACE--' & '--VISUAL--'.

  1. Command (normal) mode. The default mode used after invokation of vim. All keys are mapped into some command. Sleeping on keyboard in that mode can be dangerous: many key combos can be lethal to your document (or if you are root to your system). That's the main mode where one manipulates the content of file.

    Press <Esc> two times to get into command mode from any other mode/situation. Typing :q<Enter> would quit VIM.

    VIM's help for the shortcuts of command mode uses prefix "c_", e.g. ":help c_CTRL-D<Enter>" would bring the help for ^D shortcut of command mode. Type ":help c_" and press Tab/^D instead of Enter to see all standard shortcuts of command mode.

  2. Insert mode. The mode activated by pressing 'i' or <Insert> in command mode. Typing characters would insert the characters into the document. That's the mode one uses to input text. Shortcuts to manipulate text in insert mode are bound to non-printable characters and functional keys (e.g. keys with modifiers - Alt, Meta, Cmd, Shift, etc).

    VIM's help for the shortcuts of insert mode uses prefix "i_". Type ":help i_<Tab>" to see all standard shortcuts of insert mode. Insertion of text heavily covered in ":help insert.txt"

    N.B. Insert mode has a submode called "Replace mode". It's the same as insert mode, but typed letters instead of being inserted into the position under cursor - moving existing letters on the line right of cursor - would replace them. The submode is usually activated by pressing <Insert> in insert mode. I normally use 'R' (Shift-r) in command mode, since I do not like going into replace mode accidently hitting <Insert> twice (or hitting it once again in insert mode)

  3. Visual mode. The mode I use a lot, since I come from PCs/DOS and standard DOS editors depend heavily on visial text selection. Visual mode allows one to mark continuos part of text. Any invoked command allowing text range would use visual selection as the range. Commands are the same as in vim's command mode. The are three submodes:

    • Visual mode - by character. Activated by pressing 'v' in command mode. Use motions keys to select range character-wise. Selection starts on character under cursor when activating visual mode. End of selection is on characted under cursor. When finished selecting use command you like e.g. 'd' to delete selected text.

    • Visual mode - by line. Activated by pressing 'V' (Shift-v) in command mode. Allows to select lines of file. For example you can select several lines and send them to external filter application - e.g. ":r!sort" to sort them.

    • Visual mode - by block. Activated by pressing ^V (c_CTRL-V) in command mode. Most commonly refered as vertical selection. Allows you to select rectange-like part of text, spanning several lines. E.g. you can delete N characters in the beginning of several adjacent lines. Or every Nth character of several adjacent lines. Try it. It's hard to explain, but easy to understand by experimenting.

    Help for the shortcuts uses prefix 'v_', thou you would find that most of the shortcuts are the same as in command mode. ":help visual.txt" for more info.

Sode note. Officially, there are more to vim modes. The three aforementioned are the modes I use. All other modes find little application in my day to day life. For more indepth info on vim modes try ":help vim-modes". Every mode serves its purpose. Probably you would find some other modes useful too.

3 comments:

Scholarly Warrior said...

You wouldn't believe how long I was looking for how to perform vertical selection in vi. Holy crap I was pissed off. Luckily I found this blog when doing a (hopeless) web search. Hats off to you sir for authoring such a resourceful blog.

/do you have 400000 viruses?

beefstew said...

I realize this is an old post but it is very helpful, thanks. I've used vi/vim for a long time and never used visual mode. I can see many cases where visual mode would be useful.

I am not clear on the issue you bring up about hitting "esc" twice to get back to command mode from any situation. Wouldn't once be enough?

Ihar Filipau said...

> Wouldn't once be enough?

Not if you have accidentally pressed ^V - which in normal/insert mode escapes next pressed character.