Search code examples
vimvim-plugin

Split file by cursor position


Let's say we have open a file xaa.txt and we are on a line 25.

Is there a command to split file xaa.txt into who files (xaaa.txt, xaab.txt) so that lines 0..24 will be in file xaaa.txt and lines 25..50 will be in file xaab.txt? The file names for spliting can be inserted as parameters. I will do this operation often.


Solution

  • You can give a range to :w:

    :1,24w xaaa.txt
    :25,50w xaab.txt
    

    Since you are on line 25, you can simplify the above to:

    :1,-w xaaa.txt
    :,50w xaab.txt
    

    See :help :w and :help :range.

    You can also build the new filenames from the current filename. Assuming you are in xaa.txt:

    :1,24w %<a.txt
    :25,49w %<b.txt
    

    See :help extension-removal and the related :help filename-modifiers.