$linuxjunkies
>

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.

BeginnerUbuntuDebianFedoraArch8 min readUpdated June 7, 2026

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 Esc press 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 :.

CommandWhat it does
:qQuit (fails if there are unsaved changes)
:wWrite (save) the file
:wq or :xSave and quit
:q!Quit without saving — discard all changes
:w filenameSave 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

KeyMotion
hLeft one character
lRight one character
jDown one line
kUp one line
wForward to start of next word
bBack to start of previous word
0Start of current line
$End of current line
ggTop of file
GBottom of file
:42Jump 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.

KeyInserts at
iBefore the cursor
aAfter the cursor
IStart of the current line
AEnd of the current line
oNew line below, then insert
ONew 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

CommandEffect
xDelete character under cursor
ddDelete entire line
dwDelete from cursor to end of word
d$Delete from cursor to end of line
3ddDelete 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.

CommandEffect
yyYank (copy) the current line
ywYank from cursor to end of word
pPaste after cursor / below current line
PPaste before cursor / above current line

Undo and Redo

# In Normal mode:
# u        undo last change
# Ctrl+r   redo

Replacing Text

CommandEffect
rReplace single character under cursor
cwChange word (delete word, enter Insert mode)
ccChange entire line
CChange 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 selection
  • y — 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 Esc twice, then u repeatedly to undo any accidental changes.
  • Swap file warning on open: Vim creates a hidden .swp file during editing. If you see a swap file warning, a previous session crashed. Press R to recover, or D to 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.
tested on:Ubuntu 24.04Fedora 40Arch rollingDebian 12

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