vi/vim tutorial for beginners#

In this post I will show you the most basic and useful commands that every vi user should know.

vi and vim are not the same. vim is a vi-like editor, it was developed as an improved and free alternative to the commercial (till 2002) vi editor.

If you are on Ubuntu, you need to install vim first. The default vi editor which comes with Ubuntu is a little 'funky'. If the arrow keys print 'A', 'B', 'C', 'D', know that you are using vi and not vim.

Install vim on Ubuntu:

$ sudo apt-get install vim

Once installed, the next time you run the vi command, it will launch vim instead of vi.

vim comes installed on Mac OS X by default, so when you run vi, it launches vim. Mac is vim-ready!

Before we get to the commands, here is a very basic but important fact about vim - it has two modes.

  1. Edit mode - when you can edit the contents of a file.
  2. Command mode - when you can execute commands by pressing keys on the keyboard.

vim is in command mode by default. You can switch to edit mode by pressing i or o or O. You can switch to command mode by pressing the ESC key. While you are in the command mode, key board inputs will be interpreted as commands, and not as text inputs to the file.

So, let's see how we can accomplish the most common text editing functions in vim.

Creating and opening file#

  • Creating a new file: vi newfile.txt
  • Opening a file: vi .bash_profile

Editing text#

  • Start typing new content: i
  • Insert a new line below the current line: o
  • Insert a new line above the current line: O

Get vim to be in command mode, by pressing the esc key before trying out the following commands.

Cursor Position#

The arrow keys works as expected in the command mode

  • Position cursor to top (Home): H
  • Position cursor to middle (Middle): M
  • Position cursor to bottom (Last): L
  • Position cursor to next word: w
  • Position cursor to previous word: b
  • Position cursor to start of next line: enter

Deleting#

  • Delete current line: dd
  • Delete two lines: 2dd
  • Delete five lines: 5dd

When you are in edit mode, the backspace and delete keys can be used for deleting content as usual.

Copying and pasting#

  • Copy: yy
  • Copy 2 lines: 2yy
  • Copy 7 lines: 7yy
  • Paste: p

Undoing and redoing#

  • Undo: u
  • Redo: Ctrl + r

Searching and replacing#

  • Search: /

To search type /, type the string to search and press enter. Eg: /hello enter

  • Replace 'vi' with 'vim' (case-sensitive): :%s/vi/vim/
  • Replace 'vi' with 'vim' (case-insensitive): :%s/vi/vim/

Shell access#

  • Execute a shell command (pwd): :! pwd

Editor settings#

  • Show line numbers: :set number
  • Syntax hilight on: :syntax on
  • Syntax hilight off: :syntax off

Page scrolling#

  • Page up: Ctrl + u
  • Page down: Ctrl + d

Saving and quitting#

  • Save: :w
  • Save as: :w newfile.txt
  • Save and quit: :wq
  • Quit without saving: :q!

So these are some vim commands which will get you through most of the text editing challenges on the the Linux shell, Mac terminal etc. Mastering them will drastically improve your experience of using vim. All the best!