How to Use Vim for Linux Users: A Step-by-Step Guide

Oodo Roland Uchenna
9 min readJul 24, 2023

--

Vim is a widely used text editor among Linux enthusiasts for its efficiency and flexibility.

However, it can be challenging for new Linux users to learn. This guide is here to change that. I will take you step by step through the basics of Vim, from navigating and editing files to using its advanced features.

Whether you are new to Linux or looking to improve your workflow, this guide will help you unlock the full potential of Vim

What is Vim?

Vim (vi) is a command line-based text editor that is commonly used in Unix-based operating systems.

It is known for its efficiency and powerful set of commands, which allow users to quickly and easily edit text files.

It has a challenging process, but many users find it to be a valuable tool for editing text files quickly and efficiently.

Vim is an extended version of Vi, that includes additional features such as syntax highlighting, undo/redo, and a visual mode for selecting text.

How to start and exit Vi (Vim)

Vim is used to create and edit text files. To start Vim, you need to open a terminal on your Linux computer.

A terminal is a program that allows you to interact with your computer using text commands. Once the terminal is open, you can start Vim by typing “vi” followed by the name of the file you want to edit.

For example:

vi file.txt

this will open the “file.txt” in vim for editing.

Once the file is open, you will be in normal mode. In this mode, you can navigate through the text using the arrow keys on your keyboard.

To start editing the file, you need to press the “i” key on your keyboard.

This will take you to the insert mode, where you can add, modify or delete text.

To go back to normal mode, you need to press the “Esc” key on your keyboard.

While in normal mode, you can also use commands such as “:w” to save your changes, and “:q” to quit without saving changes.

To exit vim and save your changes, you need to press the “ESC” key, then type “:wq” and press the “Enter” key.

This command will save your changes and close the file.

To exit vim without saving your changes, you need to press the “Esc” key, then type “:q!” and press the “Enter” key.

This command will close the file without saving your changes.

It is important to note that you must press the “Esc” key first before entering any command in the command mode.

In summary, Vim has two main modes that are used frequently as seen above:

  1. Normal mode: This is the default mode you see when you first open a file in vim. In normal mode, you can navigate through the text using the arrow keys, and you can use various commands to manipulate text.
  2. Insert mode: This mode allows you to insert or edit text within a file. To enter insert mode, press the “i” key in normal mode. Once in insert mode, you can type text as you would in any other text editor. To return to normal mode, press the “Esc” key.

How to edit text in Vim

Editing text in Vim can seem a bit tricky at first, but once you understand the basic concepts, it becomes quite straightforward.

When you first open a file in Vim, you will be in “normal mode”.

In this mode, you can navigate through the text using the arrow keys, but you cannot make any changes to the text.

To make changes to the test, you need to enter “insert mode”.

To enter insert mode, you can press the “i” key.

Once you are in insert mode, you can add, modify, or delete text as you would in any other text editor.

To exit insert mode, you can press the “Esc” key.

For example, let’s say you have a file open in Vim and the contents of the file are:

Hello, this is a sample text file.

BashCopy

If you want to add more text to the end of the file, you would first press the “i” key to enter insert mode, then you can add text at the cursor position.

Hello, this is a sample text file.
This is an additional text.

BashCopy

Also, if you want to modify some existing text, you would first press the “i” key to enter insert mode, then place the cursor on the text you want to change and start editing, then press the “Esc” key to go back to normal mode.

How to cut, copy and paste lines in Vim

One of the common tasks you may need to do is to move lines of text around in a file. To do this, Vim provides the commands “dd”, “yy” and “p” to help you cut, copy and paste lines of text.

  • To cut a line, you need to move your cursor to the line you want to cut, then type “dd”. This will delete the line and store it in a buffer.
  • To copy a line, you need to move your cursor to the line you want to copy, then type “yy”. This will copy the line and store it in a buffer.
  • To paste a line, you need to move your cursor to the location where you want to paste the line, then type “p”. this will insert the line from the buffer at the location of the cursor.

For example, if you have a file that looks like this:

This is the first line.
This is the second line.
This is the third line.

and you want to move the second line to the end of the file, you could do the following:

  • Move your cursor to the second line
  • Type “dd” to cut the second line
  • Move your cursor to the third line
  • Type “p” to paste the second line after the third line

Now the text file will look like this:

This is the first line.
This is the third line.
This is the second line.

It is important to remember that the commands “dd”, “yy” and “p” are used in the Vi command mode to work with lines, not words or characters.

How to search forward and backward in Vim

In Vim, you can search for a specific word or phrase in your text by using the “search” command. There are two ways to search in vim: forward and backward.

To search forward for a word or phrase:

  • Press the “/” key, which will bring up the search prompt at the bottom of the screen.
  • Type in the word or phrase you want to search for and press the “Enter” key.
  • Vim will take you to the next occurrence of the word or phrase in your text.
  • You can press “n” to go to the next occurrence or “N” to go to the previous occurrence.

For example, if you have a text file that looks like this:

This is the first line.
This is the second line.
This is the third line.

If you want to search for the word “the”, you would press “/” then type “the” and press enter.

This will take you to the first occurrence of the word “the” in the text. If you press “n” it will take you to the next occurrence of the word.

To search backward for a word or phrase:

  • Press the “?” key, which will bring up the search prompt at the bottom of the screen.
  • Type in the word or phrase you want to search for and press the “Enter” key.
  • Vim will take you to the previous occurrence of the word or phrase in your text.
  • You can press “n” to go to the next occurrence or “N” to go to the previous occurrence.

It is important to notice that the commands “n” and “N” are used to navigate between the search results not to search again.

How to undo in Vim

In Vim, you can undo changes you have made to a text file by using the “undo” command. This can be helpful if you accidentally delete or change something and want to go back to the previous version.

To undo the last changes, you made:

  • Press the “u” key. This will undo the last change you made.
  • You can press “u” multiple times to undo multiple changes.

For example, if you have a text file that looks like this:

This is the first line.
This is the second line.
This is the third line.

BashCopy

and you delete the second line by accident, you can press “u” to undo the deletion and bring the second line back.

It is important to notice that the undo command only undoes the last change you made.

If you have made multiple changes and want to go back to a previous version, you can use the command “: earlier” followed by the number of minutes you want to go back.

For example, : earlier 5m will undo the changes made in the last 5 minutes.

Also, it is possible to redo changes by using the command “ctrl + r”.

How to quit Vim

In Vim, you can quit the editor by using the “:q” command. This command will close the current file and exit Vim.

To quit Vim:

  • Press the “Esc” key to enter command mode
  • Press the “:” key
  • Type “q” and press “Enter”.

For example, if you are currently editing a file called “file.txt” and want to quit Vim, you would press the “:” key, type “q”, and press “Enter”.

However, if you made changes to the file and have not saved them yet, Vim will prompt you to save the changes before quitting. If you want to save the changes and quit, you can use the “:wq”.

If you don’t want to save the changes and quit, you can use the command “:q!”.

It is also important to notice that if you are editing multiple files in a Vim session, you can quit the current file using “:q” but you have to use “:qa” to quit all the open files, also you can use “:qa!” to quit all the open files without saving the changes.

Further Resources:

Before you wrap up, I invite you to delve deeper into the world of Linux file systems.

Conclusion

To wrap it up, Vim is a powerful and versatile text editor that Linux users can benefit greatly from.

With this guide, users can learn how to use Vim for basic text editing tasks, as well as advanced features like undo and redo.

By mastering Vim’s commands and shortcuts, Linux users can take their text editing skills to the next level, becoming more efficient and productive.

Struggling with a Challenge? Join our ‘Ask a Question’ forum! Connect, learn, and get expert help from our community. Join now!

#Vim #TextEditor #TechLover #linux #linuxadministrator

--

--