Home Classroom How To

1428

Vim is a very popular text editor, if you are a programmer, you probably have heard a lot about it. Here I try to present a concise list of the very basic functionality of Vim to get you started.

 

Basics

  • There are three basic modes in Vim, normal mode, visual mode, and the insert mode.
  • By default, you are in the normal mode when you start Vim.
  • Write :e /filepath to open the file you want
  • Write :e /filepath to open the file you want
  • Write i to switch to the insert mode, where you can write and edit text. (You can also use I, a, A, o, O, s, S also but they all have slightly different functionalities)
  • To switch back to the normal mode, press Esc.
  • Insert mode is used for only writing and editing text. Most of the commands are used in the normal mode.
  • The visual mode is used for selecting text and performing operations on the selected text. Visual mode commands in this text are labeled [VM]
  • Write v in normal mode to switch to visual mode, and Esc in visual mode to switch to the normal mode.
  • To save a file, use :w (w is for write).
  • To quit vim, use :q
  • i is used for inserting text, just before the cursor and a is used to insert
    text just after the cursor.
  • I is used for inserting at the beginning of the line, and A is used for appending text at the end of the line.
  • o is used for opening a new line below the cursor position.
  • O is used for opening a new line above the cursor position.
  • s is used to substitute the current character and S is used for substituting the current line. Substituting deletes the character(s) and allows you to
    insert zero or more characters.
  • r is for replacing a character, and R is for replacing continuous characters. For every character to be replaced, only one other character is put at its place.
  • v is used for selecting a character, which you can follow up with c to substitute it.
  • You can use V to select a line. Both v and V take you to the visual mode where you can select text, the commands are given later.
  • Use :cd /directorypath to switch to the directory you want.
  • Use :pwd to find the present working directory

 

Moving the Cursor

  • Use h,j, k,l to move the cursor left, down, up and right respectively.
  • w moves to the cursor to the next word. Attach a prefix to specify how many words you want to move ahead. Eg. 2w moves ahead by 2 words. Also, 2e moves to end of the second word from the current cursor position.
  • b moves to the previous word. Just like 2w, 2b moves back 2 words.
  • ( Moves the cursor one sentence back.
  • ) Moves the cursor one sentence ahead.
  • ^ Moves the cursor to the starting of the line
  • $ Moves the cursor to end of the line.
  • { Moves the cursor to the previous paragraph.
  • } Moves the cursor to the next paragraph.
  • mC sets a mark at the current cursor position where C is a char from ‘a’-'z’ and ‘A’-'Z’. This is usual when you need to jump back-and-forth between parts of the text.
  • ‘C takes you back to the place you marked with C.
  • Use ctrl-o to jump to the previous location (before a jump).
  • Use ctrl-i to jump to the next location again.
  • In visual mode, use ap to select a paragraph. [VM]
  • In visual mode, use ~ to flip the case of a a selection. [VM]

 

Editing

  • aw, ab, ap can be used to select a word, block or paragraph respectively. [VM]
  • Use d to delete a selection of text. [VM]
  • Use y to copy(yank). [VM]
  • Use p to paste. [VM]
  • Use dl to delete a character (You use l to move to the next char)
  • Use dw to delete a word (You use w to move to the next word)
  • yy yanks the current line and dd deletes the current line.
  • p pastes after the current cursor.
  • P pastes before the current pos of the cursor.
  • x is used for cutting a single character.
  • u is used for undo and ctrl-r is used for redo.
  • Instead of repeated undos, you can make use of the command :earlier 10m to go back to the version of the text 10 minutes back in time. Similarly, :earlier 10s for 10 seconds.
  • Make use of :later 10m to go to the version 10 minutes in the future. No, this is not a time machine :D . This command is effective when you have done some undos or used the :earlier command to come back to a version of text back in time, and now want to go back to a version that was created after the current one.
  • xp swaps two adjacent characters.
  • You can make use of the s, S, r, and R commands which we discussed earlier to edit.

 

Programming in Vim

  • You may want vim to distinguish between normal text and code. Normally vim automatically detects C/C++ code. But you can tell Vim that a particular file is a piece of code, for example, you can set the file type by :set filetype=Python for python.
  • Set indentation by :set autoindent
  • Use % to jump to the corresponding curly braces }.
  • To execute terminal commands in Vim, use :!cmd where, cmd is the command you want to execute.
  • Place your cursor over a particular local variable and the command gd, will take you to its declaration.
  • Use gD for the same function in case of global variables.
  • :new splits the window to open multiple documents.
  • Make use of ctrl-w motion-key where motion-key is one of h,j, k or l to go to the desired window.
  • You can also use ctrl-w ctrl-w to cycle between the open windows.
  • Use :sp to split the window into parts. This can be used for multiple windows to the same file, and is especially useful when you need to move back-and-forth between the same parts of the text.
  • :vsp to create a vertical partition.
  • To increase the size of a window, use ctrl- _
  • To make all widows equal in size, use ctrl- =
  • :tabnew to create a new tab.
  • :gt to go to a tab to the left
  • :gT to go to a tab to the right.

 

What Next?

I wrote this document while reading Swaroop C H’s book ‘A Byte of Vim’ (http://www.swaroopch.com) to keep track of the numerous commands that were coming my way. I strongly recommend this book if you want to read anything in detail or some advanced topics which I did not cover.
Happy Vimming :)