Is there a vim plugin or other program that copy-pastes a selected text to a permanent file in one keystroke? E.g. You select a text, do Ctrl + s and instead of copying to clipboard, it copies to a permanent file.

It can be via a vim plugin because I'll always do this action from inside a vim session. Ultimately I would want to first paste the relative path of the current file I'm in, then paste the selected text and then insert a blank line to separate for the next entry.

I am using Ubuntu 11.10 btw.


That's basically a simple mapping:

:vnoremap <C-s> :write! >> file<CR>

To prepend the file name, I'd invoke an external command, though it could be done with readfile() / writefile(), too.

:vnoremap <C-s> :<C-u>execute '!echo "%" >> file'<Bar>'<,'>write! >> file<CR>
  • When I select a text in visual mode and do :write! >> ~/myfile.txt<CR> it works. But when I put :vnoremap <C-s> :write! >> ~/mytext.txt in my .vimrc and press <C-s> after restarting a vim session it doesn't do anything strangely. – Bentley4 Apr 2 '13 at 10:14
  • Also, when executing :<C-u>execute '!echo "%" >> ~/myfile.txt'<Bar>'<,'>write! >> ~/myfile.txt<CR> after selecting a text I get an error message trailing characters, seems it's not correctly formatted. – Bentley4 Apr 2 '13 at 10:14
  • 1
    The mappings must include the trailing <CR>, but if you type it as commands, instead of <Bar> type the literal |. Failure may also be due to your settings, see :help map-special. – Ingo Karkat Apr 2 '13 at 10:18
  • I did :noremap <C-s> :write! >> ~/Desktop/ho.txt<CR> and :noremap <special> <C-s> :write! >> ~/Desktop/ho.txt<CR> confer to the example in :help map-special, still the same thing. Do you recognise anything in my .vimrc that could interfere with this command? – Bentley4 Apr 2 '13 at 10:34
  • 1
    No, looks fine. Can you try another key, e.g. <F9>? Maybe it's this issue. – Ingo Karkat Apr 2 '13 at 11:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.