Everything is considered a file in Linux or Unix like systems, and if not so it is a process. But there are special files that are more than just files. For example, these are pipes and sockets. There is no difference between a file and a directory since a directory file contain names of other files. Texts, services, programs, etc are all files.

In general, all devices either input or output are considered to be files according to the system. During our day-to-day operations as System Administrators, we have to edit these files in satisfaction of our work. File editing can be done with text editors like vi, vim, nano and emacs.

In this guide, we are going to learn how to edit files using vi or vim editor in Linux.

What is vim?

Vim is an improved version of the vi editor. Vim is a highly configurable text editor built to enable efficient text editing in Unix and Linux operating systems. Sometimes it is called Programmer’s Text Editor.

By default, vim editor is not installed on Linux. To check whether vim is installed, launch your terminal with CTRL+ALT+T keys and type “vim” you will see Vim splash page appear.

Otherwise run the following commands to install it.

For Debian/Ubuntu;

sudo apt update
sudo apt install vim 

For RHEL/CentOS;

sudo yum install vim

For Arch Linux;

sudo pacman -S vim

For Fedora Linux 22 and above;

sudo dnf install vim-enhanced

Understanding vim Modes

vim editor have three different execution modes for where program behavior changes;

Command Mode

Also known as normal mode, this is where vim starts by default. In this mode, keyboard keys are associated with commands for navigation and text manipulation tasks.

Insert Mode

The insert mode is straightforward: text appears on the screen as it is typed on the keyboard. It is the type of interaction most users expect from a text editor, but it is not how vim first presents a document.

Ex Mode

This mode is sometimes also called colon commands because every command entered here is preceded with a colon (:). For example, to leave the vim editor and not save any changes you type :q and press the Enter key.

Navigating Files using vim

You can navigate within a file in vim as follows;

Move cursor left one character:


Move cursor right one character:


Move cursor down one line (the next line in the text):


Move cursor up one line (the previous line in the text):


vim command mode – Cursor move commands

The following are commonly used commands to move cursor in vim

Moving Cursor

Move cursor forward one word to front of next word:


Move cursor to end of current word:


Move cursor backward one word:


Move cursor to beginning of line:


Move cursor to end of line:


Move cursor to the file’s first line:


Move cursor to the file’s last line:


Move cursor to file line number n:


Screen scrolling in vim

Scroll up almost one full screen:


Scroll down almost one full screen:


Scroll up half of a screen:


Scroll down half of a screen:


Scroll up one line:


Scroll down one line:


vim command mode – edit commands

The following are commonly used vim command mode editing commands;

Inserting in vim

Insert text after cursor:


Insert text at end of text line:


Insert text before cursor:


Insert text before beginning of text line:


Open a new text line below cursor, and move to insert mode:


Open a new text line above cursor, and move to insert mode:


Searching in vim

Forward searching:


Backward searching:


Deleting in vim

Delete current line:


Cut four lines:


Delete current word:


Cut a character:


Cut to the end of the line:


Pasting in vim

Paste copied text after cursor:


Paste copied (yanked) text before cursor:


Copying in vim

Yank (copy) current word:


Yank (copy) two lines:


Yank (copy) current line:


vim ex mode commands

This is where saving our changes takes place in vim.

Saving changes in vim

Write buffer to file and quit editor:


Write buffer to file and quit editor:


Write buffer to file and quit editor (overrides protection):


Write buffer to file and stay in editor:


Write buffer to file and stay in editor (overrides protection):


Quit editor without writing buffer to file:


Quit editor without writing buffer to file (overrides protection):


Write buffer to file and quit editor:


Using Tabs in vi

Open a second tab in vim:


Listing all tabs in vim:


Switch to the next tab:


Switch to the previous tab:


Extra vim tips

Search and replace in vim

In vi and vim you cab search and replace text



Turning line numbering on and off

Turn on line numbering:

:set number
:set nu

Turn off line numbering:

:set nonumber
:set nonu

Enable Auto indentation

Use set command like below:

:set autoindent
:set ai

Turn off:

:set noautoindent
:set noai

Setting the indentation level:

:set shiftwidth=4

Ignore case on searches

Enable case ignore:

:set ignorecase
:set ic

Turn case sensitivity back:

:set noignorecase
:set noic

Using vimdiff command

You can use the vimdiff command to compare two, three or four files with vim or vi side by side.


vimdiff file1 file2

Set vim as default editor

To set vim as our default editor, we add the following to our shell configuration i.e ~/.bashrc file.

export VISUAL=vim

How To Customize vim

To customize vim you create a file under ~/.vimrc

See example below:

$ cat  ~/.vimrc
set nocompatible
set backup
set history=1000
set ignorecase
set smartcase
set hlsearch
set incsearch
set number
set showmatch
syntax on
highlight Comment ctermfg=LightCyan
set wrap


This marks the end of our guide on Basic File Editing with vim. I hope the guide have been helpful.

Below are more interesting guides on LPIC 101.

LPIC 101 – Compress and Uncompress Files Using tar, gzip, bzip2

LPIC 101 – Managing Processes in Linux Systems

LPIC 101 – Changing Runlevels and Boot Targets on Linux System

LPIC 101 – Processing Text Streams Using Filters on Linux