1.4 β Command-Line Mode β Ex Commands
1.4 β Command-Line Mode β Ex Commands#
Command-line mode gives you access to Vimβs powerful Ex commands. These commands handle file operations, search and replace, settings, and much more.
What is Command-Line Mode?#
Command-line mode (also called Ex mode or Cmdline mode) is activated by pressing :, /, or ? from Normal mode. A command line appears at the bottom of the screen where you type commands.
| Entry Key | Purpose | Prompt |
|---|---|---|
: | Ex commands | : |
/ | Search forward | / |
? | Search backward | ? |
Essential Ex Commands#
File Operations#
| Command | Action |
|---|---|
:w | Write (save) file |
:w filename | Write to filename |
:q | Quit |
:q! | Quit without saving |
:wq or :x | Write and quit |
:e filename | Edit (open) file |
:e! | Reload file, discard changes |
:r filename | Read file into buffer |
:r !command | Read command output into buffer |
Navigation#
| Command | Action |
|---|---|
:10 | Go to line 10 |
:$ | Go to last line |
:. | Current line |
:% | All lines (alias for 1,$) |
Buffer and Window Commands#
| Command | Action |
|---|---|
:ls or :buffers | List buffers |
:b N | Switch to buffer N |
:bn | Next buffer |
:bp | Previous buffer |
:bd | Delete (close) buffer |
:sp | Horizontal split |
:vs | Vertical split |
:close | Close current window |
:only | Close all other windows |
Settings#
| Command | Action |
|---|---|
:set option | Enable option |
:set nooption | Disable option |
:set option? | Show option value |
:set option=value | Set option value |
Common settings:
:set number " Show line numbers
:set relativenumber " Show relative line numbers
:set ignorecase " Case-insensitive search
:set smartcase " Case-sensitive if uppercase present
:set hlsearch " Highlight search matches
:set nohlsearch " Disable highlight
:set expandtab " Use spaces instead of tabs
:set tabstop=4 " Tab width
:set shiftwidth=4 " Indent width
Search Commands#
Basic Search#
| Command | Action |
|---|---|
/pattern | Search forward for pattern |
?pattern | Search backward for pattern |
n | Repeat search (same direction) |
N | Repeat search (opposite direction) |
* | Search forward for word under cursor |
# | Search backward for word under cursor |
Search Options#
/pattern\c " Case-insensitive search
/pattern\C " Case-sensitive search
/\vpattern " "Very magic" (regex without escaping)
Clear Search Highlighting#
:noh " Clear highlight (short for :nohlsearch)
Substitution (Search and Replace)#
The substitute command is one of Vimβs most powerful features.
Basic Syntax#
:[range]s/pattern/replacement/[flags]
Ranges#
| Range | Meaning |
|---|---|
| None | Current line |
% | All lines |
1,10 | Lines 1-10 |
.,$ | Current line to end |
'<,'> | Visual selection (auto-inserted) |
Flags#
| Flag | Meaning |
|---|---|
g | Global (all occurrences on line) |
c | Confirm each substitution |
i | Case-insensitive |
I | Case-sensitive |
n | Count matches, donβt substitute |
Examples#
:s/old/new/ " First occurrence on current line
:s/old/new/g " All occurrences on current line
:%s/old/new/g " All occurrences in file
:%s/old/new/gc " All occurrences, confirm each
:5,10s/old/new/g " All occurrences on lines 5-10
:'<,'>s/old/new/g " All occurrences in selection
Special Replacement Patterns#
| Pattern | Meaning |
|---|---|
& | Entire matched pattern |
\1, \2 | Captured groups |
\u | Uppercase next character |
\l | Lowercase next character |
\U | Uppercase until \E |
\L | Lowercase until \E |
Example: Swap first and last name:
:%s/\(\w\+\) \(\w\+\)/\2, \1/g
" "John Doe" becomes "Doe, John"
The Global Command#
The :g command executes an Ex command on all lines matching a pattern.
Syntax#
:g/pattern/command
Examples#
:g/TODO/d " Delete all lines containing "TODO"
:g/^$/d " Delete all blank lines
:g/pattern/p " Print all matching lines
:g!/pattern/d " Delete lines NOT matching (or :v/pattern/d)
:g/error/t$ " Copy error lines to end of file
:g/function/normal A; " Append semicolon to lines containing "function"
Command-Line Editing#
While typing in command-line mode:
| Key | Action |
|---|---|
Esc or Ctrl-c | Cancel and return to Normal mode |
Enter | Execute command |
Ctrl-b | Move to beginning of line |
Ctrl-e | Move to end of line |
Ctrl-w | Delete word before cursor |
Ctrl-u | Delete to beginning of line |
Up / Ctrl-p | Previous command in history |
Down / Ctrl-n | Next command in history |
Ctrl-r Ctrl-w | Insert word under cursor |
Command History#
Vim remembers your commands. Access history with:
q:β Open command history windowq/β Open search history window:thenUp/Downβ Cycle through command history
Filter history by typing first characters:
:e <Up> " Cycle through commands starting with :e
External Commands#
Run shell commands from Vim:
| Command | Action |
|---|---|
:!command | Run command, show output |
:r !command | Insert command output |
:.!command | Filter line through command |
:%!command | Filter buffer through command |
Examples:
:!ls " List files
:r !date " Insert current date
:.!sort " Sort current line (not useful)
:%!sort " Sort entire file
:'<,'>!sort " Sort selected lines
Practice Exercises#
Exercise 1: File Operations#
- Open Vim:
vim - Type some text
:w test.txtto save:e test.txtto reopen- Make changes,
:wto save :qto quit
Exercise 2: Search#
- Open a file with text
/wordto search for βwordβ- Press
nto find next occurrence - Press
Nto find previous :nohto clear highlighting
Exercise 3: Substitution#
- Create text: βHello World Hello World Helloβ
:s/Hello/Hi/β replaces first Hellouto undo:s/Hello/Hi/gβ replaces all on lineuto undo:%s/Hello/Hi/gcβ all in file, with confirmation
Exercise 4: Global Command#
- Create a file with various lines
- Add βTODOβ to some lines
:g/TODO/pβ print TODO lines:g/TODO/dβ delete TODO linesuto undo
Exercise 5: External Commands#
:r !dateβ insert current date:r !lsβ insert file listing- Select lines with
V :'<,'>!sortβ sort selected lines
Summary#
| Category | Commands |
|---|---|
| File Ops | :w, :q, :e, :wq |
| Search | /pattern, ?pattern, n, N |
| Substitute | :%s/old/new/g |
| Global | :g/pattern/command |
| Settings | :set option |
| External | :!command |
Command-line mode transforms Vim from a text editor into a text processing powerhouse. Master these commands to handle complex editing tasks efficiently.