How do I join all lines in a file into one, or use a custom separator when joining?
Using \n in the pattern of :substitute matches the newline character at the end of each line, letting you join lines with any separator you choose — something
category:
editing
tags:
#editing
#ex-commands
#search
#formatting
Why doesn't \n insert a newline in a :s replacement, and what should I use instead?
In Vim substitutions, \n and \r behave differently depending on whether they appear in the search pattern or the replacement string — a common gotcha that sur
category:
search
tags:
#search
#editing
#ex-commands
#normal-mode
How do I join multiple lines together with a custom separator like a comma?
Vim's J command joins lines with a single space, but sometimes you need a custom separator like a comma, pipe, or semicolon.
category:
editing
tags:
#editing
#ex-commands
#visual-mode
#substitution
#lines
How do I collapse long runs of blank lines to a single empty line?
When files pass through formatters, generators, or repeated edits, they often accumulate noisy vertical gaps.
category:
editing
tags:
#editing
#substitute
#formatting
#cleanup
How do I paste multiline clipboard text as a comma-separated list in Insert mode?
<C-r>=substitute(getreg('+'), '\n\+', ', ', 'g')<CR>
When you paste from the system clipboard into code or config, multiline text often needs to be flattened first.
category:
registers
tags:
#registers
#insert-mode
#expression-register
#text-processing
How do I delete consecutive duplicate lines from a file using a single Ex command?
The :g command with a backreference pattern can detect and delete consecutive duplicate lines in one pass.
category:
command-line
tags:
#ex-commands
#search
#editing
#normal-mode
How do I remove accidental Enter keystrokes from a recorded macro?
:let @q = substitute(@q, '\n', '', 'g')
A common macro failure mode is accidentally hitting while recording.
category:
macros
tags:
#macros
#registers
#debugging
#vimscript
How do I run a substitute command without overwriting my last search pattern?
:keeppatterns %s/old/new/g
The :keeppatterns modifier runs any Ex command without modifying Vim's last search pattern (stored in @/).
category:
command-line
tags:
#search
#ex-commands
#command-line
#substitute
#registers
What is the difference between \n and \r in Vim substitution patterns and replacements?
One of the most confusing asymmetries in Vim's substitution syntax: \n and \r mean different things depending on whether they appear in the pattern or the repla
category:
search
tags:
#search
#ex-commands
#editing
How do I insert an actual newline in a substitution replacement, and why does backslash-n not work?
In Vim substitutions, \r in the replacement string inserts a line break, creating a new line.
category:
editing
tags:
#editing
#search
#substitute
#newline
#regex
How do I count the number of lines containing a pattern without making any replacements?
The n flag in the substitute command suppresses the actual replacement and instead reports the match count.
category:
search
tags:
#search
#ex-commands
#substitute
#normal-mode
How do I replace a character or pattern with a newline in a substitute command?
In Vim's substitute command, \r in the replacement string inserts a newline.
category:
editing
tags:
#editing
#substitute
#newline
#text-manipulation
How do I split a line by inserting a newline with the substitute command?
In Vim's substitute command, use \r (not \n) in the replacement to insert a real newline.
category:
editing
tags:
#editing
#search
#ex-commands
#substitution
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 insert a newline in the replacement string of a :s substitution?
In Vim's :substitute command, \r in the replacement string inserts a literal newline — it splits the line at that point.
category:
search
tags:
#search
#editing
#ex-commands
#normal-mode
How do I quickly resize the current window to exactly N lines tall in Vim?
The z{N} command sets the current window's height to exactly N lines and simultaneously positions the current line at the top of the window.
category:
buffers-windows
tags:
#buffers-windows
#windows
#navigation
How do I run a :g or :s command without overwriting my current search pattern?
Whenever Vim runs a command that involves searching — :g, :s, :v, or even moving the cursor with / — it overwrites the last search register (@/).
category:
search
tags:
#search
#ex-commands
#macros
How do I run a substitute command without overwriting my current search pattern?
:keeppattern %s/old/new/g
When you run a :s or :%s substitute command, Vim updates the search register (@/) with the substitution pattern.
category:
command-line
tags:
#ex-commands
#search
#editing
#registers
#substitute
How do I quickly create an Ex command range spanning the next N lines using a count before the colon?
Typing a count before : in normal mode automatically fills in a line range in the command line.
category:
command-line
tags:
#command-line
#ex-commands
#ranges
#normal-mode
How do I change the cursor shape to a thin bar in insert mode and a block in normal mode in Neovim?
:set guicursor=n-v-c:block,i-ci-ve:ver25,r-cr:hor20
Neovim's guicursor option lets you assign a distinct cursor shape and style to each mode, providing immediate visual feedback about which mode you are in.
category:
config
tags:
#config
#normal-mode
#insert-mode
#visual-mode