21 votes
Accepted

Is there a simple equivalent of :g/PATTERN/m0 that doesn't reverse the matched lines?

Instead of moving the target lines up (and reversing them), move all the non-target lines down. Since lines are processed top-to-bottom, the order of non-target lines will be preserved. Also, the ...
Mass's user avatar
  • 13.8k
17 votes
Accepted

How to join all lines together which matching pattern?

Possible explanation of the problem I think the reason why :g/;/j doesn't work is because the :g command operates with a 2-pass algorithm: during the first pass it marks the lines containing the ...
saginaw's user avatar
  • 6,666
16 votes
Accepted

Delete all consecutive duplicates

I think the following command should work : :%s/^\(.*\)\(\n\1\)\+$/\1/ Explanation : We use the substitution command on the whole file to change pattern into string : :%s/pattern/string/ Here ...
saginaw's user avatar
  • 6,666
12 votes
Accepted

How to replace content between two patterns from the file?

A substitution can be used to replace a pattern with the result of an expression like this: :keeppatterns %s;<inputs>\zs\_.\{-}\ze</inputs>;\=insert(readfile('test.txt'), '') See :help ...
djjcast's user avatar
  • 901
12 votes

Delete all consecutive duplicates

Try the following: :%s;\v^(.*)(\n\1)+$;\1; As with saginaw's answer, this uses Vim's :substitute command. However, it takes advantage of a couple of extra features to improve readability: Vim lets ...
Bloodgain's user avatar
  • 221
11 votes
Accepted

How to write each line into separate file?

Try a global command: :g/^/exe ".w! line".line('.').".txt" :g/^/ Do a command for every line (you can adjust this regular expression if you only want to save certain lines, i.e. . for non-empty ...
Tumbler41's user avatar
  • 7,666
11 votes
Accepted

How to use :g to insert?

What comes after the second / is an Ex command. In this case you could use the :normal command, which executes its argument as if you typed it in normal mode (see :help :normal) :g/^#/normal I# or ...
Jair López's user avatar
  • 1,824
11 votes
Accepted

Deleting lines matching a pattern and put them into the buffer

You could delete the lines in a register: First clean up your register q for example with qqq in normal mode. Then use :g/PATTERN/norm! "Qdd In normal mode when you use "qdd you replace the ...
statox's user avatar
  • 49k
10 votes
Accepted

Append lines of text after pattern using global command

If I understand your post correctly, you had this snippet of C code : switch (result) { case CASE_1: return report("..."); case CASE_2: return report("..."); } And you wanted to transform ...
saginaw's user avatar
  • 6,666
9 votes
Accepted

How to delete searched line and next

You could use the following command: :g/SPECIAL/.,.+3d Which can be detailled like this: :g/ Apply a command on all the lines matching a pattern SPECIAL/ The pattern to match .,.+3d The ...
statox's user avatar
  • 49k
8 votes

Delete all consecutive duplicates

If you want to remove ALL adjacent identical lines, not just Hold, you can do it extremely easily with an external filter from within vim: :%!uniq (in a Unix environment). If you want to do it ...
Wildcard's user avatar
  • 4,315
8 votes
Accepted

How can I use consecutive numbers in an Ex-style substitute command?

This command does what you want: :let i = 1|g/^Do/s/^/\=i/|let i = i + 1 Explanation… let i = 1 initializes counter i, g/^Do/s/^/\=i/ prepends i to each line starting with Do, let i = i + 1 ...
romainl's user avatar
  • 38.3k
8 votes
Accepted

Using global command on Quickfix entries

Update: New official vim plugin cfilter Since 21.8.2018 (patch: 8.1.0311) the plugin cfilter is distributed with vim in $VIMRUNTIME. It is documented under :h cfilter-plugin. Load plugin cfilter ...
Hotschke's user avatar
  • 4,590
7 votes
Accepted

How to surround all headlines with empty new lines in a markdown file?

Well, you can have perhaps more cases than you account for in your description: a header line can be preceded by 0, 1 or many empty lines, and can be followed by 0, 1 or many empty lines. In all these ...
VanLaser's user avatar
  • 9,600
7 votes
Accepted

Reducing multiple blank lines into single blank line. Exceptional case

Why your command doesn't work The simple answer is the . as the end range for your :j command will match the space in your offending line. This means that your nearly empty line will not be removed/...
Peter Rincker's user avatar
7 votes
Accepted

Unmapping the global mappings on a per-buffer basis

You could overwrite the mapping by a buffer-local mapping. Just map <ESC> to <ESC>: tnoremap <buffer> <ESC> <ESC>
Ralf's user avatar
  • 9,037
7 votes
Accepted

How to truncate all lines at a certain length?

Using a substitution :%s/.\{100}\zs.*// Find 100 characters, .\{100} then start the match, \zs, and select the rest of the line, .*. Replace the match with nothing. For more help see: :h :s :h /\. ...
Peter Rincker's user avatar
6 votes

Append lines of text after pattern using global command

One way to do it is to use the widely underappreciated \zs: :%s/report.*\zs/\r break;/ What this does is, look at lines containing report, find end of line, and add a new line with break. You ...
Sato Katsura's user avatar
  • 3,969
6 votes

How to replace content between two patterns from the file?

This seems to work, at least on a Unix system: :/<inputs>/+1,/<\/inputs>/-1!cat foo.txt It uses the {range}!{filter} command to filter the lines from one after <inputs> to one ...
garyjohn's user avatar
  • 6,244
6 votes
Accepted

Joining consecutive commas using ex-mode

You can do this with a simple substitute command. Try this: :%s/\n,/, This removes a newline from every line containing a newline followed by a comma. You could also do it like this: :%s/\n\ze, ...
DJMcMayhem's user avatar
  • 17.3k
6 votes

How to execute command on every matching pattern, not just lines?

There's nothing like it mentioned anywhere in repeat.txt (:h repeating) so one can probably conclude that there's no native support for it. If you're running this on the whole file a recursive macro ...
B Layer's user avatar
  • 19.4k
6 votes
Accepted

replace or operate within regex match

Maybe your solution is to use a sub-replace-expression (:h sub-replace-expression) For example in your first example let's say that I want to replace spaces with underscores (because it's more visual ...
statox's user avatar
  • 49k
6 votes
Accepted

function to search for a pattern

To create a custom command line command :command is a good choice: :command! -nargs=1 SL g/<args>/z#.1 You'll need to use a name that starts with a capital letter, though, so I'm using "SL" ...
B Layer's user avatar
  • 19.4k
6 votes

Join multiple lines based on pattern

I found something on the Vim wiki that combines ranges with searches. This command did the trick for me: :g/^"/,/"$/j e.g. :g (global) /^"/,/"$/ (a range between a line ...
daviewales's user avatar
5 votes

How to replace content between two patterns from the file?

An alternative approach to that from Gary is this: :g/^<inputs>/+,/^<\/inputs>/-d|-r dummy Which first deletes everything in the given pattern, than uses the :r command to read the data ...
Christian Brabandt's user avatar
5 votes

Use pattern of global ex command found on a line to substitute in another line

A possible workaround is to use a macro: qa/^subroutine<CR>f<space>/end subroutine<CR>$pq Which can be detailled like this: qa Record a macro in the a register /^...
statox's user avatar
  • 49k
5 votes
Accepted

Run global with range from within function

From the documentation (:h function-range-example): function Cont() range execute (a:firstline + 1) . "," . a:lastline . 's/^/\t\\ ' endfunction 4,8call Cont() You need to use execute to pass the ...
nobe4's user avatar
  • 15.8k
5 votes
Accepted

indent command does not indent matched lines

I'm guessing the problem is that :global operates linewise, so the \zs and \ze in your regex don't do anything; They still apply to the line that matched the entire pattern, not the line that the ...
DJMcMayhem's user avatar
  • 17.3k
5 votes
Accepted

Duplicate line and replace part of line multiple times

If you're willing to use one command by language you can do this: g/en-US/t.|s/en-US/en-GB g/en-GB/t.|s/fr-FR/en-GB As you can see we reuse your command, and add another one to be executed with the |...
statox's user avatar
  • 49k

Only top scored, non community-wiki answers of a minimum length are eligible