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.
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
| Prefix | Opens | Example |
|---|---|---|
: | Ex command | :w |
/ | Forward search | /error |
? | Backward search | ?error |
! | Shell command / filter | :!ls |
= | Expression register | Ctrl+r = |
Entering and Leaving
| Key | Action |
|---|---|
: | Open the Ex command line |
/ | Search forward |
? | Search backward |
Enter | Execute the command |
Esc / Ctrl+[ | Cancel and return to Normal mode |
Ctrl+c | Cancel (force) |
Editing the Command Line
The command line is a real editing buffer — you can fix typos before running it:
| Key | Action |
|---|---|
Backspace / Ctrl+h | Delete previous character |
Ctrl+w | Delete previous word |
Ctrl+u | Delete to start of line |
Ctrl+b / Ctrl+e | Move cursor to start / end |
Left / Right | Move cursor |
Up / Down | Previous / next history entry |
Ctrl+p / Ctrl+n | Previous / next history entry |
Ctrl+r {reg} | Insert register contents |
Tab / Shift+Tab | Complete forward / backward |
Ctrl+d | Show the completion list |
Ctrl+v / Ctrl+q | Insert a literal character |
Ctrl+f | Open 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
| Pitfall | Fix |
|---|---|
: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 occurrence | press u to undo, then use :s/old/new/gc |
| A long command is misspelled | press Up to recall it, edit, then Enter |
| Re-running the same command repeatedly | @: repeats the last Ex command |
Practice
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).
| Step | Command / keys | What it does |
|---|---|---|
| 1 | :w | Save the current file |
| 2 | :e /tmp/scratch.txt | Open a fresh scratch buffer |
| 3 | ihello from scratch + Esc | Type some text |
| 4 | :w | Save the scratch file |
| 5 | :q | Close it and return |
| 6 | :vs | Vertical split of the same file |
| 7 | Ctrl+w w | Switch between windows |
| 8 | :sp | Horizontal split |
| 9 | Ctrl+w q | Close the current window |
In LazyVim
Command-line mode is identical in LazyVim. LazyVim additionally provides fuzzy alternatives to many Ex commands through Telescope, such as <leader>fb for buffers or <leader>/ for searching, plus <leader>sg for project-wide search and replace via grug-far.nvim.
| Aspect | Vanilla Neovim | LazyVim |
|---|---|---|
| Ex commands | :w, :q, :%s | Same |
| Find files / buffers | :e, :ls | Telescope pickers |
| Project search/replace | :grep, :vimgrep | <leader>sg (grug-far) |