DEV Community

Shah Nawaz Shuvo
Shah Nawaz Shuvo

Posted on • Edited on

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 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

Command Action
:e filename Open filename for editon
:w Save file
:q Exit vim
:q! Quit without saving
:x Save and Exit
:sav name Save 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 end of line use $.

Find Word Under Cursor

Find the next occurrence of the word under 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 text of 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 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 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 upto 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 coppied using y. It can be pasted after the cursor using p or before the cursor using P.

Top comments (4)

Collapse
 
taylorbeeston profile image
Taylor Beeston

I think one of the most helpful things with learning vim is to realize that you don't need to use <Esc> to get out of insert mode! Both <C c> (CTRL+C) and <C [> (CTRL+[) will work. Using these allows you to switch modes without leaving home row, which I think really helps with learning muscle memory.

Also, I can't speak for everyone, but I really got used to vim by forcing myself to use ASDF HJKL as home row instead of ASDF JKL; as home row like I was taught in school

Collapse
 
shuv1824 profile image
Shah Nawaz Shuvo

Thank you very much for the feedback. Really helpful suggestion you gave there.

Collapse
 
kewbish profile image
Emilie Ma

Great guide!

Collapse
 
shuv1824 profile image
Shah Nawaz Shuvo

Thanks.