The vim command
The vim command opens a file in Vim, a text editor that runs in the terminal…
The vim command opens a file in Vim, a text editor that runs in the terminal: press i to type, Esc to stop, then :wq and Enter to save and quit.
Taught in Intermediate, lesson 28; more in Advanced and Ninja
A practice shell in your browser that behaves like bash on Linux for these commands. Nothing you type leaves the page.
Usage
vim [OPTION]... [FILE]...
Edit text files in the terminal (a practice subset of Vim). Opens the files in Vim, which takes over the terminal until you quit. This is a practice subset of Vim 9 that runs in your browser on the practice shell’s files, so cat shows what you save. Vim starts in normal mode, where keys are commands. Press i to type text, Esc to stop, :w and Enter to save, :q to quit (E37 means there are unsaved changes: :wq saves and quits, :q! quits without saving). It has normal, insert, visual (v, V), replace (R) and command-line modes; motions h j k l w b e W B E 0 ^ $ gg G f t F T ; , % { } H M L; operators d c y > < g~ gu gU with counts, motions and text objects (iw aw i" a" i( a( i{ ip ap it at); x X r s S J ~ p P u Ctrl-r . Ctrl-a Ctrl-x; / ? n N * # with highlighting (:noh clears it); :s and :%s with the g, c, i, n and e flags; ranges (:2,5d, :g/pat/d, :v, :m, :t, :normal, :sort); registers ("a, "A, "0 to "9, "-, "_, :reg); macros (q, @, @@); marks (m, ' and `, :marks); splits (:split, :vsplit, Ctrl-w h j k l w, :only, :q closes a window); buffers (:e, :ls, :bn, :b, Ctrl-^) and :set (number, relativenumber, hlsearch, ignorecase, tabstop, shiftwidth, expandtab, wrap, list and more). It reads ~/.vimrc (set, map and let mapleader lines). It doesn’t have plugins, syntax colours, folds, tabs, block selection (Ctrl-v), :help or :terminal; anything missing says so in the command line. Your browser keeps Ctrl+W for closing tabs (except on a Mac), so for windows use :wincmd w (or h, j, k, l), or the on-screen Ctrl key.
How to use vim
Open a file
vim notes.txt opens the file, or a new one if it doesn’t exist yet. Vim fills the terminal and starts in normal mode, where keys are commands rather than text.
vim notes.txtType, save and quit
Press i to start typing (the bottom line says -- INSERT --) and Esc to stop. Then type :wq and press Enter to save and quit. :w saves and stays in Vim.
vim todo.txtQuit without saving: :q!
:q won’t leave a file with unsaved changes; it says E37: No write since last change (add ! to override). :q! quits and throws the changes away.
vim fruit.txtStart on a line or at a word
+3 opens the file on line 3, and +/word on the first line with that word.
vim +3 notes.txtvim +/dentist notes.txtTwo files side by side: vim -O
Each file gets its own window (-o stacks them instead). Inside Vim, :wincmd w moves to the other window and :q closes one.
vim -O fruit.txt data.csvRead only: vim -R
Vim won’t save until you insist with :w!, which keeps a log or a config file safe while you look.
vim -R app.logOptions
| +N | start on line N (+ alone: the last line) |
|---|---|
| +/TEXT | start at the first match of TEXT |
| -c CMD | run an Ex command after opening, like +CMD |
| -R | read-only: saving needs :w! |
| -o, -O | open the files in split windows, stacked or side by side |
| --clean | don’t read ~/.vimrc |
More examples
vim -O a.txt b.txtQuestions about vim
How do I exit Vim?
Press Esc so you are in normal mode, then type :q and press Enter. If Vim says E37: No write since last change, type :wq to save and quit, or :q! to quit without saving.
What does E37: No write since last change mean?
The file has changes you haven’t saved, so :q won’t quit. Save and quit with :wq, or add ! to throw the changes away: :q!.
What is the difference between vi, vim and nvim?
vi is the original editor, and on most Linux systems the vi command starts Vim (Vi IMproved). nvim starts Neovim, a newer editor built from Vim with the same keys and its configuration in Lua.
Is the Vim on this page the real Vim?
It is a practice subset of Vim that runs in your browser on the practice files: the modes, motions, operators, search and replace, registers, macros and splits work as in Vim, and anything it doesn’t have says so.
Learn it step by step
- Lesson 28Vim, part 1: open a file, type, save
- Lesson 38Vim: move by word, line and search
- Lesson 39Vim operators: dw, cw, ci" and the dot command
- Lesson 40Search and replace in Vim:
:sand:%s - Lesson 54Vim macros: record an edit once, play it a hundred times
- Lesson 64Something broke: the web server won’t start
Related commands
Every everyday command on one page, to print or keep open: the Linux commands cheat sheet.