Can one put control characters into a text so that when I copy and paste it in Vim it exits insert mode and does something nasty in command mode?
The short answer seems to be "yes". I was able to put the following in my clipboard:
hello<Escape>:!date<CR>
and when I pasted it into vim while in insert mode hello
was typed and then the shell opened up and the date
command was run.
Obviously if I can run the date command in the shell I can do much more nasty stuff.
To get that string in my paste buffer I opened vim and typed hello<C-V><Esc>:!date<C-V><Enter>
. I then had to save that file, open it with Kate and copy the contents that way (copying from vim didn't preserve the control characters).
That depends on the environment, and the Vim command used.
Graphical GVIM can differentiate pastes from typed keys, but in the terminal, this is not (generally) possible. That's why Vim has the 'paste'
and 'pastetoggle'
options, to tell Vim what is expected. Despite that, if the character stream contains a key like <Esc>
that switches modes, Vim will do so.
Instead of pushing text into Vim, it is safer to pull with Vim's p
ut command: "*p
. There, special characters like <Esc>
will be inserted literally into the buffer; Vim won't switch modes here. The only Vim command that interprets register contents as typed (and therefore is susceptible to mode switch commands) is i_CTRL-R
. To avoid that, one should use any of the other command variants, e.g. i_CTRL-R_CTRL-R
.
Pull text into Vim instead of pushing it; if you avoid the i_CTRL-R
command (or neuter it by remapping it), this is safe. Additionally, the :registers
command allows you to inspect all contents before pasting.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With