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:

h

Move cursor right one character:

l

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

j

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

k

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:

w

Move cursor to end of current word:

e

Move cursor backward one word:

b

Move cursor to beginning of line:

^

Move cursor to end of line:

$

Move cursor to the file’s first line:

gg

Move cursor to the file’s last line:

G

Move cursor to file line number n:

nG

Screen scrolling in vim

Scroll up almost one full screen:

Ctrl+B

Scroll down almost one full screen:

Ctrl+F

Scroll up half of a screen:

Ctrl+U

Scroll down half of a screen:

Ctrl+D

Scroll up one line:

Ctrl+Y

Scroll down one line:

Ctrl+E

vim command mode – edit commands

The following are commonly used vim command mode editing commands;

Inserting in vim

Insert text after cursor:

a

Insert text at end of text line:

A

Insert text before cursor:

i

Insert text before beginning of text line:

I

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

o

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

O

Searching in vim

Forward searching:

?

Backward searching:

/

Deleting in vim

Delete current line:

dd

Cut four lines:

4dd

Delete current word:

dw

Cut a character:

x

Cut to the end of the line:

D

Pasting in vim

Paste copied text after cursor:

p

Paste copied (yanked) text before cursor:

P

Copying in vim

Yank (copy) current word:

yw

Yank (copy) two lines:

2yy

Yank (copy) current line:

yy

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:

:x

Write buffer to file and quit editor:

:wq

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

:wq!

Write buffer to file and stay in editor:

:w

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

:w!

Quit editor without writing buffer to file:

:q

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

:q!

Write buffer to file and quit editor:

ZZ

Using Tabs in vi

Open a second tab in vim:

:tabe

Listing all tabs in vim:

:tabs

Switch to the next tab:

:tabn

Switch to the previous tab:

:tabp

Extra vim tips

Search and replace in vim

In vi and vim you cab search and replace text

Example:

:%s/OLDLINE/NEWLINE/g

Turning line numbering on and off

Turn on line numbering:

:set number
#OR
:set nu

Turn off line numbering:

:set nonumber
#OR
:set nonu

Enable Auto indentation

Use set command like below:

:set autoindent
#OR
:set ai

Turn off:

:set noautoindent
#OR
: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.

Example:

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
export EDITOR="$VISUAL"

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

Conclusion

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