How do I run a command without moving the cursor or changing the scroll position?
:let view=winsaveview() | {cmd} | call winrestview(view)
When writing Vimscript functions or mappings, commands like :substitute, gg, or :%normal will move the cursor and change the scroll position.
category:
command-line
tags:
#ex-commands
#editing
#vimscript
#navigation
How do I set breakpoints and debug Vimscript functions interactively?
:breakadd func {funcname}
Vim has a built-in debugger for Vimscript that most users never discover.
category:
config
tags:
#ex-commands
#config
#debugging
#vimscript
How do I set a different statusline for a specific buffer or window?
:setlocal statusline=%f\ %m\ %y\ [%l/%L]
The :setlocal statusline command lets you override the global statusline for a specific window.
category:
config
tags:
#config
#statusline
#ex-commands
#buffers
How do I run a Vim command in a restricted sandbox to prevent side effects?
The :sandbox command modifier executes any Ex command in a restricted environment where potentially dangerous operations are blocked.
category:
command-line
tags:
#command-line
#security
#ex-commands
#sandbox
How do I ignore whitespace changes when using Vim's diff mode?
When comparing files in Vim's diff mode, whitespace-only changes (extra spaces, changed indentation) can clutter the diff and hide meaningful edits.
category:
config
tags:
#config
#diff
#editing
#ex-commands
How do I swap two parts of a line using capture groups in a Vim substitution?
:s/\v(pattern1)(pattern2)/\2\1/
Vim's substitute command supports capture groups (also called backreferences), which let you rearrange matched portions of text.
category:
search
tags:
#search
#substitute
#regex
#editing
#ex-commands
How do I move all lines matching a pattern to the top of the file?
When working with large files, you sometimes need to reorganize content by pulling all lines matching a certain pattern to the top.
category:
command-line
tags:
#global
#move
#ex-commands
#editing
#command-line
How do I set up Vim to parse errors from my compiler or linter into the quickfix list?
Vim ships with built-in compiler plugins that configure makeprg and errorformat for popular tools.
category:
command-line
tags:
#ex-commands
#config
#editing
How do I move lines to a different position without using yank and paste?
The :move command (abbreviated :m) relocates lines to a new position in the buffer without touching any registers.
category:
editing
tags:
#ex-commands
#editing
How do I sort lines based on a specific column or pattern match rather than the whole line?
Vim's :sort command accepts a pattern that controls which part of each line is used as the sort key.
category:
command-line
tags:
#ex-commands
#editing
#formatting
How do I run a substitute command without changing my current search pattern?
:keeppattern %s/pattern/replacement/g
When you run a :substitute command, Vim updates the search register (@/) with the substitute pattern, which changes your hlsearch highlighting and affects n/N n
category:
search
tags:
#ex-commands
#search
#editing
How do I create a key mapping that behaves differently depending on context?
:nnoremap <expr> j (v:count == 0 ? 'gj' : 'j')
Expression mappings use the flag to evaluate a Vimscript expression at the time the key is pressed.
category:
config
tags:
#config
#normal-mode
#ex-commands
#navigation
How do I filter text through an external program using a motion in normal mode?
The ! operator in normal mode lets you pipe a range of text through any external program and replace it with the output.
category:
editing
tags:
#editing
#normal-mode
#ex-commands
#formatting
How do I run a command on lines near each match, not just the matching line itself?
The :g (global) command normally operates on lines that match a pattern.
category:
command-line
tags:
#ex-commands
#editing
#search
#global
How do I populate the quickfix list from the output of an external shell command?
:cexpr system('grep -rn TODO .')
The :cexpr command evaluates an expression and parses the result as quickfix entries using the current errorformat.
category:
command-line
tags:
#ex-commands
#search
#editing
#buffers
How do I add line numbers to the beginning of every line using a substitute expression?
Vim's substitute command supports expressions in the replacement string using \=.
category:
search
tags:
#search
#ex-commands
#editing
#formatting
How do I modify the contents of a register without re-recording it?
:let @a = substitute(@a, 'old', 'new', 'g')
After recording a macro or yanking text into a named register, you may need to tweak it — fix a typo in a recorded macro, change a variable name in yanked tex
category:
registers
tags:
#registers
#macros
#ex-commands
#editing
How do I search for a pattern only within specific columns of a line?
When working with columnar data like CSV files, log files, or fixed-width records, you often need to match a pattern only when it appears in a specific column r
category:
search
tags:
#search
#regex
#navigation
#ex-commands
How do I turn a visual selection of lines into a numbered list?
:s/^/\=line('.') - line("'<") + 1 . '. '/
When you need to quickly number a set of lines — such as TODO items, steps, or bullet points — you can use a visual selection combined with a substitution e
category:
visual-mode
tags:
#visual-mode
#editing
#ex-commands
#formatting
#substitute
How do I move all lines matching a pattern to the top or bottom of a file?
The :global command combined with :move lets you restructure a file by relocating all lines that match a pattern.
category:
editing
tags:
#ex-commands
#editing
#text-objects