The direct way to write a series of lines to delete and delete after that you need to run the command
:$-9,$w path | '[,']d
However, this is inconvenient for frequent use if the file name is not constant.
The following is the MoveToFile() function that implements a command with the same name. All this includes the following two steps: write a series of lines using the command :write (see :help :w_f :help :w! , :help :w_a ), then delete this range. 1
command! -nargs=* -complete=file -range=% -bang -bar MoveToFile \ :<line1>,<line2>call MoveToFile(<q-args>, <bang>0) function! MoveToFile(fname, overwrite) range let r = a:firstline . ',' . a:lastline exe r 'w' . ' !'[a:overwrite] . fnameescape(a:fname) exe r 'd' endfunction
You can now use the command above to cover all commonly used use cases. For example, to move a visually selected range of lines to a file, use mapping
:vnoremap <leader>m :MoveToFile
This mapping produces a semi-complete command that calls :MoveToFile for the range of lines selected in visual mode ( '<,'> ). You only need to enter the file name and press Enter .
If you often do this for the last ten lines of the buffer, create a similar mapping for this case only:
:nnoremap <leader>m :$-9,$MoveToFile
1 The specified lines are deleted by default by overwriting its previous contents. Deleting strings without registers changes the last command in MoveToFile() to
exe r 'd_'
In this case :delete uses the black hole case (see :help :d and :help "_ ) instead of the standard one.
source share