Making a Window Submode in Vim
I found a plugin that is changing my Vim-tire life! This plugin is so awesome it should be built into default Vim. What does the plugin do? It enables the creation of new submodes. Why would a person want more modes?!? Isn’t dealing with modes the main deterrent for new Vim users? Isn’t Normal, Insert, Command-line, Visual, Select, and Operator-pending enough? (Did I miss one?) Let’s try out a new submode and see what happens.
Window commands are prefixed with
<C-w>. Want to create a horizontal split?
<C-w>s, didn’t mean to do that and want to do vertical split?
Want to resize the vertical split
50<C-w>>? Too wide? Narrow it with
Move back to the other window?
Are your fingers getting tired? After I get the windows just right using default mappings my fingers are crying for mercy.
Here’s a short list of common default window commands:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
For a comprehensive list of window commands try
The most common solution to window-command-itis is to map other keys to these
common actions so to include the
1 2 3 4 5
1 2 3 4
This has been the accepted solution for most, but it takes away so many
convenient keys. And in some cases, it even overrides default behaviour.
<C-L>, I miss you.
C-H, isn’t that also
<BS>? Guess I won’t be using
Solution B – Submode to the Rescue
This entire solution depends on kana/vim-submode,
I consider it one of Japan’s national treasures along with ninjas and ramen.
Unfortunately, Kana’s example use of submodes is a little underwhelming:
g+. I agree with the author that using
g+ is not convenient, and using
g++++-++-+ is easier, but the solution
for that was simply
<C-R>. I feel a better application for a new
submode is window management. Imagine if resizing a split was
<C-w>------=->>>>>>>><> or changing cursor location was
or moving was
<C-w>HjKLkjh. Imagine no more!
First, install the plugin. If you’re not sure how to install a plugin, try
junegunn/vim-plug. Next, add the
following to your
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
:source $MYVIMRC, you’ll have a glorious new submode in Vim.
You can see I named it window mode. Can you guess how to get into window mode?
<C-w>, the normal prefix used to do any
wincmd. If this is too drastic, feel
free to change line #7 to something else. Just replace
<C-w> with a different
Let’s give it a test drive.
I know you can’t see what keys I’m pressing, but I guarantee I only pressed
<C-w> once. I also didn’t have to remember any new key bindings. The
hesitation in the demo is the resistance to hitting
<C-w> every time, which
I’ll get over soon enough.
But wait there’s more! In case I haven’t provided enough tips for one post,
here’s the overrides I have in
$MYVIMRC to make windowing even better.
1 2 3 4 5 6 7 8 9 10 11 12 13
Rainbows without Unicorns
While learning this new way of windowing, there have been a few negatives:
I forget that I’m in window mode and get disoriented when I think I’m moving the cursor within a buffer, but it jumps around to other splits.
For one off window commands, I have to hit an extra key to get out of window mode or wait for the timeout.
When I use some one else’s computer, I’m useless.
I think most of these annoyances will go away with time, and the benefits overtime in keystroke savings are non-trivial. As for #3, regardless of submodes, the brain freeze will never go away, because no one thinks as strangely as me, and that’s a Good Thing™.
Shout-out to Kana Natsuno, @kana1, http://whileimautomaton.net/, https://github.com/kana . None of this awesomeness would be possible without https://github.com/kana/vim-submode . She makes some totally sweet plug-ins. Check out her stuff. You won’t regret it!
Let me know what you think. Am I crazy? What other things deserve a submode? Hit me up in the comments below! Thanks for reading!