Splitting a single file into multiple windows on Vim

5,072

Solution 1

As has already been pointed out, you can use splits displaying the same buffer to get you most of the way there.

:vsplit

or

:split

As for displaying only a specific section of the file, one way of doing it would be to fold the parts you aren't interested in. There are a few ways of folding arbitrary parts of files.

The easiest way would be to select the lines you don't want to see in visual mode, and use the zf mapping to fold it.

You can also use marks for folding, so you might put a mark just above the text you want to see, move to the top of the file, and then fold to that mark, which would look something like this:

ma
gg
zf'a

Folding is per window (split) so any folds you have in one window don't affect other windows, even if you have the same buffer displayed in each.

If you have trouble with creating the folds, try setting the foldmethod to manual:

:set foldmethod=manual

Solution 2

This is a actually pretty straightforward, and thankfully doesn't need any configuration. Once you open up your file, run

:vsplit

or the keyboard shortcut

<C-w>v

which does the same thing. This will make another window into the same buffer. The nice thing about doing this is that any change you make in one split will automatically be applied to other split. You don't even have to save!

The reason it works like this is because you only have one buffer, just multiple views into the same buffer.

You can also do

:split

or

<C-w>n

which works the same but with a horizontal split.

This general approach can then be extended to an arbitrary number of buffers, splits, and tabs.

Solution 3

Posted the below before reading carefully and seeing that you wanted the changes you make in the new split to be reflected in the original file. Which pretty much renders my answer obsolete. Still going to leave this here in case it's useful for anyone.


Inspired from this: http://vim.1045645.n5.nabble.com/How-to-read-specific-lines-from-a-file-into-current-buffer-td1180012.html

Let's say you have ~/.vimrc open,

You could open a new file in a split first :vsp file

then

:r! sed -n '22,25p;26q' ~/.vimrc

to put lines 22 to 25 to file

I'm sure this could be scripted and refactored. I'll look into the possibilities when I have some time.

Share:
5,072

Related videos on Youtube

Admin
Author by

Admin

Updated on September 18, 2022

Comments

  • Admin
    Admin over 1 year

    Is it possible to open a part of the file as a separate vertical split window in Vim?

    Say I have a file with hundred lines, and from the fortieth up until and including sixtieth lines I want to be rendered as a separate locked window within the main window as a split "tab" to focus better on that part of the file, and the changes I made there would reflect on the whole file in the main window as well.

    I have a very long file and I would like to implement such thing to manage its contents better.

  • Joshua Wade
    Joshua Wade over 7 years
    That's great, thank your for the shortcut, and it's nice that the changes immediately reflect on both windows, but what about locking specific lines, I don't want the whole file to be displayed but just a part, some lines of it.