move selection to a separate file

By "move a piece of text to a new file" I assume you mean cut that piece of text from the current file and create a new file containing only that text.

Various examples:

  • :1,1 w new_file to create a new file containing only the text from line number 1
  • :5,50 w newfile to create a new file containing the text from line 5 to line 50
  • :'a,'b w newfile to create a new file containing the text from mark a to mark b
    • set your marks by using ma and mb where ever you like

The above only copies the text and creates a new file containing that text. You will then need to delete afterward.

This can be done using the same range and the d command:

  • :5,50 d to delete the text from line 5 to line 50
  • :'a,'b d to delete the text from mark a to mark b

Or by using dd for the single line case.

If you instead select the text using visual mode, and then hit : while the text is selected, you will see the following on the command line:

:'<,'>

Which indicates the selected text. You can then expand the command to:

:'<,'>w >> old_file

Which will append the text to an existing file. Then delete as above.


One liner:

:2,3 d | new +put! "

The breakdown:

  • :2,3 d - delete lines 2 through 3
  • | - technically this redirects the output of the first command to the second command but since the first command doesn't output anything, we're just chaining the commands together
  • new - opens a new buffer
  • +put! " - put the contents of the unnamed register (") into the buffer
    • The bang (!) is there so that the contents are put before the current line. This causes and empty line at the end of the file. Without it, there is an empty line at the top of the file.

How about these custom commands:

:command! -bang -range -nargs=1 -complete=file MoveWrite  <line1>,<line2>write<bang> <args> | <line1>,<line2>delete _
:command! -bang -range -nargs=1 -complete=file MoveAppend <line1>,<line2>write<bang> >> <args> | <line1>,<line2>delete _

Tags:

Vim