How do I extract the directory, filename, or extension from the current file path inside a Vim command?
%:h
Vim's filename modifiers let you derive path components from the current buffer's filename directly on the command line.
%:h
Vim's filename modifiers let you derive path components from the current buffer's filename directly on the command line.
:cdo s/old/new/ | update
:cdo {cmd} executes a command at every entry in the quickfix list, visiting each location in turn.
"/
Vim stores the last search pattern in the special / register.
:keeppatterns %s/old/new/g
The :keeppatterns modifier runs any Ex command without modifying Vim's last search pattern (stored in @/).
command-line #search #ex-commands #command-line #substitute #registers
:%!
The ! operator pipes text through a shell command, replacing the selected lines with the command's output.
:keeppatterns {command}
The :keeppatterns modifier runs an Ex command — typically :s, :g, or :v — without modifying @/ (the last search pattern) or the command history.
command-line #ex-commands #search #substitution #command-line #scripting
:'<,'>!sort -t',' -k2 -n
Vim does not have a built-in multi-column sort, but you can leverage the external sort command to sort selected lines by any field.
editing #editing #sorting #ex-commands #external-commands #command-line
: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
:set wildmenu wildmode=longest:full,full
By default, Vim's command-line tab completion just cycles through options.
: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.
:.+1,.+3d
Vim's Ex command addresses support arithmetic offsets relative to the current line (.
command-line #ex-commands #editing #navigation #command-line
:cexpr system('grep -rn pattern .')
While :make and :grep populate the quickfix list, they are limited to their configured programs.
:cexpr system('command')
The :cexpr command parses any expression into the quickfix list using the current errorformat.
:/start/,/end/ {command}
Vim's range addressing lets you specify a line range using search patterns instead of explicit line numbers.
:<C-r>"
When typing an Ex command or search pattern, you often need to insert text you've already yanked or deleted.
:let @+ = @"
Vim's :let @{reg} syntax lets you read from one register and write to another.