Skip to main content
14 votes
Accepted

Passing visual range to a :command as its argument

See this answer on stackoverflow. The example given is: function! PrintGivenRange() range echo "firstline ".a:firstline." lastline ".a:lastline " Do some more things endfunction ...
statox's user avatar
  • 50.5k
8 votes
Accepted

How to avoid 'Backwards range given' warning when invoking search range?

In :<from>,<to>command, both <from> and <to> are relative to the current line. What you want is to make <to> relative to <from>. For this you need :help :;: :/foo/...
romainl's user avatar
  • 42.3k
7 votes
Accepted

How can I use a range as input for an external command without deleting it?

You can use :write for this: :'<,'>w !copy_stuff For more details, see :help :write_c: Execute {cmd} with [range] lines as standard input (note the space in front of the '!').
Rich's user avatar
  • 32.6k
7 votes
Accepted

How to highlight a search range?

A If you just want to quickly confirm the validity of the range (and it isn't too long), you can use the built-in :print or :number: :/<head/,/\/head>/print B Another idea would be (mis-)...
Ingo Karkat's user avatar
7 votes
Accepted

How to check whether a command is run with range or not?

A range with commands is always about lines. It seems you want to work with line/column positions. Whether a range was given can be checked by using <range>: command! -range Test call TestFunc(...
Ralf's user avatar
  • 9,417
6 votes
Accepted

How to move marked line to current location

The format for :m[ove] is :[range]m[ove] {address} Where the range is the line or lines you want to move and the address is the target. Note that the moved line(s) will actually be placed on the line ...
B Layer's user avatar
  • 20.1k
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
5 votes
Accepted

What does the command option '-range=N' do?

Using the -range flag when defining a user-command usually allows the user to provide a range to the command: command -range Mine echo <range> <line1> ',' <line2> %Mine 1,/bar/Mine "...
D. Ben Knoble's user avatar
  • 27.7k
4 votes
Accepted

Is it possible to execute a Normal mode command only on the first/last line of a range?

As I said in the comments I strongly recommend a plugin like nerdcommenter or vim-commentary to do stuff like this because they are well tested and handle a lot of corner cases. Now if you want to do ...
statox's user avatar
  • 50.5k
4 votes

How can I echo the result of function with a range?

I looked into this, and I could not find a direct way of doing it. However, one possibility is to create an auxiliary function, that is: function! Range() range abort return RangeAux(a:firstline, a:...
Karl Yngve Lervåg's user avatar
4 votes

In Vim ex mode how to re-use a string from the default buffer in the command

Using ex Ex is the command-line successor to the venerable ed, a line-oriented editor. You can access ex-mode from vim by typing Q (unless you have it mapped). gQ gives an improved ex-mode. See :help ...
D. Ben Knoble's user avatar
  • 27.7k
4 votes

How supply range to normal mode key-binding?

Assuming the cursor is on line #80 and <leader> defaults to backslash, type directly 2\t. The point is that typing N: in Normal mode, where N is arbitrary number, is converted by Vim to :.,.+N-1....
Matt's user avatar
  • 21.6k
3 votes

How can I echo the result of function with a range?

@Karl's answer is pretty good but you can also use execute(): echo execute("1,2call Range()") You can even use variables for the range: let l1=1 let l2=2 echo execute(l1 . "," . l2 . "call Range()"...
statox's user avatar
  • 50.5k
3 votes
Accepted

Deleting folds over a range

You can use the :normal command to execute a normal command such as zD for all lines in a range. To execute that command in all lines from the current one until the end of the file, you can use: :.,$...
filbranden's user avatar
  • 29.9k
3 votes
Accepted

In Vim ex mode how to re-use a string from the default buffer in the command

@D.BenKnoble has already given you a terrific answer explaining all the ins and outs of batch mode, but in case you were looking for a one liner similar to the one you already tried, here's one that I ...
Rich's user avatar
  • 32.6k
3 votes

In Vim ex mode how to re-use a string from the default buffer in the command

ex -s +'norm! gg"adiwdd' +'exec printf("%%s/^/%s : /", @a)' +'x' ex_txt norm! gg"adiwdd delete black in 1st line to register a, delete first line exec printf("%%s/^/%s : /", @a) add content of ...
dedowsdi's user avatar
  • 6,358
3 votes
Accepted

Is it possible to chain multiple ex commands together with a single range?

Short answer: no. You can use the :global command to pick some lines to operate on, and then give a sequence of commands to operate on those selected lines: :3,4 g/^/ > | m1 We're only ...
Antony's user avatar
  • 2,610
3 votes
Accepted

How to assign a search range selection into a register?

You can use the :yank ex command: :/pattern1/;/pattern2/yank a a is the register you want to use and has a default value. Note: You used ;, in your command. It should be either ; or ,, not both.
doraemon's user avatar
  • 1,677
3 votes

Passing visual range to a :command as its argument

you can also adapt this one liner command -range TmuxSession execute "!tmux.session " . @* or if you do need multiline visual selection change @* to substitute(@*,"\n"," ","g") The @* register ...
mrajner's user avatar
  • 213
3 votes

How to pipe *characters* to cmd ( `:!` )

You can define a function in your .vimrc: function! GetVisualSelection() let [lnum1, col1] = getpos("'<")[1:2] let [lnum2, col2] = getpos("'>")[1:2] let lines = getline(lnum1, ...
grodzik's user avatar
  • 4,738
3 votes
Accepted

How to exclude the ending pattern in a search range?

I could be wrong but I think the atom \ze does take effect, it just doesn't prevent Vim to find a match on the group3 line. It's an empty match since you write \ze just after the anchor ^ which ...
user9433424's user avatar
  • 6,198
3 votes
Accepted

Replace characters in relative linespan

I actually figured it out a few minutes later: define a range based on current location . and relative line number +{} or -{}, e.g. :.,+5s/foo/bar/g. I'll leave the question up anyway for the sake of ...
Yehuda's user avatar
  • 275
3 votes
Accepted

Yank current line range

If you want to copy the range specification you could add the following mapping: vnoremap <silent><C-s> :call setreg('"', line("'<") .. ',' .. line("'>"))<...
Vivian De Smedt's user avatar
2 votes

Using more than two line specifiers in an ex-range

In the following range, the offset is calculated from "the current line" which may or may not be a match for /foo: :/foo/,+23# You can use ; to make the first line matching foo the first line ...
romainl's user avatar
  • 42.3k
2 votes
Accepted

Using more than two line specifiers in an ex-range

Almost right below your quote from documentation it also says: When separated with ';' the cursor position will be set to that line before interpreting the next line specifier. So while there ...
xaizek's user avatar
  • 1,828
2 votes

How to pipe *characters* to cmd ( `:!` )

If someone looks for an answer, here is the one from Replace selection with output of external command c to put the visually selected text in the default ("") register Ctrl+r to execute a Vim command ...
Christian Rondeau's user avatar
2 votes

How to exclude the ending pattern in a search range?

My approach :g/group2/+1,/group3/-1p
SergioAraujo's user avatar
  • 1,185
2 votes

Is it possible to execute a Normal mode command only on the first/last line of a range?

When surrounding with this trick, always start by appending, then you can insert the head. Moreover, I'd play with visual mode for this, no range, no :normal. " untested vnoremap <buffer> µ `...
Luc Hermitte's user avatar
  • 17.8k
2 votes

In Vim ex mode how to re-use a string from the default buffer in the command

You can use registers (see :h reg) to store a word of a line. For you particular case, you would have to perform the following set of commands: :norm! gg"zyiw :norm! gg"_dd :%s/^/<C-r>z: / The ...
3N4N's user avatar
  • 5,934
2 votes

Search and replace multiple line items within a range?

UPDATE So I have found simpler solution: :/first/,/second/g/bar/s/foo/bar/g /first/,/second/ take a range between first and second g/bar/ filter out lines that do not have bar (keep lines that has ...
Maxim Kim's user avatar
  • 13.9k

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