Vim for Beginners
Learn Vim's modal editing model from scratch: modes, movement, editing, saving, search, and the essential commands every Linux user needs to know.
Before you start
- ▸Access to a Linux terminal (local or SSH)
- ▸Ability to install packages (sudo access or root)
- ▸Basic familiarity with the command line (cd, ls, cat)
Vim is installed on nearly every Linux system you'll ever touch. The first time you accidentally land in it — editing a crontab, a git commit message, or a config file — and can't figure out how to quit, it feels hostile. It isn't. Vim has a logic to it, and once that clicks, it becomes one of the fastest text editors available at the command line. This guide walks you through that logic: modes, movement, editing, and saving — everything you need to go from panicked to productive.
Installing Vim
Most systems ship with a minimal vi or vim-tiny. Install full Vim to get syntax highlighting, better defaults, and the vimtutor command.
Debian / Ubuntu
sudo apt install vim
Fedora / RHEL / Rocky
sudo dnf install vim-enhanced
Arch
sudo pacman -S vim
Understanding Modes
Vim is a modal editor. The key you press does something completely different depending on which mode you are in. This is the single most important concept to internalize.
- Normal mode — the default. Keys trigger commands (move, delete, copy). You are always one
Escpress away from here. - Insert mode — keys type text, exactly like a conventional editor.
- Visual mode — keys select text for further operations.
- Command-line mode — triggered by
:from Normal mode; runs editor commands like saving and quitting.
When Vim feels broken, press Esc twice. You are now in Normal mode and can regain control.
Opening and Quitting
Open a file by passing its path as an argument. If the file does not exist, Vim creates it on save.
vim myfile.txt
Quitting is done from Normal mode by entering Command-line mode with :.
| Command | What it does |
|---|---|
:q | Quit (fails if there are unsaved changes) |
:w | Write (save) the file |
:wq or :x | Save and quit |
:q! | Quit without saving — discard all changes |
:w filename | Save to a different file name |
Moving Around in Normal Mode
Resist reaching for the arrow keys. Vim's movement keys are on the home row and keep your hands in position. Arrow keys do work in most setups, but learning the native keys pays off immediately.
Basic Motion
| Key | Motion |
|---|---|
h | Left one character |
l | Right one character |
j | Down one line |
k | Up one line |
w | Forward to start of next word |
b | Back to start of previous word |
0 | Start of current line |
$ | End of current line |
gg | Top of file |
G | Bottom of file |
:42 | Jump to line 42 |
Motions accept a count prefix. 5j moves down five lines. 3w moves forward three words. This composability is what makes Vim fast.
Entering Insert Mode
There are several ways to switch from Normal to Insert mode, each placing your cursor differently.
| Key | Inserts at |
|---|---|
i | Before the cursor |
a | After the cursor |
I | Start of the current line |
A | End of the current line |
o | New line below, then insert |
O | New line above, then insert |
Type your text, then press Esc to return to Normal mode. You'll do this constantly — it becomes muscle memory quickly.
Editing Text in Normal Mode
Most editing in Vim happens without ever entering Insert mode. These Normal-mode commands handle the common operations.
Deleting
| Command | Effect |
|---|---|
x | Delete character under cursor |
dd | Delete entire line |
dw | Delete from cursor to end of word |
d$ | Delete from cursor to end of line |
3dd | Delete 3 lines |
Copy and Paste
Vim calls copying yanking. Deleted text also lands in the same buffer, so dd followed by p is a cut-and-paste.
| Command | Effect |
|---|---|
yy | Yank (copy) the current line |
yw | Yank from cursor to end of word |
p | Paste after cursor / below current line |
P | Paste before cursor / above current line |
Undo and Redo
# In Normal mode:
# u undo last change
# Ctrl+r redo
Replacing Text
| Command | Effect |
|---|---|
r | Replace single character under cursor |
cw | Change word (delete word, enter Insert mode) |
cc | Change entire line |
C | Change from cursor to end of line |
Search and Replace
Searching
From Normal mode, press / and type a search term, then Enter. Navigate results with n (next) and N (previous). Press ? instead of / to search backwards.
/error # search forward for "error"
?warning # search backward for "warning"
Global Substitution
Run substitutions from Command-line mode using :s (substitute). The g flag replaces all occurrences on a line; % targets the whole file.
:s/old/new/ # replace first occurrence on current line
:s/old/new/g # replace all occurrences on current line
:%s/old/new/g # replace all occurrences in the entire file
:%s/old/new/gc # same, but confirm each replacement
Visual Mode: Selecting Text
Press v from Normal mode to enter character-wise Visual mode, then move the cursor to extend the selection. Press V for line-wise selection. Once selected:
d— delete selectiony— yank (copy) selection>or<— indent or un-indent
A Useful .vimrc to Start With
Vim's defaults are deliberately minimal for compatibility. A small configuration file at ~/.vimrc makes it behave more sensibly for everyday use.
cat <<'EOF' > ~/.vimrc
set number " show line numbers
set relativenumber " relative line numbers for fast jumps
set hlsearch " highlight search results
set incsearch " search as you type
set tabstop=4
set shiftwidth=4
set expandtab " use spaces instead of tabs
set autoindent
syntax on
EOF
Verifying Your Skills
Run the built-in interactive tutorial. It takes about 30 minutes and covers everything in this guide with hands-on practice.
vimtutor
After completing it, open a real file, make a change, save it with :wq, and verify the change with cat.
vim /tmp/test.txt
# make edits, then :wq
cat /tmp/test.txt
Troubleshooting
- Vim opens in the wrong mode or looks garbled: Your terminal may have sent unexpected keystrokes before Vim loaded. Press
Esctwice, thenurepeatedly to undo any accidental changes. - Swap file warning on open: Vim creates a hidden
.swpfile during editing. If you see a swap file warning, a previous session crashed. PressRto recover, orDto delete the swap file if you don't need the recovery. - Can't save — "E212: Can't open file for writing": You opened a file you don't own. Either fix permissions first, or if it's a system file, use
:w !sudo tee %to write via sudo without reopening. - Search highlight won't go away: Run
:noh(no highlight) to clear it until the next search.
Frequently asked questions
- How do I exit Vim if I'm completely lost?
- Press Esc twice to ensure you're in Normal mode, then type :q! and press Enter. The exclamation mark forces a quit without saving any changes.
- What's the difference between Vim and Vi?
- Vi is the original 1976 editor. Vim (Vi IMproved) is its modern successor with syntax highlighting, unlimited undo, split windows, plugins, and many other additions. On most Linux systems, `vi` is now a symlink to Vim or a minimal Vim build.
- How do I save a file I opened without sudo when I need root permissions?
- From Normal mode, run :w !sudo tee % and press Enter. This pipes the buffer through sudo tee to overwrite the file without relaunching Vim.
- How do I enable line numbers?
- Run :set number from Normal mode for the current session, or add `set number` to your ~/.vimrc to make it permanent. Use :set relativenumber for relative line numbers instead.
- Should I learn Vim or switch to Neovim?
- Learn Vim first — all the core keybindings, modes, and commands are identical in Neovim. Once you're comfortable, Neovim adds Lua-based configuration and a stronger plugin ecosystem, and the transition is trivial.
Related guides
Bash Arrays and Associative Arrays
Master bash indexed and associative arrays: declaration, element access, looping, mapfile, namerefs, and practical patterns for real scripting work.
Bash Functions and Variable Scoping
Master Bash function scoping with local variables, source-based libraries, correct use of return codes, and array passing techniques including namerefs.
Bash Loops: for, while and until
Learn all three Bash loop types — for, while, and until — with practical, copy-paste examples covering file iteration, counting, polling, and safe line reading.
Bash Scripting for Beginners
Learn Bash scripting from scratch: shebang lines, variables, conditionals, loops, and arguments, plus a real backup script to tie it all together.