How do I prepend // to every line using one Vim command?
:g/^/normal I// <CR>
When you need to comment a whole block quickly, :global combined with :normal is faster than recording a macro or entering Visual Block mode.
:g/^/normal I// <CR>
When you need to comment a whole block quickly, :global combined with :normal is faster than recording a macro or entering Visual Block mode.
:g/pattern/.,+2d
Inside a :global command, .
command-line #ex-commands #global #delete #editing #command-line
:[range]g/pattern/command
The :global command accepts an optional line range prefix that restricts which lines it even considers matching.
:g/^./,/^$/join
The :g/^.
command-line #command-line #editing #global #formatting #paragraphs
:g/pattern/put ='text'
Combining :global with :put = lets you insert synthesized lines of content after every line matching a pattern — without plugins or complex macros.
:g/^/t.
The command :g/^/t.
:g/./norm @q
Combining the :global command with :normal lets you run a macro on every non-blank line in one shot.
:g/pattern/d A
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
: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
:g/pattern/.-1,.+1d
The :g (global) command normally operates on lines that match a pattern.
: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
: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
: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
:g/pattern/cmd1 | cmd2
The :g (global) command can execute multiple Ex commands per matching line by chaining them with .
command-line #command-line #global #ex-commands #batch-editing #advanced
:g/pattern/y A
The :g/pattern/y A command yanks every line matching the pattern and appends it to register a.
command-line #command-line #registers #global #ex-commands #filtering
:g/pattern/normal @q
The :global command combined with :normal lets you execute a recorded macro on every line that matches a given pattern.
macros #macros #command-line #ex-commands #global #batch-editing
:g/pattern/m$
The :g (global) command combined with :m (move) relocates all matching lines to a specified destination.
command-line #command-line #ex-commands #global #editing #organization