Using Vim Keybindings on the Bash Command line in a remote shell

January 4, 2016

The core philosophy of the Vim editor is accomplishing editing tasks with as few keystrokes and hand motions as possible.
Commands are short and succinct to minimize hand motions and keystrokes, and moving your hands to a mouse or even the arrow keys on your keyboard takes away precious seconds or fractions of a second that distract your brain from your editing task at hand. And once you've become proficient at this form of editing, using a standard editor is just not the same. There are even ways to make your browser more like Vim. Taken to the terminal, one essential tool that I now can't live without while working on the command line is Bash's Vi editing mode.

To enable command line Vi, just set Bash's editing mode.

set -o vi

(Incidentally you can also use emacs keybindings if that's your cup of tea)

set -o emacs

Now instead of hitting ↑  &  ↓ arrows, you can use Vi's standard k and j. For insert mode hit i. To erase a character hit x. To go forward and back a word w and b. To change a word cw. You get the point. It's just like Vi, and trust me, if you're a Vi user this is something you can't live without.

Since I'm logging into remote servers on a daily basis I started editing the .bash_profile on all of those servers to run the set -o vi command. However this became cumbersome, and further I'd often log into a team's server where I wasn't comfortable changing shared settings.

To get around this I came across this quick little snippet to force Vi mode on any remote ssh session. This line can be added to your local .bash_profile:

function sshv() { ssh "$@" -t bash -o vi ;}

The "$@" preserves any normal ssh options, eg. user@hostname or anything else. The rest of it tells ssh to log you in to a bash shell with the vi editor option set.

Now, instead of typing ssh me@remote-server, replace that with

sshv me@remote-server

and you'll have a vi-enabled terminal for just that session.

Next Post >

comments powered by Disqus

About This Site

This site was designed by We Are How.

This site is powered by Sculpin static site generator and the source is available here.

Yotta = 10^24, or 1 000 000 000 000 000 000 000 000, the largest metric prefix.


Get in touch to find out how we can help you refine your vision and implement a dynamite product that will help your business grow. Our agile product development process is thoughtfully designed to give clients ongoing feedback and visibility from project inception to completion.