Tyler Benziger

Better ctrl-z for Vim

Aug 18 2014

I stumbled onto “How to boost your Vim productivity” the other day and it’s full of awesome Vim tips. One of them involved improving ctrl-z.

For those of you who don’t know. You can hit ctrl-z from within Vim and it puts Vim in the background and returns you to your shell. You can type in the command fg into your shell to get back into Vim. I use this workflow all the time. It’s much nicer to use an actual console to do file system stuff then many of the other Vim-specific workarounds out there (although I will say that vim-eunuch is awesome).

One huge problem with this approach is typing fg<CR> is annoying. And I never knew it until I read this article. The author has managed to map ctrl-z to not only send Vim into background mode but also return to Vim when you’re ready.

He achieves this through ZSH scripting. I’m a dedicated iTerm2 and BASH user, so I was immediately disappointed that this awesome tip didn’t apply to me.

So I figured out how to do it in BASH and iTerm2. You can take a look here: https://github.com/tybenz/ctrl-z.

It uses iTerm2’s custom key bindings with something called a coprocess. The coprocess itself is just a BASH script. Feel free to check it out. If you follow the steps, you’ll be up-and-running in no time and using ctrl-z up and down the street. Good luck nerds!