Wikia

Vim Tips Wiki

Use vi shortcuts in terminal

Talk0
1,613pages on
this wiki
Revision as of 07:29, June 14, 2013 by Magicmat (Talk | contribs)

Many bash power users have come to learn the shortcuts for navigating and editing input in bash: C-a jumps to the start of the line, C-e jumps to the end, and so on. However, these are all shortcuts taken from Emacs. It's possible to tell bash to use Vi shortcuts instead. Not only will the keybindings match what you're used to, but you'll also have modal editing, just like Vi: type text in insert mode, then press Esc to enter command mode to quickly edit this text.

To enable it, edit (or create) the file ~/.inputrc or /etc/inputrc and add the following lines

set editing-mode vi
set keymap vi

Restart your terminal and you'll have Vi keybindings. By default, you're in insert mode; press Esc to enter command mode. You may want to keep a cheat sheet of default keymappings handy, since they do differ from Vi/Vim occasionally.

To see a list of the keymappings currently active for you, use the command bind -p | grep -v '^#\|self-insert\|^$'. Important: these keybindings will change if you're in insert or command mode. It is recommended that you run the command once with insert mode active, and once when command mode is active.

A reference to adjusting the options and mappings of the readline library bash uses for text editing can be found in the GNU bash man page.

Comments

Some more interesting keys from my ~/.inputrc (works in cygwin/linux bash).

# Edit options before rerunning previous command, eg: ls a b c -> ls -al a b c
"\e-": "\C-p\C-a\M-f "

# Cycle thru completions.
"\e/": menu-complete

# glob patterns without executing, eg: 'rm *x'
"\ee": glob-expand-word

# Vim style history search
"\e[A": history-search-backward
"\e[B": history-search-forward

"\e[C": forward-char
"\e[D": backward-char

# Two escapes clear command line.
"\e\e": "\C-a\C-k"

If you use zsh (and you really should!) you can add to your .zshrc:

bindkey -v

to set editing mode to vi. I think this is useful because zsh does not use readline like bash etc do.


I'm sure all bash users will be familiar with:

set -o vi

Ought to be noted that you can do the same with the Korn Shell (ksh). For automatic vi keystrokes, edit $HOME/.profile or whatever your $HOME/$ENV script is.


Quick clarification for ksh -- that's "set -o vi" you need to add to .profile or $ENV script.


To summarize: As I understand, setting file ~/.inputrc or /etc/inputrc will set Vi mode to all programs that use 'readline' library. From what i know it's bash, pgsql, mysql, ... + I like to use Term::ReadLine module in perl and therefore Vi mode works right a way for all of my programs. Setting up 'set -o vi' in ~/.bashrc will set it only for bash (the same goes for ksh and zsh setting i guess).


The problem is that the bindings do not work with vi-mode. Had to change the sequences to VI sequences:

set editing-mode vi
set keymap vi-insert

# Edit options before rerunning previous command, eg: ls a b c -> ls -al a b c
"\C-o": "\e-0eli "

# Cycle thru completions.
"\C-a": menu-complete

# glob patterns without executing, eg: 'rm *x'
"\C-e": glob-expand-word

# Vim style history search (Already Default)
#"\e[A": history-search-backward
#"\e[B": history-search-forward

#"\e[C": forward-char
#"\e[D": backward-char

# Two escapes clear command line. (Use C-c or \eC\eC instead)
"\e\e": "\e0\C-ki"

bindkey -v

Is also the method for tcsh.


Around Wikia's network

Random Wiki