Skip to main content
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
  • 14.2k
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

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,756
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
  • 50.1k
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
  • 50.1k
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,790
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,287
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
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.7k
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
  • 20k
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,636
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
  • 50.1k
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
  • 20k
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
  • 50.1k
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

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
  • 50.1k
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.7k
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
  • 16.1k
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
  • 50.1k
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
  • 26.6k
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,790
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.9k
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

Run a command on every match of a search (not on the line)

You can use the expression register, see :h sub-replace-expression: :s/search/\=MyFunction(submatch(0))/ The submatch() function can also be used directly inside the function, which would make the ...
Karl Yngve Lervåg's user avatar
4 votes

Joining consecutive commas using ex-mode

@DJMcMayhem's solution is pretty good but Vim is pretty powerful so here is an alternative with the global command: :g/^,/normal! kJ That you can translate as "For every line which begins by a comma ...
statox's user avatar
  • 50.1k
4 votes
Accepted

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

You could use a combination of :global, matchit and :normal!: :g/^\s*subroutine/norm ^whye^%$p Explanation: :g/^\s*subroutine: for all lines matching ^\s*subroutine, do: normal: enter normal mode (!...
Marth's user avatar
  • 356
4 votes

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

Here's the macro solution by @statox, modified to deal with subroutine parameters and nested subroutines. This assumes you also have installed matchit: qaq - clear register @a qa - start recording ...
Sato Katsura's user avatar
  • 4,019
4 votes

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

This is job for :substitute :%s/pattern\zs\s\+\S\+//g after the pattern (\zs), I match spaces (\s\+) followed by any number of non spaces (\S\+). If you really want dw and not dW, you'll prefer \(\...
Luc Hermitte's user avatar
  • 17.5k
4 votes

vim comment out every line of text containing a pattern using the 'global' command

Try using :g/\\draw/norm gcc From :help norm: Execute Normal mode commands {commands}. This makes it possible to execute Normal mode commands typed on the command-line. {commands} are executed ...
gandalf3's user avatar
  • 267

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