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 sort lines by the text that matches a regex pattern rather than the text after it?
The :sort r /pattern/ command sorts lines using the matched portion of the regex as the sort key.
category:
command-line
tags:
#ex-commands
#search
#editing
#normal-mode
How do I match a pattern only when preceded or followed by another pattern using zero-width assertions?
Vim's regex engine supports zero-width lookahead and lookbehind assertions — \@= and \@<= — which let you match text based on surrounding context without in
category:
search
tags:
#search
#regex
#advanced
#patterns
#substitution
How do I list all lines matching a pattern across the current file and its includes?
The :ilist command searches for a pattern not only in the current buffer but also in files referenced by #include directives (or whatever 'include' is set to).
category:
search
tags:
#search
#ex-commands
#navigation
#editing
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
How do I delete each line matching a pattern along with a fixed number of lines that follow it?
Inside a :global command, .
category:
command-line
tags:
#ex-commands
#global
#delete
#editing
#command-line
How do I access and reuse the last search pattern?
The / register contains the most recent search pattern.
category:
registers
tags:
#registers
#search
How do I search backward for text in Vim?
The ? command searches backward from the cursor position.
category:
search
tags:
#search
#normal-mode
How do I collect all lines matching a pattern into a register without leaving them in place?
Using :g/pattern/d A you can sweep through the entire buffer and extract every line that matches a pattern into register a, removing them from the buffer in the
category:
registers
tags:
#registers
#ex-commands
#search
#editing
#global
How do I land on a specific position relative to a search match?
Search offsets let you place the cursor at a specific position relative to the match.
category:
navigation
tags:
#navigation
#search
#normal-mode
How do I find all files matching a pattern across all directories in Vim's runtimepath?
globpath(&rtp, 'pattern')
globpath() is a Vimscript function that searches all directories in a given path list for files matching a glob pattern.
category:
config
tags:
#ex-commands
#normal-mode
How do I run a normal mode command on every line matching a pattern?
The :g/pattern/normal {commands} command executes normal mode keystrokes on every line in the file that matches the given pattern.
category:
command-line
tags:
#ex-commands
#command-line
#editing
#search
How do I highlight a pattern without executing a search or moving the cursor?
Assigning a string directly to the search register @/ with :let causes Vim to highlight all matches (if hlsearch is enabled) without performing a search or movi
category:
registers
tags:
#registers
#search
#normal-mode
#ex-commands
How do I search for a pattern across all files in a project using Vim's built-in grep?
While external tools like grep or ripgrep are fast, Vim's built-in :vimgrep has a key advantage: it populates the quickfix list directly, so you can jump betwee
category:
search
tags:
#search
#quickfix
#ex-commands
#navigation
How do I suppress the 'Pattern not found' error when a substitution has no match?
The e flag in Vim's :substitute command silently ignores the "E486: Pattern not found" error when the pattern does not match anything.
category:
search
tags:
#search
#substitute
#ex-commands
#macros
#editing
How do I use an external grep program from within Vim?
The :grep command runs an external grep tool and loads the results into Vim's quickfix list.
category:
search
tags:
#search
#ex-commands
How do I replace text with a newline in a substitute command?
In the replacement part of :s, \r inserts a newline.
category:
search
tags:
#search
#editing
#ex-commands
How do I search for a pattern across all files in my project without leaving Vim?
:vimgrep /pattern/ searches recursively through all files in the current working directory tree using Vim's own regex engine, populating the quickfix list with
category:
search
tags:
#search
#quickfix
#ex-commands
#navigation
How do I highlight a custom pattern with a priority so it wins over other overlapping highlights?
matchadd('Group', 'pattern', priority)
matchadd() accepts an optional third argument: a priority integer that controls which match wins when two patterns cover the same text.
category:
config
tags:
#config
#search
#normal-mode
How do I search forward for text in Vim?
The / command initiates a forward search in the file.
category:
search
tags:
#search
#normal-mode