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
  • 14k
19 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,736
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
  • 921
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,726
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,854
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
  • 49.6k
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
  • 49.6k
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,740
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,680
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,147
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

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,299
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.5k
6 votes

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

You might be interested by this question: Run a command on every match of a search (not on the line). The answer suggests the use of sub-replace-expression but this is more adapted when the changes ...
LEI's user avatar
  • 1,626
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.7k
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
  • 49.6k
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.7k
6 votes
Accepted

g/pattern/d but check first

One way to achieve what you want to do is to use the :s command inside your :g command: We know that we can delete a line, with a confirmation using the following substitution: :s/.*\n//c This will ...
statox's user avatar
  • 49.6k
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
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.5k
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
  • 16k
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
  • 49.6k
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
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
  • 49.6k
5 votes

How to run a python command based on a matched group of a pattern?

TL;DR There is no simple way to get capture groups into other commands. They simply don't see them. About the :s command As many vim users know, :s is the :substitute command, and it deals in vim's ...
D. Ben Knoble's user avatar
  • 25.7k
5 votes

How can I delete lines fit a pattern to a register or a new file?

Use a Dedicated Register to Which You Append Note delete overwrites the unnamed register "" for each match. Therefore, only the last line matching the pattern is still available in "". Also the ...
Hotschke's user avatar
  • 4,740
5 votes
Accepted

Using :g to yank in quotes across multiple lines

You're confusing the passing of the register to :yank (Ex command) vs. y (normal mode). For the latter, instead of appending the register, it must come before the y command, and with the " prefix: "...
Ingo Karkat's user avatar
  • 17.8k
5 votes
Accepted

Delete blank lines within range

In the very general case, the :g command cannot be called recursively. There is one exception: Since Vim patch 8.0.0630, one can call a recursive :g command, if it operates only within a single line. ...
Christian Brabandt's user avatar
4 votes
Accepted

How to simulate Return and Escape for global command without using Control-V?

exec the norm command. From :h :norm: An alternative is to use :execute, which uses an expression as argument. This allows the use of printable characters to represent special characters. Example: ...
muru's user avatar
  • 24.8k

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