A Weird Imagination

Making :w work everywhere

Posted in

The problem

After using Vim as my primary editor for a while, I find myself trying to use vi-style keyboard shortcuts in other contexts. Usually resulting in :w in middle of whatever I was writing as saving is a natural thing to do when pausing.

There's a few ways to fix this:

  1. Get used to the fact that not everything supports vi-style keyboard shortcuts.
  2. Change the keyboard shortcuts of the programs I do use.
  3. Use Vim for everything.

The first option is suboptimal because vi-style keyboard shortcuts are very useful. Luckily, in many cases there's ways to get them.

Native vi keybinding support

Very few programs support vi keybindings by default. There's vi and vim, of course, but other than that the only one I know of is the hex editor hexer.

GNU Readline and Bash both have options for it that can be enabled:

In your ~/.inputrc put

set editing-mode vi
set keymap vi

which will make GNU Readline act like vi. GNU Readline is used by most programs that accept interactive text input, so that will add vi-style line editing to programs like python even though they don't include explicit support for it.

In your ~/.bashrc add the line

set -o vi

(while you're there, you probably also want a line like

export EDITOR="gvim -f"

or

export EDITOR="vim"

to use gvim or vim as your default editor for things like editing commit messages. The -f argument to gvim makes it run in the foreground because the calling process will assume you are done when the command exits. If opening a graphical window fails it will show an error and default to running the console version of vim.)

You can also edit the current line in your editor by hitting Esc v (or just v if you're already in command mode) to get the full features of Vim.

Plugins for vi keybindings

While most programs do not include vi keybindings, many do have plugins to add them. Here's plugins for a few programs, but some searching will likely find one for any reasonably popular program.

Emacs

As the old joke goes, Emacs is a great operating system, lacking only a decent editor. The solution, of course, is Evil, a vi mode for Emacs that includes many Vim features.

Visual Studio

For Microsoft Visual Studio 2010 or later, the VsVim extension adds vi keybindings to the editor. It will even read your _vimrc for settings, although the support for Vim features is, of course, limited.

Other applications

Applications that are not editors sometimes also have vi keybinding plugins. For instance, there's Vimperator for Mozilla Firefox along with other similar plugins for Firefox and other web browsers. For the Mozilla Thunderbird email client, there's Muttator. There's even a Word template called viWord.

Web browsers do contain textareas for editing text, and you might want vi keybindings when editing text in them. The It's All Text! extension for Firefox will let you edit textareas with an external editor like gvim. The situation for Chrome/Chromium is a little more complicated.

Extending Vim

The other way to deal with an application not acting enough like Vim is to stop using it and use Vim instead. Okay, that's not exactly a solution, but it's part of one. Vim with the proper setup can have much of the functionality of an IDE. Specifically, there exist plugins for accessing the functionality of Visual Studio and Eclipse through Vim. Then the user interactions are through Vim's interface, not the other program's, so you get the familiar interface and customization support as well as the desired IDE features.

Comments

Have something to add? Post a comment by sending an email to comments@aweirdimagination.net. You may use Markdown for formatting.

There are no comments yet.