Another little bit of
vim configuration I wrote, this bit of code helps me to re-size a
vim process running in a terminal.
As I mentioned before, I ended up running
vim mostly in terminals, because next to editing code, I was doing a lot of other shell work. So, for a quick fix or small edit, the usual terminal size I use (classic 80 x 24), is more than enough, but for bigger jobs, I want more space.
Re-sizing the terminal window with the mouse and setting some
vim settings (especially turning on line numbers) is too tedious, so instead, I set my heart on having a keyboard shortcut to automatically size
vim up and down on demand.
vim and most modern terminal emulators can cooperate in this sense, it is sufficient to set the window size in
vim, and the terminal emulator will resize accordingly. The following function does just that, and a tad more:
1 " Function to size up. 2 function SizeUpFunc() 3 if exists("g:oldColumns") 4 return 5 endif 6 " Save the current width. 7 let g:oldColumns = &columns 8 let g:oldLines = &lines 9 " Reset column size when Vim quits. 10 au VimLeave * SizeDown 11 " Bigger width to make room for line numbers and the sign markers. 12 set columns=87 lines=999 13 " Turn on line numbers. 14 set number 15 endfunction 16 command SizeUp call SizeUpFunc()The code is pretty simple. I use two global variables,
oldLines, to store the original width and height of the terminal. If
oldColumnsalready exists, I know that
SizeUpFunc()has already been executed, and stop right there.
On line 10 I register the command
SizeDown to the
VimLeave event, i.e. when
vim exits. This way, when I quit
vim, the terminal will return to its original size.
The sizes on line 12 are simply my preference: I like the width to be wide enough to house a default line length (78 is my preference), plus enough space for the numbers on the left, and some space on the right to mark lines that are too long (using the colorcolumn feature in
vim 7.3). As for the height, 999 is obviously way more than can fit on my screen, but this makes sure that
vim will resize the terminal to the maximum possible height.
For convenience, I define a command called
SizeUp on line 16.
Now, let’s move on the the counterpart to the above function, which sizes
vim and the terminal down again.
1 " Function to size down. 2 function SizeDownFunc() 3 if !exists("g:oldColumns") 4 return 5 endif 6 " Restore the original size. 7 let &columns = g:oldColumns 8 let &lines = g:oldLines 9 " Remove the variable. 10 unlet g:oldColumns 11 unlet g:oldLines 12 endfunction 13 command SizeDown call SizeDownFunc()Again, I use
oldColumnsto see, if the terminal was resized or not, although this time, it’s the absence of that variable, that will cause the function to terminate early.
For the rest, I simply restore the old values and finally remove the global variables, to signify that, indeed, the terminal is back to what it used to be. And for good measure, I defined a command to go with this function as well.
To make my life easier, I mapped the commands to keyboard shortcuts:
1 " Mappings for SizeUp and SizeDown. 2 map <C-Up> :SizeUp<CR> 3 map <1b>Oa :SizeUp<CR> 4 map <C-Down> :SizeDown<CR> 5 map <1b>Ob :SizeDown<CR>I had to use both symbolic names and byte-codes to get it to work, so if you’re trying to do something similar, your milage may vary with the above codes.
Lastly, because I am inherently lazy, I’ve added a little feature to start up
vim in “expanded” mode. Using an environment variable, I tell
vim to size up on startup:
1 " Resizing. 2 if $_VIMEXPAND == 1 3 SizeUp 4 endifI use this together with an alias, which sets the environment variable before calling
And that’s it for this little