vim, short for Vi Improved is a command-line text editor that is used for creating and viewing text files. In this vim tutorial, you are going to learn useful tips in using the vim text editor. Without much ado, let’s jump right in.
vim has 2 modes; insert mode and command mode.
- Insert mode allows you to enter text into the text file like you would in a normal file editor.
- Normal mode gives you the power to navigate and make modifications to the text.
To navigate between the 2 modes, use the ESC character or i .
Once you open a text file you get into normal mode. To change to insert mode and start typing or inserting text, press the letter i on your keyboard.
To go back again to command mode, hit the Esc key.
Insert mode allows you to interact with the text. For example, you can enter text, delete text and navigate up and down, left or right.
Once you’ve opened a text file using vim editor, you immediately land in command mode.
To enter into Insert mode and start inserting text, follow the command below.
i key to insert text at the current cursor position.
a key to insert text one character after the current position of the cursor.
A key to insert text at the end of the current line.
o key to insert text on a new line below the current line.
O key to insert text on a new line above the current line.
s key to delete the current letter on the cursor and insert text.
S key to delete the current line and insert text.
Commands in vim command mode
Let’s now look at the command mode. Arguably, this is the mode that you can perform most of the operations.
Navigating in command mode (up/down, left/right)
To navigate up/down, left/right use the keys h, j, k, and l.
k – Moves the cursor up by one line.
j – Moves the cursor down by one line.
l – Moves the cursor to the right by one character
h – Moves the cursor to the left by one character
In command mode, you can also navigate to the beginning and end of a line/file. Let’s dive into the commands for achieving this.
Navigating to the beginning and end of a line/file in command mode
^ – Moves the cursor to the beginning of a line
$ – Moves the cursor to the end of a line
1G – Moves the cursor to the beginning of a file.
G – Moves the cursor to the end of a file
nG – Moves the cursor to the start of line number “n” in the file
Searching and replacing text
Command mode also gives the user the ability to search and replace text in a file
To search for text in a file, press the ESC key and use the
/ (forward slash) followed by the search term e.g
To search backwards in the text file, again press the ESC key and use the
? followed by the search term e.g
If you want to search a string and replace it with another string in the file, use the syntax
For example, if we want to search the string
Linux and replace it with
Unix from Line 1 to line 3 in code>linuxgeek.txt file , the command will be
/g ensures that all instances of the search string are replaced within the specified range. If you omit the
/g option, only the first instance in each line will be replaced.
Copying and pasting text
Vim also gives you the ability to copy and paste text within the file.
To copy a string and paste it at another location in a file follow the steps below
- Move the cursor to the beginning of the string or text
von your keyboard and press cursor forward to highlight text
- Once you get to the end of text , hit
yshort for yank , to copy the text
- Move the cursor to the location you want to paste the copied text
pshort for paste, to paste the copied content
Additionally, you can use the commands below for copying lines.
y$ – Copies text from current position to the end of the line
yy – Copies the entire line
4yy – Copies 4 lines below
If you want to delete a single character in command mode, hit the key
To delete a word, place the cursor in front of the word and hit
To delete text from the current word to the end of the line hit
To delete or cut the entire line hit
You can delete a number of lines by preceding the
dd command with a number. For example, to delete 3 lines including the current line, run
Saving and Quitting vim Editor
Below are different ways that you can use to quit the vim editor.
:wq – To save changes and quit the vim editor
:q! – To quit without saving changes
:e– To save and exit where changes exist
We hope that you found this tutorial insightful. Feel free to take a test run on your editor and leave us feedback.