Vim: Modes
This is part 2 of a series of tutorial to Vim. You can read Part 1 here.
Multiplexing Keys
Vim does different things on pressing the same key based on the mode enabled. There are many modes in Vim's modal interface. We'll begin with just two because these two are the ones which you are going to use the most. I'll introduce other modes in later parts.
Normal Mode
In editors without modal interface, there are two sets of keys: the character
keys which print characters on screen and control keys using which you give commands
to the editor like <ctrl>+s
, <pageup>
, <ctrl>+<rightarrow>
.
In normal mode, character keys can be used to give control commands to Vim. This
makes a variety of commands possible without having intricate key combination1. By
default Vim opens up in normal mode, that is why when you press :q
, Vim interprets
it as command rater than printing literal string ":q" on buffer.
Insert Mode
When switched to insert mode, Vim behaves like any other conventional text editor you might have used. You can use character keys to input text and arrow keys to move around. Use the insert mode to enter text and nothing else.
Lesson 3
Press i
to switch to insert mode.
Press <Esc>
to switch to normal mode.
Most of the magic in Vim happens in normal mode. Thus, unless you are actually entering text, be in normal mode. I can't stress this enough, actually I can, here's amendment to Lesson 3:
Normally, be in normal mode.
Vim will tell you which mode you are in at bottom left corner of screen.
Achievement Unlocked: Nano Level
Congratulations, just like that you have gained enough skills to replace Nano
(well almost, you still need to know how to search, cut and paste, but that's
quite simple after this). You can now write commit message in Vim, do inline
changes on production server2 or set crontab
. Here's a quick summary of
what we have learned till now:
- hit
vim
to fire up Vimvim <filename>
opens up the file to edit, if file doesn't exists, a new file is created.
- hit
i
to switch to insert mode - In insert mode, Vim behaves like any other editor: use character keys to input text, arrow keys to move around.
- hit
<Esc>
to return back to normal mode. - When not entering text, be in normal mode.
- hit
:q
to quit:q!
to quit, discarding changes:w
to write file (save file)- writing file doesn't quit Vim, hit
:q
to quit after writing or:wq
to write file and quit in one go.
- writing file doesn't quit Vim, hit
Go on, give Vim a spin. You may feel using it very cumbersome, but I assure after going through next couple of parts your productivity will be at par, if not more, than your current editor. You are going through a paradigm shift, it is not going to be easy.