15
votes
Accepted
Replace a series of asterisk bullet points with a numbered list
You could try the following command:
:let c=0 | g/^* /let c+=1 | s//\=c.'. '
First it initializes the variable c (let c=0), then it executes the global command g which looks for the pattern ^* (a ...
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
...
9
votes
Replace a series of asterisk bullet points with a numbered list
This only works with a recent Vim version (that has :h v_g_CTRL-A):
Block-select the list bullets (*) and replace them with 0 (cursor is on first *): Ctrl-v j j r 0.
Reselect previous block and ...
9
votes
Replace a series of asterisk bullet points with a numbered list
Visually select the lines and execute this substitution command:
:'<,'>s/*/\=line('.') - line("'<") + 1 . '.'
See :help sub-replace-expression, :help line(), and :help '<.
To avoid ...
7
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 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 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 '!').
6
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
Can my mapping/function detect when count/range 1 is given instead of none?
To get the count given to <C-E>, you don't need to use the range to do it. There are two pre-defined variables you can use, v:count and v:count1, which contain the count given to the last normal ...
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. ...
4
votes
Creating a Vim function mapping that takes a count
When using counts and calling functions a range will be inserted automatically by Vim. You can see this by doing simply doing 3:. A common solution is to use <c-u> to remove the range. See :h ...
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
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
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
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
Search and replace in vimscript using a range?
Use :execute and concatenation:
let startline = 2
let endline = 56
let searchpattern = "foo"
let replacepattern = "bar"
execute startline . "," . endline . "s/" . searchpattern ."/" . replacepattern ...
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:
...
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.
...
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 ...
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
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
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
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
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
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:
:.,$...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
range × 54command-line × 9
search × 7
vimscript × 6
cut-copy-paste × 5
normal-mode × 5
functions × 5
visual-mode × 4
ex-command × 4
key-bindings × 3
regular-expression × 3
substitute × 3
external-command × 3
register × 3
ex-mode × 3
global-command × 3
neovim × 2
formatting × 2
line-numbers × 2
command × 2
count × 2
object-motions × 2
user-commands × 2
insert-mode × 1
cursor-motions × 1