Vim Basics for Beginners
Vim shows up on nearly every Linux system you will ever touch, often as the only editor available on a minimal server install. Its modal design, where the same keys mean different things depending on what mode you are in, is genuinely different from most other editors and trips up almost everyone the first time. This guide covers enough vim to open a file, navigate it, make changes, and save reliably, which is enough for the vast majority of real-world config file editing.
The three modes
Vim’s defining feature is that it has distinct modes, and keystrokes mean different things depending on which mode you are in.
Normal mode is where vim starts, and where keystrokes are interpreted as commands: moving the cursor, deleting text, copying, and so on. This is not where you type text.
Insert mode is where typing works like a conventional editor, inserting characters at the cursor. You enter it explicitly and leave it explicitly.
Visual mode lets you select a block of text (character by character, line by line, or as a rectangular block) to operate on, similar to click-and-drag selection in a graphical editor.
The single most important habit for a vim beginner is knowing which mode you are in. When in doubt, press Escape, which always returns you to Normal mode regardless of what mode you were in.
Opening a file
vim filename.txt
If filename.txt does not exist yet, vim opens a new empty buffer that will create the file when you save. You start in Normal mode.
Entering Insert mode
From Normal mode, several keys switch to Insert mode, each starting the cursor at a different position:
i insert before the cursor
a insert after the cursor (append)
I insert at the beginning of the line
A insert at the end of the line
o open a new line below and insert
O open a new line above and insert
Once in Insert mode, you can type normally. Press Escape when you are done to return to Normal mode.
Basic movement
Vim’s original movement keys are h, j, k, l, corresponding to left, down, up, and right. Arrow keys also work in modern vim, but the letter keys stay under your fingers without moving off the home row, which is why they are the traditional choice:
h move left
j move down
k move up
l move right
Beyond single-character movement:
w jump forward to the start of the next word
b jump backward to the start of the previous word
0 jump to the beginning of the line
$ jump to the end of the line
gg jump to the beginning of the file
G jump to the end of the file
:42 jump to line 42 (type a colon, then the line number)
Deleting and changing text
x delete the character under the cursor
dd delete (cut) the current line
dw delete from the cursor to the end of the current word
d$ delete from the cursor to the end of the line
Most delete commands can be prefixed with a number to repeat them: 3dd deletes three lines, 5x deletes five characters. This composability, a count, an operator like d, and a movement like w or $, is a core part of how vim commands are built.
Copy and paste (yank and put)
Vim calls copying “yanking” and pasting “putting”:
yy yank (copy) the current line
yw yank the current word
p put (paste) after the cursor or current line
P put (paste) before the cursor or current line
Deleted text also goes into the same register that yanked text uses, so dd followed by p effectively moves a line, cutting it from one place and pasting it elsewhere.
Undo and redo
u undo the last change
Ctrl+r redo (redo a change you just undid)
Both work from Normal mode. Press Escape first if you are in Insert mode.
Searching
/pattern search forward for pattern
?pattern search backward for pattern
n jump to the next match
N jump to the previous match
Search and replace uses a colon command with s (substitute), following syntax borrowed from sed:
:s/old/new/ replace the first match on the current line
:s/old/new/g replace every match on the current line
:%s/old/new/g replace every match in the entire file
:%s/old/new/gc same as above, but confirm each replacement
Saving and quitting
All of these are colon commands, typed from Normal mode:
:w write (save) the file
:q quit (only works if there are no unsaved changes)
:wq write and quit
:x write and quit (only writes if there are unsaved changes)
:q! quit and discard unsaved changes
ZZ shortcut for :wq (from Normal mode, no colon needed)
:q! is the one to remember when you have made changes you do not want to keep, the exclamation mark forces the action despite vim’s normal refusal to discard unsaved work.
Visual mode
Visual mode selects text so you can operate on the selection as a block, rather than moving and issuing separate commands:
v start character-wise visual selection
V start line-wise visual selection
Ctrl+v start block (column) visual selection
Once text is selected, commands like d (delete), y (yank), or > (indent) act on the entire selection rather than a single character or line.
A realistic first workflow
A common real-world task: editing a config file to change one value.
vim /etc/hosts
- Press
/and type the text you are looking for, thenEnterto jump to it. - Press
Escapeto make sure you are in Normal mode, thenito enter Insert mode where you want to type. - Make your edit.
- Press
Escapeto return to Normal mode. - Type
:wqand pressEnterto save and quit.
That five-step loop, search, insert, edit, escape, save-and-quit, covers the overwhelming majority of real vim usage for anyone who is not writing code full-time inside it.
Frequently Asked Questions
How do I exit vim?
Press Escape to make sure you are in Normal mode, then type :q and press Enter to quit if you have not made changes. If you have made changes and want to save them, type :wq and press Enter (write, then quit). If you have made changes and want to discard them without saving, type :q! and press Enter, where the exclamation mark forces the quit despite unsaved changes. This sequence, Escape then a colon command, is the answer to the frequently joked-about problem of not knowing how to leave vim.
Why can I not type text as soon as I open vim?
Vim opens in Normal mode by default, where keystrokes are interpreted as commands (movement, deletion, and so on) rather than inserted as text. To actually type text, you need to switch to Insert mode first, most commonly by pressing i (insert before the cursor) or a (append after the cursor). Once in Insert mode, typing works the way it does in any other editor. Press Escape to leave Insert mode and return to Normal mode when you are done typing.
What is the difference between vim and vi?
vi is the original Unix text editor, dating back to the 1970s, present on nearly every Unix-like system including minimal Linux installations. Vim (Vi IMproved) is a modern, extended reimplementation of vi, adding features like syntax highlighting, multiple undo levels, visual mode, plugins, and many usability improvements, while keeping vi’s core modal editing philosophy and keybindings intact. On most modern Linux distributions, the vi command is actually an alias or symlink pointing to vim, so typing vi in practice often opens vim anyway.
How do I undo and redo changes in vim?
From Normal mode, press u to undo the last change, and you can press it repeatedly to undo further back through your edit history. To redo a change you just undid, press Ctrl+r (there is no plain redo key in vim; Ctrl+r is it). Both only work from Normal mode, so if you are in Insert mode, press Escape first.
How do I search for text in vim?
From Normal mode, type / followed by the text you want to find, then press Enter. Vim jumps to the first match after the cursor and highlights matches if highlighting is enabled. Press n to jump to the next match, and N to jump to the previous match. To search backward from the cursor instead of forward, use ? followed by your search text instead of /. To search and replace, use a command like :%s/old/new/g, which replaces every occurrence of old with new across the entire file.
Is it OK to use nano instead of vim?
Yes. nano is a simpler, non-modal terminal editor that behaves more like a typical text editor, with on-screen shortcut hints and no mode-switching required, and it is a perfectly reasonable choice for quick edits, especially for beginners. The main reason to learn at least basic vim is that it is installed by default on nearly every Linux and Unix system, including minimal server images where nano is not guaranteed to be present, so knowing enough vim to open a file, make a change, and save it reliably is a useful skill even if nano remains your everyday editor of choice.