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 modes

vim has 2 modes; insert mode and command mode.

  1. Insert mode allows you to enter text into the text file like you would in a normal file editor.
  2. 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

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.

Inserting text

Press i key to insert text at the current cursor position.

Press a key to insert text one character after the current position of the cursor.

Press A key to insert text at the end of the current line.

Press o key to insert text on a new line below the current line.

Press O key to insert text on a new line above the current line.

Deleting text

Press s key to delete the current letter on the cursor and insert text.

Press 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

Copy
:/Linux

To search backwards in the text file, again press the ESC key and use the ? followed by the search term e.g

Copy
:?Linux

If you want to search a string and replace it with another string in the file, use the syntax

Copy
:[range]s/search/replace/

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

Copy
:1,3 s/Linux/Unix/g

The /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.

Copying text
To copy a string and paste it at another location in a file follow the steps below

  1. Move the cursor to the beginning of the string or text
  2. Type v on your keyboard and press cursor forward to highlight text
  3. Once you get to the end of text , hit y short for yank , to copy the text
  4. Move the cursor to the location you want to paste the copied text
  5. Hit p short 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

Cutting/Deleting text

If you want to delete a single character in command mode, hit the key x

To delete a word, place the cursor in front of the word and hit dw

To delete text from the current word to the end of the line hit d$

To delete or cut the entire line hit dd

NOTE:
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 3dd

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

:x or :exit or :e– To save and exit where changes exist

Wrapping up

We hope that you found this tutorial insightful. Feel free to take a test run on your editor and leave us feedback.