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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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/...
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>
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 /\.
...
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 ...
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 ...
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,
...
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 ...
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 ...
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" ...
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 ...
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 ...
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
/^...
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 ...
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 ...
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 |...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
global-command × 83regular-expression × 19
substitute × 14
search × 8
vimscript × 7
ex-mode × 6
cut-copy-paste × 5
replace × 5
command-line × 4
multiple-lines × 3
range × 3
vimrc × 2
key-bindings × 2
neovim × 2
normal-mode × 2
highlight × 2
external-command × 2
macro × 2
register × 2
comments × 2
repeated-commands × 2
ex-command × 2
sort × 2
syntax-highlighting × 1
gvim × 1