Here is an extremely simplified version as a function (run it in Vim to test):
Hopefully Inspiring Function
Assuming that tee
is working on your machine:
:function! PasteTo(path)
: silent execute ":'<,'>w !tee -a ".a:path
: normal! gvd
:endfunction
Then, select some text with linewise Visual mode, and do :call PasteTo('a.txt')
(You'll need to press <C-u>
to clear that :'<,'>
inserted)
The lines are gone and you can do :vnew a.txt
to see them there. Try it to see if it also appends.
Explanation: First line appends the lines to the file. Second one deletes the lines. (By gv
to repeat selection and d
to use visual delete)
Making that an Ex Command
:command! -nargs=1 Cap call PasteTo(<q-args>)
Then, you can use :Cap a.txt
(short form for cut and paste).
Questions
It is strange if I have forgotten to press <C-u>
... like if I selected 2 lines the text will be pasted twice. And extra lines deleted? What happened?
It runs for each selected line -- so when I selected 2, it copies and deletes once, and when the function is called for the 2nd line, it copies again and deletes again! You may want to do a mapping so <C-u>
is pressed before you "enter" :Cap
, or investigate for other ways to make your function works nicely with Visual mode.
- Even if I just character-wise select, it copies the whole line.. How to get visually selected text in VimScript? (xolox is promising, I'm using his easytags plugin).
Hope this helps and hope I have given enough rope...