Basic Vim Commands
Has Vim's vast options intimidated you to the point of not even daring to learn the basics? Fear not, for the Captain is here with you. In this post I will show you the most basic and useful commands that every Vim user should know.
First off, there is a difference between Vi and Vim. Vim is like the improved version of the Vi editor (Vim == Vi improved).
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.
i. Edit mode - when you can edit the contents of a file.
ii. 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 Esc. 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.
• Create a new file
• Open a file
• Start typing new content
• Insert a new line below the current line
• Insert a new line above the current line
The following commands require Vim to be in command mode, so press Esc before trying out the commands.
Note: the arrow keys works as expected in the command mode.
• Position cursor to top (
• Position cursor to middle (
• Position cursor to bottom (
• 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
Note: when you are in edit mode, the backspace and delete keys can be used for deleting content as usual.
• Copy 2 lines
• Copy 7 lines
To search type
/, type the string to search and press enter. Eg: /hello Enter
• 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
• Page down
• 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!