vi/vim tutorial for beginners#
In this post I will show you the most basic and useful commands that every
vi user should know.
vimare not the same.
vi-like editor, it was developed as an improved and free alternative to the commercial (till 2002)
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 on Ubuntu:
$ sudo apt-get install vim
Once installed, the next time you run the
vi command, it will launch
vim instead of
vim comes installed on Mac OS X by default, so when you run
vi, it launches
vim. Mac is
Before we get to the commands, here is a very basic but important fact about
vim - it has two modes.
- Edit mode - when you can edit the contents of a file.
- 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
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
Creating and opening file#
- Creating a new file:
- Opening a file:
- Start typing new content:
- Insert a new line below the current line:
- Insert a new line above the current line:
vim to be in command mode, by pressing the esc key before trying out the following commands.
- Position cursor to top (Home):
- Position cursor to middle (Middle):
- Position cursor to bottom (Last):
- Position cursor to next word:
- Position cursor to previous word:
- Position cursor to start of next line: enter
- Delete current line:
- Delete two lines:
- Delete five lines:
Copying and pasting#
- Copy 2 lines:
- Copy 7 lines:
Undoing and redoing#
- Redo: Ctrl +
Searching and replacing#
To search type
/, type the string to search and press enter. Eg:
- Replace 'vi' with 'vim' (case-sensitive):
- Replace 'vi' with 'vim' (case-insensitive):
- Execute a shell command (
- Show line numbers:
- Syntax hilight on:
- Syntax hilight off:
- Page up: Ctrl +
- Page down: Ctrl +
Saving and quitting#
- Save as:
- Save and quit:
- Quit without saving:
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!