0

I have no idea how to do this, but here's my idea: Highlight some text in visual mode, type an ex command and a file name. It takes that highlighted text, deletes it, and appends it to the file you specify, creating it if it doesn't already exist.

I've italicized the parts I don't know how to do.

3
  • 1
    There are a number of ways to achieve this. What operating system are you on? Do you require a cross-platform solution? Do you want the new file to be opened in Vim? Or do you need it not to be opened in Vim? Or do you not care much either way?
    – Rich
    Commented Nov 21, 2017 at 10:12
  • Also (and this is just me being nosy, so feel free not to answer), what are you actually using the command for?
    – Rich
    Commented Nov 21, 2017 at 10:17
  • @Rich I'm using vim on cygwin (windows 10). I have a big file of esoteric code (there won't be a plugin for it) that I want to break up into small files. Commented Nov 21, 2017 at 17:37

2 Answers 2

2

Combining the advice of @SunnyPun and @Rich, I came up with this:

command! -nargs=1 -range -complete=file Move '<,'>w! >> <args>|'<,'>d
1

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

  1. 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.

  2. 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...

5
  • Why use tee? cat >> blah seems simpler?
    – Rich
    Commented Nov 21, 2017 at 15:40
  • Because it seems to me that :'<,'>w !cmd will append the contents at the end of the command.. I tried blah << (lol) and awk but can't make the file name come before the contents. I know I must have missed something obvious but all I have on my hands is tee now..
    – Sunny Pun
    Commented Nov 21, 2017 at 15:52
  • I'm not sure what you mean, but '<,'>w !cat >> blah.txt works for me, appending the current visual selection to the file blah.txt :w ! writes to stdout and cat without a filename reads from stdin.
    – Rich
    Commented Nov 21, 2017 at 16:29
  • 2
    Actually, you don't even need to shell out. So long as blah.txt isn't open in Vim, :'<,'>w! >> blah.txt works fine. Possibly that's a different answer, though.
    – Rich
    Commented Nov 21, 2017 at 16:33
  • @Rich your comment seems like the most straight forward way to do the whole thing. Thanks a lot Commented Nov 21, 2017 at 17:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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