By simply calling :(range)g/bob/norm Axxx
I can add xxx
to the to the end of all lines in range
containing bob
. Is there a way to return to normal mode after having used A/I/a/i to enter insert mode, all within a command, in order to perform more actions to the matching lines without starting another command?
1 Answer
Let's say that we're using the global command to surround each matching line with brackets. (This is not really a good way to bracket text. It's just for illustration here.)
You hinted at maybe inserting an Esc somewhere in there. Perhaps like this...
:g/bob/ norm! A}<ESC>I{
But that approach won't work.
Others might have a first instinct to try something like this:
:g/bob/ norm! A} | norm! I{
Alas, this won't work either. Both these cases see everything after the opening A
as text to be appended.
Digging deeper into the second approach, if we look at :h :norm
we'll find the following:
This command cannot be followed by another command, since any '|' is considered part of the command.
Fortunately we can get around this by embedding that first :norm
command in an execute
call (:h :exe
)...
:g/bob/ exe "norm! A}" | norm! I{
That does the trick. Using :exe
allows us to implicitly signal the end of the first :norm
.
Note that this is a generally applicable solution that answers the question "How do I follow :norm
with another command?".
-
If it is used in place (not inside a script) i would prefer to insert a literal Escape: g/bob/norm! A}<C-v><Esc>I{ Commented Sep 9, 2018 at 0:27
-
@SaulAxelMartinezOrtiz If you're talking about those times that one enters a command manually I would tend to use Ctrl-V myself. In any other scenario, including when providing answers here, I avoid Ctrl-V. Such commands are very often ambiguous to folks who aren't familiar so extra explanatory text is necessary. And they frequently cause trouble for people who unknowingly try to copy/paste such commands.– B LayerCommented Sep 9, 2018 at 4:15
:help :bar
seems to be what you what to read