puk
puk

Reputation: 16802

Force bash to use .vimrc in vi mode

Bash can be set to operate in vim mode . However, my .vimrc is no longer used. Specifically, my mapping of jj to Esc no longer works. I have to press Esc to exit to insert mode. How do I tell bash to use my .vimrc file?

Upvotes: 16

Views: 5685

Answers (4)

Leonard
Leonard

Reputation: 13787

While the accepted answer has pointers, the cut-to-the chase answer is as follows:

In your .inputrc file, put

set keymap vi-insert

"jj": "\e"

You need to force the re-read of your .inputrc file. This can be done by closing your terminal and re-opening it. For prolonged experimentation with .inputrc file, so you don't have to log out and then back in, put this in too:

"\C-x\C-r": re-read-init-file

Then you can use Control-x, control-r to reread the init file and not have to log out and back in.

Upvotes: 2

aidan
aidan

Reputation: 1795

@sehe definitely has the most direct answer. BUT if you'd rather not mess with the .inputrc, there are at least two other options

  • v, which takes whatever commands you've written so far, or nothing at all, into the editor defined in your .bash_profile (use this line: EDITOR=/usr/bin/vim). save and quit executes the command. EDIT: this is to be executed from normal mode, so you'll still have to press ESC to get there :(

  • fc, which is the handy 'fix command' command. more on that one in man bash. EDIT: This one you type in insert mode.

Upvotes: 3

sehe
sehe

Reputation: 393924

You are looking for bash-'s vi mode (which is just that: a vi input mode for bash, and has nothing at all to do with vi or vim).

It does have to do with readline/inputrc as far as I know so you could see whether you can

  • bind keys the bash way
  • from ~/.inputrc

links:

The last link contains a somewhat more advanced example of a .inputrc for use with 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"

Upvotes: 10

chemila
chemila

Reputation: 4351

in your .bashrc:

set -o vi

vim mode for bash

Upvotes: -2

Related Questions