Ad-hoc keymapping in Vim

Ever since I saw a screencast by Gary Bernhardt from Destroy All Software, I’ve been using his awesomely handy key mapping trick in vim while writing scripts.

If you’re like me, you’ll spend most of your time writing small shell/bash scripts. I typically would write/edit my script, save my work, switch to another pane in iTerm2, test the script, back to my editing pane, edit, save, switch pane, test… repeat.

How about just create an ad-hoc mapping to do both saving and execution of the script all in one go?

In vim

:map ,s :w\|!./script_name.sh<cr>

That’s it.

Now, every time you’re done editing, hit ‘,s’ and you’ll save and execute the script, all in one go.

Tags: linux vim vi