How to use Vim Editor in Linux

Linux TLDR
Last Updated:
Reading time: 3 minutes

Vim is popular and my personal favorite text editor in Linux, providing advance features and functionalities comparative to its competitor, the GNU Nano editor.

Although you can perform basic file editing using both of these programs, Still, they are slightly different from each other, as you can see from the following comparison.

Comparison of Vim and Nano Editor

Vim EditorNano Editor
Improved version of Vi editorImproved version of Pico editor
Mode-basedModeless
Slightly complex for beginnersPeace of cake for beginners
Supports programming languagesDoes not support programming languages
Advanced editor with many featuresBasic editor with limited features

Tutorial Details

DescriptionVim Editor
Difficulty LevelModerate
Root or Sudo PrivilegesMaybe
Host System and ArchitectureUbuntu 22.10 (x64)
OS Compatibility Ubuntu, Manjaro, Fedora, etc.
Prerequisitesvim
Internet RequiredYes (for installation)
Discussed Tools in this Article

How to Install Vim on Linux

Although this editor is available in most Linux distributions, if it’s missing from your system, execute any of the following commands, depending on your distribution:

For Debian/Ubuntu/Pop!_OS

$ sudo apt install vim -y

For Red Hat/Fedora/CentOS/AlmaLinux

$ sudo dnf install vim -y

For Arch/Manjaro/EndeavourOS

$ sudo pacman -sY vim

Opening the Vim Editor

Execute the following command with or without specifying the file path.

$ vim

Getting Started

You will get the following window when your launch the Vim editor.

Vim editor in Linux
Vim editor in Linux

In the Vim editor, you have two types of modes.

  1. Command-line mode allows you to pass commands into your editor (press Esc to enter).
  2. Insert mode allows you to edit the file (press i to enter).

Right now, you are in command-line mode, where you are unable to modify the file. Enter in insert mode by pressing the i input key, and you will get the “INSERT” alert at the bottom.

Entering in insert mode in the vim editor
Entering in insert mode in the vim editor

Now you can start typing your content inside the file; after you’re done, press the Esc key to enter in command-line mode to save the file.

Entering in command-line mode to save the file
Entering in command-line mode to save the file

Now press the “:” key to open the command-line interpreter.

Opening the command-line input
Opening the command-line input

To save the file, type the “w” key with the file name, including the extension, and then press the enter key to save the file with that name.

Saving the file in the vim editor
Saving the file in the vim editor

Lastly, your file will be saved in your current location, and you will get the amount of memory used to write it.

The file is saved using the vim editor
The file is saved using the vim editor

To exit from your current active Vim editor hold the shift key and press z key twice or you can again enter in command-line mode to exit Vim editor by using the :exit command.

If want to open the same file again than specify the path of the file to the vim command as shown.

$ vim file.txt

In case the file does not exists than it will create a new file with specified filename.

Output:

Opening the saved file again using the Vim editor
Opening the saved file again using the Vim editor

Press the “i” key to enter in insert mode and write your new content again.

Modifying the saved file in the vim editor
Modifying the saved file in the vim editor

This time you can directly hold the shift key and press the “z” key twice to save the file.

From now on, you’ll do most of the things yourself using the different commands shown in the following cheat sheet.

CommandsActions
hTo move the cursor to the left position
lTo move the cursor to the right position
jTo move the cursor to the down position
kTo move the cursor to the up position
MTo move the cursor directly to the middle of the screen
LTo move the cursor directly to the bottom of the screen
HTo move the cursor directly to the top of the screen
ePlaces the cursor at the end of the word
bPlaces the cursor at the start position of the previous word
wPlaces the cursor at the start position of the next word
$Places the cursor at the end position of the line
0Places the cursor at the start position of the line
}Takes the cursor to the start position of the next block or next paragraph
{Takes the cursor to the start position of the previous block or previous paragraph
)Moves the cursor directly to the start position of the next sentence
(Moves the cursor directly to the start position of the previous sentence
GPlaces the cursor at the end of the file
ggPlaces the cursor at the start of the file
#To go on a specific line, type the number of lines next to #
CTRL +  bMoves the cursor to one page back
CTRL + fMoves the cursor to one page forward

To get the descriptive cheat sheet, please refer to this site.

We will end this conversation here. If you find something missing, do let us know in the comment section.

Join The Conversation

Users are always welcome to leave comments about the articles, whether they are questions, comments, constructive criticism, old information, or notices of typos. Please keep in mind that all comments are moderated according to our comment policy.