In this tutorial, you are going to learn how to edit text files with Vim in Linux. Vim
, an acronym Vi IMproved is a programmer’s text editor that is used to edit all kinds of plain texts and modify files. System administrators works with files in their daily activities therefore, as good system admin you require knowledge on using vim
to edit file. Using VIM
makes work easier and faster.
Edit Text Files with Vim in Linux
Installing Vim in Linux
To check whether vim
is installed in the system, launch your terminal and type vim
you will see vim splash screen appear.
You can as well use your respective Linux distro package manager to check if vim is installed.
For example;
dpkg -l | grep vim
Example output. Note, ii means installed.
ii vim 2:8.1.2269-1ubuntu5 amd64 Vi IMproved - enhanced vi editor
ii vim-common 2:8.1.2269-1ubuntu5 all Vi IMproved - Common files
ii vim-runtime 2:8.1.2269-1ubuntu5 all Vi IMproved - Runtime files
ii vim-tiny 2:8.1.2269-1ubuntu5 amd64 Vi IMproved - enhanced vi editor - compact version
Consult your distro package manager on how to check if packages are installed.
Otherwise, type the following command to install vim.
In RHEL or CentOS Linux;
sudo yum install vim
In Debian or Ubuntu Linux;
sudo apt install vim
In Arch-Based Distribution;
sudo pacman -S vim
In Fedora Linux 22 and above;
sudo dnf install vim-enhanced
Edit Text Files with Vim in Linux
There are three standard modes in vim
editor:
- Command mode: Also called normal mode, this is the default mode in which vim starts up. Here you enter keystrokes to enact commands. i.e pressing the k key move cursor up one line (the previous line in the text).
- Insert mode: Also called edit or entry mode, this is where you perform editing i.e write the text.
- Ex Mode: Also called colon commands here every command entered is preceded with a colon (:) i.e to leave the vim editor and save changes you type :wq and press the Enter key.
Vim Navigation
In Vim, you can navigate around to different insertion points i.e
- Left
- Right
- Up
- Down
- Jump to the start of the word
- Jump to the end of the word
Inserting Text
Enter insert mode.
i
Jump to the start of the word.
w
Jump to the end of the word.
e
Return to command mode.
esc
Cursor Movement
Move the cursor up.
k
Move the cursor down.
j
Move the cursor left.
h
Move the cursor right.
l
Insert before the cursor.
i
Insert at the beginning of the 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
Append after the cursor.
a
Append at the end of the line.
A
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
Scrolling
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
Cut, Copy and Paste Text
Cut
Cut a line.
dd
Cut three lines.
3dd
Cut current word.
dw
Cut a character.
x
Cut to the end of the line.
D
Copy (Yank)
Yank (copy) current line.
yy
Yank (copy) four lines.
4yy
Yank (copy) to the end of the line.
y$
Paste
Paste copied (yanked) text before cursor.
P
Paste copied text after cursor.
p
Exit Vim Editor
There are several ways to exit vim;
- Write the file but don’t exit (save)
- Write and quit (save and exit)
- quit, it fails when there is unsaved changes
- Force quit (this will exit even if there is unsaved changes)
Write buffer to file and quit editor.
:x
Write buffer to file and quit editor.
ZZ
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
Write buffer to file and quit editor.
:wq
Write buffer to file and quit editor (overrides protection).
:wq!
Quit editor without writing buffer to file (overrides protection).
q!
And that’s all about how to edit text files with Vim in Linux, feel free to post a comment below on any other uses of vim editor.
You can also check;
Compress and Uncompress Files with zip Command in Linux