Is there a vim plugin or other program that copy-pastes a selected text to a permanent file in one keystroke?

1

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.

Bentley4

Posted 2013-04-02T09:26:08.207

Reputation: 1 588

Answers

2

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>

Ingo Karkat

Posted 2013-04-02T09:26:08.207

Reputation: 19 513

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 – 2013-04-02T10:14:09.583

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 – 2013-04-02T10:14:48.633

1The 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 – 2013-04-02T10:18:43.253

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 – 2013-04-02T10:34:03.940

1

No, looks fine. Can you try another key, e.g. <F9>? Maybe it's this issue.

– Ingo Karkat – 2013-04-02T11:17:45.193

It looks like <C-s> was the culprit(and apparently also <C-q> did not work), I tried it with Q and both commands worked. Any idea how I can add a blank line in the file after each insertion? – Bentley4 – 2013-04-02T11:42:57.287

Ah, glad you've found it! Another !echo "" should work for a blank line. – Ingo Karkat – 2013-04-02T12:04:30.120

:vnoremap Q :<C-u>execute '!echo "%" >> ~/Desktop/hi.txt'<Bar>'<,'>write! >> ~/Desktop/hi.txt<CR><CR><CR>:<C-u>execute '!echo " " >> ~/Desktop/hi.txt'<Bar>'<,'><CR><CR>:echo "Pasted!"<enter> is the full command I was looking for. It skips the confirmation screen and shows a "Pasted!" message after you pasted your lines to that file. – Bentley4 – 2013-04-02T12:05:41.923

Simply adding !echo does not work (:vnoremap Q :<C-u>execute '!echo "%" >> file'<Bar>'<,'>write! >> file<CR>!echo ""<CR>). But that's probably not what you meant. Anyway, with that command in my previous comment it does what I want, although I think that newline could be added in a less verbose as I wrote it. Thanks for your help! – Bentley4 – 2013-04-02T12:17:01.913