I am trying to prepend text from within Vim to another file. Specifically the current line, but a visual selection would be nice as well. Eventually I would like to map it to something like <leader>i
.
In bash I can use the following sed command to achieve this:
sed -i '1i some new text' myfile.md
So I tried this from within vim:
:execute "!sed -i '1i" .getline('.') "' myfile.md"
This works but when there is an !
in the currentline: shell returned 2
.
Then I found out about shellescape, and I got the following command to work. Now it seems like I'm doing something wrong escaping characters as this will add single quotes around the string.
:execute "!sed -i \"1i " . shellescape(getline('.'), 1) . "\" myfile.m
What am I missing? Is there a better way?