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.1k
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
  • 41.2k
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
  • 17.9k
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.2k
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,287
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
  • 20k
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
  • 26.6k
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.1k
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
  • 26.6k
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
  • 20.9k
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,590
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
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,667
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,148
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,658
3 votes
Accepted

specifying a range the last of which is the penultimate line

You can express the penultimate line with $-1. So, in your case, to prepend X in front of all the lines from the fifth down to the second to last one, you could use the following substitution command: ...
user9433424's user avatar
  • 6,148
3 votes
Accepted

Applying changes to lines in alternating modes

Well actually you can use a litteral <ESC> character to chain commands like you would do normally: :'<,'>norm! A,^II+ This will insert a , at the end of each line, and a + at the start. ...
nobe4's user avatar
  • 16.1k
3 votes

Invert range selection

Here is a solution which comes from here. Note that might not be exactly what you're looking for since it's implies to use a visual (or normal) mode mapping instead of just an ex command. The idea is ...
statox's user avatar
  • 50.1k
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.1k
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,288
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.2k
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
  • 29k
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

How to search and format a text range?

Per @statox comment... Using normal commands applied to ranges is the same as when they are used in scripts, requiring the normal function to be called. :?^[^\/][^\/]?+1,/^[^\/][^\/]/-1norm gq A ...
jecxjo's user avatar
  • 2,700
2 votes

Define range as: "from first line matching to last line matching a given pattern"

Here are two functions that you could use. Both takes as argument a pattern to search and a command to execute on the consecutive lines matching the pattern. You can call them with: call ...
statox's user avatar
  • 50.1k
2 votes

Act on lines changed by previous command

As @romainl said, you can use the [ and ] marks for this purpose, e.g.: nnoremap gc `[v`] Pressing gc visually select the previous change. :'[,']s/var_name/varName/g Replace in the last modified ...
nobe4's user avatar
  • 16.1k

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