Vim · guide

The Vim text editor: modes, core commands and how to exit safely

Vim is a modal editor: the same keys move, delete or type depending on the mode. This guide covers the modes, the first commands to learn, a small .vimrc and the six ways out.

LineVim
Read9 min · 8 stops
DatePublished Dec 20, 2022 · updated Nov 05, 2026
Tested withVim 9.1 (Ubuntu package 9.1.0016), nano 7.2 and git 2.43.0 on Ubuntu 24.04.4 LTS
Mechanical keyboard with blue backlight, home row in focus (AI-generated image)
AI-generated image
Guide8 stations on the Vim line

Most people meet the Vim text editor by accident. A git commit without -m, a crontab -e on a server, or vi on a rescue system opens a screen with a column of tildes, and typing letters seems to do random things. Those letters are commands, because Vim starts in normal mode, and once that is clear the rest is a small grammar of operators and motions.

What the Vim text editor is

Vim stands for "Vi IMproved". Its reference manual describes an editor that "includes almost all the commands from the Unix program 'Vi' and a lot of new ones", and Bram Moolenaar wrote most of it. It runs in a terminal, so it works the same over SSH as on your laptop, and there are graphical builds such as GVim on Windows and MacVim on macOS.

Commands are typed on the letter keys, so hands stay on the home row and eyes stay on the text. Without a single plugin, the Vim text editor has syntax highlighting, macros, regular expression search, split windows and tab pages.

Installing Vim and running vimtutor

Many Linux systems already have some form of vi. Check the version first:

shell
$ vim --version | head -1
VIM - Vi IMproved 9.1 (2024 Jan 02, compiled Aug 24 2026 22:13:04)

If the command is missing, install it from your distribution's package sources: sudo apt install vim on Debian and Ubuntu, sudo dnf install vim-enhanced on Fedora, sudo pacman -S vim on Arch Linux. On macOS a vim binary is usually present already; MacVim adds the GUI.

Then run vimtutor. It opens a copy of a practice file (Version 1.7 of the tutor on Vim 9.1) whose introduction puts the time needed at 30 minutes, and since it is a copy, wrecking it costs nothing.

Desk by a window at night with a mechanical keyboard and a closed laptop (AI-generated image)
AI-generated image

Vim modes: normal, insert, visual and command-line

Vim has several modes, but four matter on day one:

ModeHow you get thereWhat keys do
NormalEsc from anywhereMove, delete, copy, paste, start other modes
Inserti, a, o, A and othersType text as in any editor
Visualv (characters), V (lines), Ctrl-v (block)Select text, then apply an operator
Command-line:, / or ?Type an Ex command or a search pattern

Normal mode is home base, and the Vim normal mode commands further down all start from it: enter insert mode to type, then press Esc the moment you stop. Of the insert commands, i inserts before the cursor, a after it, A at the end of the line, and o opens a new line below.

How to exit Vim

Knowing how to exit Vim starts with one key. Press Esc first (twice does no harm) so you are in normal mode, then pick one:

CommandWhat it does
:qQuit. Refuses if there are unsaved changes
:q!Quit and discard changes
:wqWrite the file, then quit (always writes)
:xWrite only if the buffer changed, then quit
ZZNormal-mode shortcut, the same as :x
ZQNormal-mode shortcut, the same as :q!

With several files open, :qa quits all of them and :qa! abandons every change. :q on a modified buffer gets this answer at the bottom of the screen:

output
E37: No write since last change (add ! to override)

The file is fine; Vim is holding on to edits that were never written. A related message appears with :wq in a buffer that never had a name, E32: No file name, and :w notes.txt gives it one.

Git is the usual reason to learn how to exit Vim. It takes its editor from $GIT_EDITOR, core.editor, $VISUAL and $EDITOR in that order, then falls back to a default that the git-var manual calls "usually vi". On Ubuntu 24.04 the packaged Git uses the system editor command instead, which points to nano on a fresh install. The githug exercises produce plenty of commit messages to practice on.

Which Vim normal mode commands to learn first

Vim normal mode commands follow a pattern: an operator, then a motion or text object, optionally with a count. d deletes, c changes (deletes and enters insert mode), y yanks (copies). Motions say how far: w to the next word, b back one word, 0 to column one, ^ to the first non-blank character, $ to the end of the line, gg to the first line, G to the last.

Combined, they read almost like sentences. Each of these was run in Vim 9.1 on a test buffer:

KeysEffect
dwDelete from the cursor to the start of the next word
d2wDelete two words (a count in front of the motion)
3xDelete three characters under and after the cursor
ci"Change the text inside the next pair of double quotes on the line
diwDelete the word under the cursor, leaving the spaces
u / Ctrl-rUndo / redo
.Repeat the last change

ci" tends to win people over. On the line let name = "old value"; with the cursor at the very start, ci"new followed by Esc produced let name = "new";, because Vim searched forward on the line for the quotes. Text objects come in pairs: on foo bar baz with the cursor on bar, daw left foo baz, while diw left two spaces between the words.

The arrow keys work in every mode, but the Vim trainer only counts h j k l and the word motions, which builds the habit faster than tables do.

Monitor glowing blue on a dark desk, city lights behind (AI-generated image)
AI-generated image

Search, replace and registers

/pattern searches forward and ?pattern backward, with regular expressions. n jumps to the next match, N to the previous one, and * searches for the word under the cursor.

Replacement is an Ex command. :%s/old/new/g replaces every match in the file (% is the range "all lines", g means every match on each line, not only the first). Add c to confirm each one: :%s/old/new/gc stops at every match and accepts y to substitute, n to skip, l to substitute this one and stop, and Esc to quit. On the test line print the old old line, :%s/old/new/g gave print the new new line.

Registers are Vim's clipboards. Every delete, change and yank lands in the unnamed register "", which is what p pastes. A named register is chosen with a double quote: "ayy copies the current line into register a, and "ap pastes it later even after other deletes. :reg lists what is stored where, and shows that every delete in Vim is also a cut.

vimrc basics: a minimal .vimrc

The vimrc basics fit in one line: settings live in ~/.vimrc. Plain Vim started with -u NONE uses shiftwidth=8, noexpandtab, no line numbers and no search highlighting, which is why many people write a config early. This one is short on purpose:

.vimrc
" Minimal .vimrc
set nocompatible
syntax on
filetype plugin indent on
set number
set relativenumber
set expandtab
set shiftwidth=4
set softtabstop=4
set incsearch
set hlsearch
set ignorecase
set smartcase
set backspace=indent,eol,start
set scrolloff=3
nnoremap <Space> :nohlsearch<CR>

Loaded with vim -u vimrc.test, Vim reported every option as set, filetype detection:ON plugin:ON indent:ON, and the <Space> mapping in normal mode. relativenumber makes counts easy: the gutter shows that the target is 7 lines down, so 7j gets there. ignorecase with smartcase makes /foo match Foo, while /Foo matches only Foo. Space clears the search highlight.

New settings can be tried the same way, with vim -u file, before they go into the real vimrc. Those vimrc basics carry most people through the first months. Copying someone else's long config wholesale usually brings mappings that override keys you were still learning.

Splits, tabs, and Vim vs nano

:split file opens a second window above, :vsplit file beside. Ctrl-w w moves to the next window and Ctrl-w followed by h j k l moves in that direction. Tab pages are different from tabs in a browser: each one holds a layout of windows. :tabnew opens one, gt and gT cycle through them.

Vim vs nano, compared on the versions tested:

Propertynano 7.2Vim 9.1
First useType and go; shortcuts shown at the bottomNeeds the mode idea first
Save / quitCtrl-o / Ctrl-x:w / :q and friends
Search and replaceRegex optional, asks per matchRegex, line ranges, confirm per match
Repeating editsOne recorded macro (Alt-:, Alt-;)., counts, macros in any register
On minimal systemsOften missingSome vi is usually there

In Vim vs nano, nano is the better choice for someone who edits a config file twice a year. For anyone who edits text all day, the Vim text editor wins, because each command combines with the others and repeats. The open question is what happens on the next machine: on a container image without either editor, vi may be BusyBox's small clone, where some of the Vim commands above do not exist.

Last stops · FAQ

Questions people ask

How to exit Vim without saving?

Press Esc, type :q! and press Enter; the exclamation mark drops the changes in the current buffer, and ZQ does the same from normal mode. Inside git commit there is a stronger option: :cq quits with an error code, and Git 2.43 answered with "There was a problem with the editor" and made no commit. A plain :q! on the untouched template also aborts, with "Aborting commit due to empty commit message."

What does E37 No write since last change mean?

Vim prints E37: No write since last change (add ! to override) when you type :q while the buffer has unsaved edits. Nothing is lost yet: the message is a refusal to quit, and the edits are still in the buffer. Type :w and then :q, use :wq or :x to do both in one step, or :q! to drop the changes. With several modified files, :wqa saves and quits all of them.

What is the difference between :wq and :x in Vim?

Both save and quit. :wq always writes the file, even when nothing changed, so the file's modification time is updated. :x writes only when the buffer was modified. In a test with an unchanged file dated 2020, :x left the timestamp alone while :wq set it to the current time. That matters for build tools such as make that compare timestamps.

Is Vim hard to learn?

The first hour is the hard part, because the same key does different things in different modes. vimtutor puts its own length at about 30 minutes, and by the end it has covered opening, editing, saving and quitting. Speed comes later and from a different place: combining a few operators with a few motions, so that dw, cw and yw are one idea rather than three commands to memorize.

Should you use Vim or nano on a server?

For a one-line change in a config file, nano is fine and needs no learning, since its shortcuts are listed at the bottom of the screen. Vim pays off for people who edit files every day, because counts, the . command and text objects make repeated edits cheap. On machines where only vi is installed the choice is made for you, which is reason enough to know the exit commands.

Where is the vimrc file located?

On Linux and macOS Vim reads ~/.vimrc, and if that file does not exist it tries ~/.vim/vimrc. A system-wide file comes first; on Ubuntu vim --version lists it as /etc/vim/vimrc. :echo $MYVIMRC inside Vim prints the user file that was actually loaded, which settles the question when a setting seems to be ignored.

Next stop

Where this line continues