Unleash your inner root

Vim: Mastering your workspace: windows

In my last article I talked about buffers, and touched on how they can improve your workflow. As promised, this is the follow up where I talk about how amazing windows are, and how they will change your (vim) life forever!

Let’s do a short recap: Buffers are essentially the vim loaded version of a file, ready to be written back to the file after a change is made. So what then is a window? A window is a view of a buffer. When you start vim by default you have a single window open. Each window can view one buffer at a time(though that buffer can be rotated at will without causing you any grief as we learned in the last article).

So what then do you do, if you need multiple windows? Well there are two way’s of accomplishing that in vim, one of them is considered the “wrong” way, and the other is considered the “right” way, and once you understand things as a whole, you will understand why each is each. For the purpose of this article, I will be guiding you in the righteous path of good, and steering you away from the terrible path of wrong. When I first started diving more deeply into vim, I managed to teach myself “the wrong way,” and I kept reading poeple’s posts talking about how what I wanted to do was wrong, but no one ever really explained why. It took me a longer than it should have for the obvious reasons to hit me. So once you know the right way, I will talk to you about the “wrong way” and tell you why its bad, and why if you do it you should feel bad.

There are two commands you should be aware of when talking about windows:

:split
:vsplit

These two commands are fairly straight forward “:split” will split the window horizontally, creating a second window below the existing one. You might notice that it opens by default it opens the new window looking at the same buffer as your original window was looking. Go ahead and enter “insert mode” and type something, you will notice that whatever you type is displayed across both windows. That is an important thing to be aware, a window is just a LOOK at a buffer, it is not a unique copy of a buffer, any changes made to the buffer in one window, edits the same buffer as is open anywhere else that buffer is being viewed.
Before Split:
Single Window

After Split:
:split Base size

“vsplit” on the other hand is simply a vertical split rather than a horizontal split(In most cases I actually prefer a vsplit to a standard split, but that’s just me)
:vsplit

You can alternatively open another, or a new, file using:

:split <filename>
:vsplit <filename>

Now that you have a split window, there are two facts you should be aware!

FACT NUMBER 1! You can double tab -w to cycle between your open windows.
FACT NUMBER 2! You can use :bn or :bp to cycle your buffers in each window independently. Thus allow you to look at multiple buffers simultaneously.

In slightly more detail, the following navigational commands will make your life wonderful:

<ctrl>-w <ctrl>-w # Cycles through all open windows
<ctrl>-w h        # Moves to the next window on the left
<ctrl>-w j        # Moves to the next window down
<ctrl>-w k        # Moves to the next window up
<ctrl>-w l        # Moves to the next window on the right

So now you have a basic idea of windows, lets get a little more advanced!

You can split windows multiple times! Each time it will split the existing window in whatever method you choose. So if you continue to only use :split, you will end up with a stack of horizontal windows, if you continue to do :vsplit, you will end up with a stack of vertical windows, and if you mix and match ,you will end up with an amazing assortment of small and large vertical and horizontal box’s. Allowing you complete control over your environment! Here lies the power of vim! You can make your environment as crazy as you are!
Multi split windows

“but wait!” I hear you say “what if all this splitting makes the windows too small or too big in the wrong way!”

well there Timmy, fear not, we’ve got you covered. First command functions:

resize +/-#
#IE
resize +10 # Makes the current window 10 lines taller
resize -10 # Makes the current window 10 lines shorter
resize 10  # Makes the current window 10 lines tall...period.
res        # Shorthand for "resize"

Base Size:
:split Base size

+10 To top Window:
split: +10

vertical resize +/-# 
#IE
vert res +10 # makes the current window 10 lines wider
vert res -10 # makes the current window 10 lines skinnier
ver res 10   # Makes the current window 10 lines wide...period.

Base Size:
:vsplit

+10 to Left Window:
vsplit: +10

Now you may be asking yourself, “That’s all fine and dandy Brandon, but you said there was a wrong way. What’s the wrong way?” Well, I’ll give you a hint, it involves tabs, and that discussion is coming up in the final installment of “mastering your work space”

splitsplitsvimwindows

Brandon.Graves • February 26, 2016


Previous Post

Next Post

Leave a Reply

Your email address will not be published / Required fields are marked *

%d bloggers like this: