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
...
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/...
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 '!').
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-)...
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(...
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 ...
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. ...
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
"...
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 ...
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:...
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 ...
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....
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()"...
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:
:.,$...
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 ...
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 ...
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 ...
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.
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 ...
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, ...
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 ...
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 ...
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("'>"))<...
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 ...
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 ...
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
...
2
votes
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> µ `...
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 ...
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 ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
range × 61command-line × 10
vimscript × 8
search × 7
cut-copy-paste × 6
visual-mode × 6
functions × 6
key-bindings × 5
normal-mode × 5
substitute × 4
global-command × 4
ex-command × 4
user-commands × 4
neovim × 3
regular-expression × 3
external-command × 3
register × 3
ex-mode × 3
line-numbers × 3
formatting × 2
count × 2
object-motions × 2
insert-mode × 1
cursor-motions × 1
replace × 1