Wikia

Vim Tips Wiki

Use vi shortcuts in terminal

Talk0
1,612pages on
this wiki
Revision as of 03:52, January 6, 2010 by JohnBot (Talk | contribs)

Tip 1220 Printable Monobook Previous Next

created May 2, 2006 · complexity basic · author peter jankuliak (inetic) · version 5.7


I got addicted to using Vim shortcuts like h,j,k,l, b (back one word), e (forward one word), and I miss it every time when I'm not in Vim. But there is a way to use basic vi shortcuts when you are in a command line. All you have to do, is write the following lines to /etc/inputrc or ~/.inputrc:

set editing-mode vi
set keymap vi

Now exit the terminal and come back. The main difference is that by default you are in insert mode and you have to pres Esc to get to normal mode.

More info: http://docs.freebsd.org/info/readline/readline.info.Readline_Init_File_Syntax.html

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