5

I am breaking apart some files, and I'd like to paste what I cut into a brand new file, which I specify the name of. Something like:

:c file-that-doesnt-exist.txt

Where c is a key command.

Edit I've found :e, but that just takes me to a new buffer with the same clipboard. I would just like to magically dump the clipboard into a new file and continue editing the original file, without seeing a new buffer.

2 Answers 2

7

A fast way, working on entire lines, is to visually select the lines to move (V ...), then:

:'<,'>write! >> the_other_file.txt

... where '<,'> is automatically appended by Vim after you press :. This will append the range to that file, creating it if it doesn't exist. Then, a gv in normal mode will reselect the range, so you can easily delete it (d).

Then, it's even easier - select another range (V ...), hit : to open command-line, Ctrl-P to bring back the last command, Enter to copy the lines 1. Then gvd to delete them locally; rinse and repeat.

Alternatively, you could "cut" to a named register by appending to it (using the capital letter), e.g. on first text selection to cut: "ad, then (repeatedly): "Ad (on other selected text). At the end, edit the new file and paste from that register ("ap).


1Even better, use @: to repeat the last command (as Karl Yngve Lervåg suggests in the 1st comment below).

6
  • 1
    Even easier: Use @: to repeat last ex command. Aug 28, 2015 at 6:09
  • Nice! I modified the post accordingly, even if its position is quite close to your comment :)
    – VanLaser
    Aug 28, 2015 at 10:04
  • @KarlYngveLervåg what is an 'ex' command?
    – user151841
    Aug 28, 2015 at 15:46
  • 1
    @user151841 Ex commands are commands you enter in command-line mode with :. See :help cmdline.txt. Aug 28, 2015 at 17:58
  • 1
    @user151841 >> is used to append lines (concatenate, yes), and ! to create the file if it doesn't exist. To overwrite a file instead, use '<,'>write! the_other_file.txt (without >>, and no, without a > - it's not shell scripting). See :h :w! and :h :write_a.
    – VanLaser
    Aug 28, 2015 at 19:36
5

An alternative is, assuming you want to store register a:

sp +0put\ a c:\temp\split.txt

Alternatively, you can use :e or :tabedit or even :new if you want a new unnamed buffer.

See the help

:h :put
:h +cmd
:h :new
:h :split_f

Your Answer

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

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