Wednesday, October 31, 2012

Starting with vim

To open a file in vim, at the prompt, type
$ vim test.txt

This opens test.txt if it already exists. Otherwise, it creates a new empty file named test.txt and opens it.

You can not type anything as soon as you open the file, because there are two modes in vim. And when you first open the file, vim is in command mode.

In the command mode almost every key on the keyboard is a command. For example, i is the command to start inserting text.

In the commands which follow, I will say esc before the command every time(eg: esc i), because we are usually using these commands after we finish typing something. But if you are already in command mode, you can just hit the key and there is not need to hit the escape key every time.

esc i    Insert text
Now, you can start typing in any text.

Once you have finished, press escape again, to go back to command mode.

esc :w Save File
esc :wq Save and Quit
esc :q! Quit without saving   
esc u Undo changes
esc control R     Redo changes

esc dd Delete a line
esc dw Delete a word
esc x Delete one character
esc yy Copy a line
esc yw Copy a word
esc p Paste last copy or cut contents

When you want to learn more, you can try this vim tutorial.

No comments: