Dynamically Resizing Window Pane Sizes
This guide walks through how to create custom Vim keybindings that will allow you implement custom behavior while using Vim. Specifically we'll use the example of dynamically resizing window panes in Vim.
Guide Tasks
  • Read Tutorial
  • Watch Guide Video
Video locked
This video is viewable to users with a Bottega Bootcamp license

In this guide, we're gonna talk about one of the most important things when it comes to customizing Vim which is to walk through how to create your own set of key bindings. And we're gonna use the whole window pane kind of behavior as an example but this could apply to any kind of thing. So to review what we want to do I have the gem file open right here, and if I use SP for split pane and go into the models and look at this application record you can see that I have accomplished what I want.

Which means I have two window panes open, however, this really isn't ideal. Because if you notice, I have three lines of code that are taking up the exact same amount of space as right here, 27 lines of code.

large

That's not really ideal for a kind of the most efficient work environment. We want to be able to have a situation where we have all of the code here in our gem file shown, and really only what we need shown right here above it, so I'm gonna show you how you can customize that by editing your vimrc file. So let's open this up, type vimrc and let's go down to the very bottom.

Now here I'm going to open up the settings and so you can go and follow along. This is in the GitHub repo that I provided with the course, and it's the Vim settings file. And this is my personal vimrc file that I use, and let's copy this long comment for custom settings. We're gonna get into this seeing is believing code here shortly but for right now I want to focus on what we're gonna be doing here with window resizing, and now let's copy the window pane resizing.

One thing you may notice because of our copy and paste indentation here is that because there was a comment on this line that the next two lines, 93 and 94 automatically received a comment. So the way you can fix that is by going to the very front of the line and typing Ctrl + V, go down with J, one over one over with L and then from there you can just type the capital X and you're good to go and it's removed those lines, so that's all you have to do in order to remove those.

large

So now that we have this let's walk through exactly what is going on with this, because if you've never written your own Vim script from scratch, this may look a little bit weird. So starting off we have this nnoremap, and what this is, is we're setting a custom map or a custom key binding here in Vim, so this is how we're declaring it. We're saying it wants to be silent which just means we're not really outputting anything with this, and then this is a really important one.

I'm not sure if you remember but back when we implemented our first vimrc file I talked about your leader key. And if you want to scroll up a little bit you can see, let's see, where is it? Okay right here on line 33, your mapleader.

large

So when we created our mapleader we set it equal to a comma. So what this is going to do is it's going to give us the ability to map this comma anywhere else by calling this leader syntax. So right here when I say leader just like this, this is essentially saying find whatever the leader key is. And this is very cool because say that you ever wanted to change your leader key you wouldn't have to go in to every single script, you can simply go change the leader variable and then it would auto-populate down here.

So what I'm saying is I have two options for window pane resizing. The first is to make it smaller the second is to make it bigger, or vice-versa, sorry, this one is going to extend it this one is going to shrink it right here. The way that the syntax goes is we have the leader key here followed by one of the brackets, the left bracket for making it larger and the right bracket for making it smaller.

So you can see what that looks like.

large

Then from there, I'm passing the exe command and notice that I start in this is kinda like we're starting in command mode and we're saying I want you to execute this command, and then we pass in what we want to execute. The command that we're passing in is resized, this is something that you could pass right into the system. So say that you had the file open and you had multiple panes open if you click resize this would do the exact same thing. So you could, you think of this just as a command that you can run inside of command mode in Vim.

After that, we're saying that we want to multiply the window height which this is something you have available and access to, we want to multiply the window height by 3/2. So this is gonna give us a larger window and right here we're saying that we actually want to shrink the window height and we want to multiply it by 2/3. You can change this up however you want. These are some numbers that I found useful but if you want to change yours to 1/4 or something like that definitely feel free to experiment with it and see how you like it.

So let's open up our gem file again, and running split pane, let's open up the same exact file, okay, so now we're here, let me switch into the our gem file. Now if I want to grow this size then what I can do is press the left key. I know you can't see me typing but what I'm pressing is comma followed by a bracket, that's all I'm doing, so comma followed by bracket is going to grow this.

Now if I want to shrink it I can do comma followed by the right bracket and it will shrink it. And so I can do this from either pane so I can go back to normal right here, and then switch and now I can shrink this size. So I can do it just like that, and that gives me exactly what I need and so now we can have a fully customized kind of window pane sizing situation within Vim and you learned how to use custom key bindings in order to accomplish it.