Vim Introduction

In my opinion, Vim is the most convenient editor on Linux.

1. Installation by Operating System

Install it in your terminal:

# Ubuntu / Debian / Mint
sudo apt update
sudo apt install vim

# Fedora
sudo dnf install vim

# RHEL / Rocky / Alma / CentOS Stream
sudo dnf install vim

# Arch / Manjaro
sudo pacman -S vim

# openSUSE
sudo zypper install vim

# Alpine
sudo apk add vim

# macOS (Homebrew)
brew install vim

# Windows (winget)
winget install vim.vim

2. VIM has four common modes:

  1. Normal mode
  2. Insert mode
  3. Command mode
  4. Visual mode

2.1 Commands you can use in normal mode

i         Enter insert mode
Esc Enter command mode
/ Enter command mode
v Enter visual mode

h j k l Left Down Up Right
gg Go to start of file
G Go to end of file
0 Go to beginning of line
$ Go to end of line
x Delete one character
dd Delete one line
yy Copy one line
p Paste
u Undo
Ctrl+r Redo

2.2 Things you can do in command mode

Backspace   Return to normal mode

:w Save
:q Quit
:wq Save and quit
:q! Force quit without saving
/word Search for "word"
n Next match
N Previous match

:set number Show line numbers
:%s/a/b/g Replace across entire file

Replace

Replace on current line

:s/old/new/      Replace first "old" on current line
:s/old/new/g Replace all "old" on current line

Replace in entire file

:%s/old/new/g        Replace all in file
:%s/old/new/gc Replace all in file with confirmation
:%s/\<old\>/new/g Replace whole word "old" only

Replace in specific lines

:1,10s/old/new/g     Replace from line 1 to line 10

3. Quick Example

vim Text

If Text exists in the current directory, Vim opens it. If not, Vim creates and opens it.

After opening, Vim starts in normal mode.

Press i to enter insert mode and type content.

Then press Esc to return to normal mode.

Save and quit with:

:wq