How do I run a Vim command in a restricted sandbox to prevent side effects?
:sandbox {cmd}
The :sandbox command modifier executes any Ex command in a restricted environment where potentially dangerous operations are blocked.
:sandbox {cmd}
The :sandbox command modifier executes any Ex command in a restricted environment where potentially dangerous operations are blocked.
:g/pattern/m0
When working with large files, you sometimes need to reorganize content by pulling all lines matching a certain pattern to the top.
command-line #global #move #ex-commands #editing #command-line
:compiler gcc | :make
Vim ships with built-in compiler plugins that configure makeprg and errorformat for popular tools.
:%sort /[^,]*,/ n
Vim's :sort command accepts a pattern that controls which part of each line is used as the sort key.
:g/pattern/.-1,.+1d
The :g (global) command normally operates on lines that match a pattern.
:cexpr system('grep -rn TODO .')
The :cexpr command evaluates an expression and parses the result as quickfix entries using the current errorformat.
:keeppattern s/old/new/g
When you run a :s or :g command, Vim updates the search register (@/) with the pattern you used.
:g/pattern/z#.5
The :global command is great for finding lines matching a pattern, but by default it only shows the matching lines themselves.
:/start/,/end/d
Instead of specifying line numbers for Ex command ranges, you can use search patterns.
command-line #ex-commands #editing #search #ranges #command-line
:keeppattern {cmd}
Many Ex commands silently overwrite the search register (@/), which changes your hlsearch highlighting and n/N behavior.
:undojoin
When writing Vim scripts or running multiple Ex commands, each command normally creates a separate undo entry.
:.+1,.+3d
Vim's Ex command addresses support arithmetic offsets relative to the current line (.
command-line #ex-commands #editing #navigation #command-line
:g/^/m 0
This clever use of the :global command reverses every line in the current buffer.
command-line #editing #ex-commands #global #text-manipulation
:keepjumps
When writing scripts or running commands that move the cursor (like :g, :s, or :normal), Vim normally adds each cursor position to the jump list.
:cexpr system('grep -rn pattern .')
While :make and :grep populate the quickfix list, they are limited to their configured programs.
:g/pattern/normal A;
The :global command combined with :normal lets you execute arbitrary normal mode keystrokes on every line that matches a pattern.
command-line #global #normal-mode #editing #ex-commands #batch-editing
:cfdo %s/old/new/ge | update
When you grep across your project and want to perform a search-and-replace on every file that matched, :cfdo is the most efficient approach.
command-line #quickfix #substitute #search #ex-commands #editing
:cexpr system('command')
The :cexpr command parses any expression into the quickfix list using the current errorformat.
:g/pattern/m $
The :g (global) command combined with :m (move) lets you collect all lines matching a pattern and relocate them to a specific position in the file.
command-line #ex-commands #editing #global #search #formatting
:filter /pattern/ command
The :filter command restricts the output of another Ex command to only lines matching a given pattern.