Skip to main content

Command-line Mode

Command-line mode is the : prompt at the bottom of the screen. It is how you run Ex commands with arguments, ranges, and flags — saving files, splitting windows, substituting text, and filtering through shell tools. Search (/, ?), shell (!), and the expression register (=) all share the same command line.

Core Idea

Normal mode is for single-key commands. Command-line mode is for full commands with arguments — the equivalent of a shell prompt inside Neovim.

The Command-line Prefixes​

PrefixOpensExample
:Ex command:w
/Forward search/error
?Backward search?error
!Shell command / filter:!ls
=Expression registerCtrl+r =

Entering and Leaving​

KeyAction
:Open the Ex command line
/Search forward
?Search backward
EnterExecute the command
Esc / Ctrl+[Cancel and return to Normal mode
Ctrl+cCancel (force)

Editing the Command Line​

The command line is a real editing buffer — you can fix typos before running it:

KeyAction
Backspace / Ctrl+hDelete previous character
Ctrl+wDelete previous word
Ctrl+uDelete to start of line
Ctrl+b / Ctrl+eMove cursor to start / end
Left / RightMove cursor
Up / DownPrevious / next history entry
Ctrl+p / Ctrl+nPrevious / next history entry
Ctrl+r {reg}Insert register contents
Tab / Shift+TabComplete forward / backward
Ctrl+dShow the completion list
Ctrl+v / Ctrl+qInsert a literal character
Ctrl+fOpen the command-line window (history editing)

Command History and the Command-line Window​

Up / Down → browse previous commands
q: → open the command-line window (editable history buffer)
q/ / q? → command-line window for search history
:history → list command history
@: → repeat the last Ex command
@@ → repeat the last @{register}

Inside the command-line window you use Normal mode to navigate and edit history, then press Enter on a line to execute it. This is the easiest way to fix a long command you just mistyped.

Ex Command Categories​

Files​

:w → write (save)
:q → quit
:wq / :x → write and quit
:q! → quit without saving
:wqa → write and quit all windows
:qa! → quit all, discarding changes
:e file → edit a file
:saveas file → save as
:r file → read/insert a file below the cursor
:w !sudo tee % → write via sudo (edit root-owned files)

Buffers, Windows, and Tabs​

:ls / :buffers → list open buffers
:bn / :bp → next / previous buffer
:bd → close (delete) a buffer
:sp / :vs → split / vertical split
:tabnew → open a new tab
:tabnext / :tabprevious
:only → close all other windows

Editing​

:d → delete line
:y → yank line
:put → paste below the cursor
:m +1 → move line down one
:m 0 → move line to the top
:t 5 → copy line to after line 5
:t$ → copy line to the end of the file
:normal! dd → run Normal-mode keys (great for scripting)
:undo / :redo
:join / :j
:sort → sort lines
:sort n → sort numerically

Search and Replace​

:s/old/new/ → replace the first match on the current line
:s/old/new/g → replace all matches on the current line
:%s/old/new/g → replace in the whole file
:%s/old/new/gc → with a confirmation prompt
:1,10s/old/new/g → lines 1-10
:'<,'>s/old/new/g → the visual selection
:s/old/new/i → case-insensitive

Read the ruler — the range decides which lines the command touches:

1 old value
2 old value ← :s/old/new/g (current line only)
3 old value
4 old value

1 new value
2 new value ┐
3 new value │ :%s/old/new/g (% = whole file)
4 new value ┘

Options​

:set number / :set nonumber
:set relativenumber
:set ignorecase / :set noignorecase
:set option? → show the current value
:set option=value
:setlocal ... → buffer/window-local

Help and Information​

:help {topic}
:checkhealth
:messages
:registers
:marks
:jumps
:changes

Shell and External Commands​

:!cmd → run a shell command
:r !cmd → insert the output of a command below the cursor
:!ls
:%!sort → filter the whole file through sort
:terminal → open a terminal buffer

Ranges​

Most Ex commands accept a range written before the command:

:1,5d → delete lines 1-5
:.d → delete the current line
:$d → delete the last line
:%d → delete the whole file
:1,$s/a/b/g → substitute in the whole file
:'a,'b ... → from mark a to mark b
:'<,'> ... → the visual selection
:/start/,/end/d → from the line matching start to the line matching end

Read the ruler — the range covers every line between the two addresses:

1 alpha
2 beta ← :1,2d deletes lines 1-2
3 gamma
4 delta
5 epsilon
1 alpha
2 beta
3 gamma ┐
4 delta │ :1,$s/a/b/g (from line 1 to the last line)
5 epsilon ┘

The Global Command​

:g runs a command on every line matching a pattern; :v runs it on every line that does not match:

:g/pattern/d → delete all matching lines
:g/^$/d → delete all blank lines
:g/TODO/normal! dd → run a Normal command on every match
:g/pattern/s/foo/bar/g → substitute only on matching lines
:v/pattern/d → delete all non-matching lines

Substitution Flags​

g → all matches on the line (global)
c → confirm each replacement
i → case-insensitive
e → suppress "no match" errors
& → reuse the flags from the previous :s

Inside the pattern and replacement:

& → the whole matched text in the replacement
\1 → capture group 1
\v → "very magic" regex (fewer escapes)
\s \+ → whitespace and "one or more" in default magic

:%s/\s\+$// → trim trailing whitespace
:%s/\v(\w+)\s+(\w+)/\2 \1/g → swap two words

The Expression Register​

:echo 2+2 → print an expression
:put =2+2 → insert the result below the cursor
Ctrl+r = ... → insert an expression result while typing

Common Pitfalls​

PitfallFix
:q refuses because changes are unsaved:w then :q, or :q! to discard
Can't save a root-owned file:w !sudo tee %
:s changed the wrong occurrencepress u to undo, then use :s/old/new/gc
A long command is misspelledpress Up to recall it, edit, then Enter
Re-running the same command repeatedly@: repeats the last Ex command

Practice​

Practice File

Open the step-by-step drills: practice-folder/nvim/02-modal-editing/modes-practice.md (Drill 8) (also available via the shell alias n02-modes).

StepCommand / keysWhat it does
1:wSave the current file
2:e /tmp/scratch.txtOpen a fresh scratch buffer
3ihello from scratch + EscType some text
4:wSave the scratch file
5:qClose it and return
6:vsVertical split of the same file
7Ctrl+w wSwitch between windows
8:spHorizontal split
9Ctrl+w qClose the current window

In LazyVim​

LazyVim Context

Command-line mode is identical in LazyVim. LazyVim additionally provides fuzzy alternatives to many Ex commands through Telescope, such as &lt;leader&gt;fb for buffers or &lt;leader&gt;/ for searching, plus &lt;leader&gt;sg for project-wide search and replace via grug-far.nvim.

AspectVanilla NeovimLazyVim
Ex commands:w, :q, :%sSame
Find files / buffers:e, :lsTelescope pickers
Project search/replace:grep, :vimgrep&lt;leader&gt;sg (grug-far)

What's Next​