You can use the :normal
command, which allows you to run a sequence of Normal mode commands. When given a range, it repeats the sequence for every line. (It also starts at the beginning of each line, when given a range.)
So you could use the e
motion (end of the word, for a definition of word that includes keyword characters) or E
(going up to the first blank character), followed by a
to append and then a `
to insert the character. (You don't need an explicit <Esc>
when using :normal
, it exits insert mode implicitly at the end of the sequence.)
For the range, assuming you want a subset of lines (for the whole buffer you'd use :%
), you could use a visual selection to pick which lines to act on. When you press :
from Visual mode, Vim will automatically insert :'<,'>
, which is a range for the most recent Visual selection.
So you could then run:
:'<,'>normal Ea`
This almost works flawlessly, but it fails on any blank lines, since on those lines the E
(or e
) motion will jump to the end of the first word of the next non-blank line, you will end up appending two or more `
s to the first word on lines that follow blank lines... To fix that, you could then use a :g
command with a /\S/
regex, to only execute the :normal
command on lines which match that pattern, in other words, lines with at least one non-blank character.
:'<,'>g/\S/normal Ea`
In some cases, you can simplify the procedure (while still supporting blank lines) by using a motion such as f{char}
or t{char}
. For example, if words are limited by commas (such as in a CSV file), you can use f,
or t,
to find the first comma (or even a count to find the nth one.)
The advantage of using that type of movement is that it's guaranteed to only match on the current line. Furthermore, :normal
will interrupt the command sequence if one such movement fails. So you can use that sequence without worrying about blank lines or lines without the matching character, since they'll just be silently skipped. No need to use :g
to filter those out in that case.
:'<,'>normal f,i`
Note that you can also use this technique using :normal
to insert the leading `
to the beginning of lines:
:'<,'>g/\S/normal I`
The advantage of this approach over using a visual block mode is that using :normal
with I
will insert the text snippet at the first non-blank position in the line (preserving indentation), while visual block insertion is limited to inserting it to a specific fixed column on every line.