Reputation: 1020
Say I have a series of commands that I want to run in vim:
ifoo<ESC>:%s:foo:bar:g<CR>
When I paste the above into vim, this will be the output:
foo<ESC>:%s:foo:bar:g<CR>
How can I paste the above into vim so that my result becomes:
bar
I.e. that it treats <ESC>
and <CR>
et al as buttons instead of just characters part of a string.
Upvotes: 0
Views: 122
Reputation: 12089
:exe "norm ifoo\<ESC>:%s:foo:bar:g\<CR>"
If I copy that line, open a new Vim document, and paste, that line appears in the command line (bottom of the screen). Then, when I hit Enter...
bar
...appears in the text document.
So, here's what's happening:
The colon ":" activates the command line, and "exe" (short for the command execute
) instructs Vim to evaluate the following quoted string as Vimscript.
The string starts with the command "norm" (short for the command normal
), which instructs Vim to treat the following text as if it were typed into Vim.
Another thing that is necessary is to escape the non-printing key identifiers (<ESC>
and <CR>
) with a backslash \
so they are interpreted as their respective keystrokes and not as the literal characters.
Upvotes: 3