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 add more steps to an existing macro without re-recording it from scratch?
When you record a macro into register q with qq.
category:
macros
tags:
#macros
#registers
#recording
#editing
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 switch between visual mode and select mode in Vim?
Vim has a lesser-known select mode that behaves like selection in typical GUI editors: any typed character replaces the selection.
category:
visual-mode
tags:
#visual-mode
#select-mode
#editing
#normal-mode
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 expand or shrink a visual selection based on the syntax tree using Treesitter?
Neovim's nvim-treesitter plugin provides incremental selection based on the abstract syntax tree (AST) of your code.
category:
plugins
tags:
#visual-mode
#plugins
#editing
#navigation
How do I get enhanced tab completion with a visual menu for commands and file paths?
:set wildmenu wildmode=longest:full,full
By default, Vim's command-line tab completion just cycles through options.
category:
config
tags:
#config
#command-line
#completion
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