Vim Tips Wiki
Explore
Main Page
All Pages
Community
Interactive Maps
Community portal
To do
FANDOM
Fan Central
BETA
Games
Anime
Movies
TV
Video
Wikis
Explore Wikis
Community Central
Start a Wiki
Don't have an account?
Register
Sign In
Sign In
Register
Vim Tips Wiki
1,649
pages
Explore
Main Page
All Pages
Community
Interactive Maps
Community portal
To do
Editing
Power of g
(section)
Back to page
Edit
Edit source
View history
Talk (0)
We recommend that you
log in
before editing. This will allow other users to leave you a message about your edit, and will let you track edits via your
Watchlist
.
Creating an account
is quick and free.
Anti-spam check. Do
not
fill this in!
==Examples== Display context (5 lines) for all occurrences of a pattern. <pre> :g/pattern/z#.5 " Same, but with some beautification. :g/pattern/z#.5|echo "==========" </pre> Delete all lines matching a pattern. <pre> :g/pattern/d </pre> Delete all lines that do '''not''' match a pattern. The commands shown are equivalent (<code>v</code> is "in<u>v</u>erse"). <pre> :g!/pattern/d :v/pattern/d </pre> Delete all blank lines (<code>^</code> is start of line; <code>\s*</code> is zero or more whitespace characters; <code>$</code> is end of line) <pre> :g/^\s*$/d </pre> Double space the file (<code>^</code> is start of line which matches each line). <pre> :g/^/pu =\"\n\" " Alternative (:put inserts nothing from the blackhole register) :g/^/pu _ </pre> Copy all lines matching a pattern to end of file. <pre> :g/pattern/t$ </pre> Move all lines matching a pattern to end of file. <pre> :g/pattern/m$ </pre> Copy all lines matching a pattern to register 'a'. <pre> qaq:g/pattern/y A </pre> :''Explanation'' <code>qaq</code> is a trick to clear register <code>a</code> (<code>qa</code> starts recording a macro to register <code>a</code>, then <code>q</code> stops recording, leaving <code>a</code> empty). <code>y A</code> is an Ex command ({{help|:y}}). It yanks the current line into register <code>A</code> (append to register <code>a</code>). Increment each number at the start of a line, from the current line to end-of-file, by one (the exclamation mark in <code>:normal!</code> means this will work even if Ctrl-A has been mapped to perform a function other than its default of incrementing a number). <pre> :.,$g/^\d/exe "normal! \<C-A>" </pre> Comment lines containing "DEBUG" statements in a C program. <pre> " using :normal g/^\s*DEBUG/exe "norm! I/* \<Esc>A */\<Esc>" " using :substituting g/^\s*DEBUG/s!.*!/* & */! </pre> Reverse lookup for records (say an address book, with Name at start-of-line and fields after a space). <pre> :g/pattern/?^\w?p "if only name is interesting :g/pattern/ka|?^\w?p|'ap "if name and the lookup-line is interesting :g/pattern/?^\w?|+,/^[^ ]/-1p "if entire record is interesting </pre> :''Explanation'' See {{help|:range}} for the meaning of the constructs in the <code><nowiki>[cmd]</nowiki></code> portion of the <code>:g</code> commands. Reverse a file (just to show the ''power of g''). <pre> :g/^/m0 </pre> :''Explanation'' According to {{help|multi-repeat}}, <code>:g</code> and its cousin <code>:v</code> work in a two-pass manner. The first pass of <code>:g</code> marks every line matching <code>{pattern}</code>, while the second pass (apparently performed starting at the file's beginning and proceeding to the end) performs the <code><nowiki>[cmd]</nowiki></code>. The above use of <code>:g</code> takes advantage of the order the lines are processed in (which is probably okay, though probably not technically guaranteed). It works by first marking every line, then moving the first marked line to the top of the file, then moving the second to the top of the file (above the line moved previously), then the third marked line (again above the previously moved line), and so on until the last line in the file is moved to the top, effectively reversing the file. Note that if <code>:g</code> processed lines in any order other than from top to bottom, this command would not work. Add text to the end of a line that begins with a certain string. <pre> :g/^pattern/s/$/mytext </pre>
Summary:
Please note that all contributions to the Vim Tips Wiki are considered to be released under the CC-BY-SA
Cancel
Editing help
(opens in new window)
Follow on IG
TikTok
Join Fan Lab