Trying out Vim

Trying out Vim

Vim (Vi IMproved) is a highly configurable text editor built to make creating and changing any kind of text very efficiently. It is included as "vi" with most UNIX systems and with Apple OS X.I attempted to learn Vim quite a few times before but could not cope up with it. But recently I thought I should at least know very basic usage of Vim. So I started learning the absolute basics. Here I am putting all the basic commands and usage of Vim for any absolute beginner like me. This can be helpful to get started with Vim I think. But there is a lot of things to learn about this handy tool that will take much time and practice. One will get proficient in using Vim only by using it consistently.

Vim has two basic modes:

  1. One is INSERT mode, in which you write text as if in a normal text editor.
  2. Another is NORMAL mode which provides you efficient ways to navigate and manipulate text. This is also called Command mode cause we can do various vim commands on this mode.

To change between modes, use ESC for normal mode and i for insert mode.

VIM Commands

CommandAction
:e filenameOpen filename for editon
:wSave file
:qExit vim
:q!Quit without saving
:xSave and Exit
:sav nameSave current file as name
.Repeat last change made in NORMAL mode

Cursor Movements

  • h to move cursor left (←)
  • l to move cursor right (→)
  • k to move cursor up (↑)
  • j to move cursor down (↓)
  • b moves to the beginning of the word
  • e moves to the end of the word
  • w moves to the beginning of the next word

Number Based Movements

Using a number before each command can execute that command that many times. e.g 3w will move to the 3rd next word.

Insert Text Repeatedly

To insert the same text multiple times use <number>i<text>esc. e.g 3<i>go<esc> will write gogogo.

Find a Character

To find and move to the next (or previous) occurrence of a character, use f and F, e.g. fo finds next 'o'. You can combine f with a number. e.g. you can find 3rd occurrence of 'q' with 3fq.

Go to Matching Parenthesis

In text that is structured with parentheses or brackets, ( or { or [, use % to jump to the matching parenthesis or bracket.

Start/End of Line

To reach the beginning of a line, use 0. For the end of line use $.

Find Word Under Cursor

Find the next occurrence of the word under the cursor with *, and the previous with #.

Go to Line

gg takes you to the beginning of the file; G to the end. To jump directly to a specific line, give its line number along with G. e.g 5G will take you to the fifth line of the file.

Search for Text

Searching text is a vital part of any text editor. In Vim, press /, and give the text to search for. The search can be repeated for next and previous occurrences with n and N respectively. For advanced use cases, it's possible to use regexps that help to find the text of a particular form.

Insert New Line

To insert text into a new line after the current line use o and to insert a new line before the current line use O.

After a new line is created, the editor is set to insert mode.

Removing a Character

x and X delete the character under the cursor and to the left of the cursor, respectively.

Also adding a number before x of X can perform the action that many times. e.g 5x will remove the next five characters including the character under the cursor and 5X will remove the previous five characters excluding the character under the cursor.

Replace a Character

User r to replace only one character under the cursor.

Deleting

d is the delete command. It can be combined it with movements, e.g. dw deletes the characters on the right side of the cursor up to the beginning of the next word. de deletes all the characters of the word on the right side of the cursor including the character under cursor. db will delete the previous word if the cursor is under the first letter of a word or else it will delete the characters left to the cursor upto the beginning of the word.

It also copies the content, so that you can paste it with p to another location.

dd will delete the whole line.

Repeat Command

To repeat the previous command, just use . (period).

Replace Mode

Use R to enter REPLACE mode. In this mode characters under the cursor can be replaced.

Visual Mode

Use v to enter the VISUAL mode and V to enter VISUAL LINE mode. In this mode the text can be selected by the movement keys before deciding what to do with it.

Selected text can be deleted/cut using d or copied using y. It can be pasted after the cursor using p or before the cursor using P. </esc></i></text></number>