1

When I need to cut some lines from one file to another file, I always do the following steps:

  • Select the lines in file-1
  • :w >> file-2
  • Re-select the lines
  • d

This is super annoying if you need to do it for like 10 times.

So, is there anyway I can write the selection to another file and delete the selection in the same time?

Edit: maybe using gvd to reselect and delete is a faster option.

3

You could make it a one liner using a range:

2,3w >> b | 2,3d

And if you want to keep doing this with visual selection, you can select the lines and then use '< and '> for the range:

'<,'>w >> b | '<,'> d

Edit To address the questions in comments:

  • If the file you're trying to write doesn't exist you can use w! to force the command
  • The | character is used to chain commands (not unlike && in bash), you can read about it at :h :bar:

                            *:bar* *:\bar*
    '|' can be used to separate commands, so you can give multiple commands in one
    line.  If you want to use '|' in an argument, precede it with '\'.
    
  • As a command: command! -range -complete=file_in_path -nargs=1 Wd <line1>,<line2>write >> <args> | <line1>,<line2>delete _ – Peter Rincker Jun 25 at 21:11
  • Thank you, this works! But there is one issue: if file b does not exist, vim will return an error (E212: Can't open file for writing). Also, 1) Can you please explain what | do in the command? 2) typing '<,'> is not faster than reselect and delete with gvd. – Teddy C Jun 26 at 1:41
  • For error, use w! >>. The bar is a command separator. – D. Ben Knoble Jun 26 at 2:50

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.