Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The way to distinguish command-mode and insert-mode in Bash's Vi command line editing

I'm always little bit confused when bash in vi-mode is switched to insert-mode, because it doesn't give any tip about used mode (command or edit). Is there any way to distinguish mods? May be automatic change of cursor color or something like that?

like image 568
chuwy Avatar asked Oct 25 '11 11:10

chuwy


People also ask

What are the difference between command mode and insert mode in vi commands?

Insert mode is the mode to be in when inserting text into the file. Command mode is the mode to be in when giving commands which will move the cursor, delete text, copy and paste, save the file etc. When entering a file, vi is in command mode. To enter text, you must enter insert mode.

What is the command to determine which mode you are in when using vi editor?

vi Editor Insert mode: You can switch to the Insert mode from the command mode by pressing 'i' on the keyboard. Once you are in Insert mode, any key would be taken as an input for the file on which you are currently working. To return to the command mode and save the changes you have made you need to press the Esc key.

How do we exit Edit mode and enter command mode in vi editor?

Press Esc to enter Command mode, and then type :wq to write and quit the file. The other, quicker option is to use the keyboard shortcut ZZ to write and quit. To the non-vi initiated, write means save, and quit means exit vi.


2 Answers

in /etc/inputrc (or ~/.inputrc) add this:

set show-mode-in-prompt on 

this will prefix your prompt with + while in insert-mode, and : while in command mode in bash 4.3

EDIT: in the latest version of bash 4.4, you will instead get a prompt prefixed with "(ins)" or "(cmd)" by default. but, you can change that:

set vi-ins-mode-string "+" set vi-cmd-mode-string ":" 

also, you can use color codes like '\e[1;31m', but surround them with '\1' and '\2' to keep readline happy:

set vi-cmd-mode-string "\1\e[1;31m\2:\1\e[0m\2" 
like image 77
Isaac Hanson Avatar answered Oct 05 '22 17:10

Isaac Hanson


Building on @Isaac Hanson's answer you can set the cursor style to reflect the mode (just like in VIM) by setting these in your .inputrc:

set editing-mode vi set show-mode-in-prompt on set vi-ins-mode-string \1\e[6 q\2 set vi-cmd-mode-string \1\e[2 q\2  # optionally: # switch to block cursor before executing a command set keymap vi-insert RETURN: "\e\n" 

This will give you a beam cursor in insert mode or a block cursor for normal mode.

Other options (replace the number after \e[):

        Ps = 0  -> blinking block.         Ps = 1  -> blinking block (default).         Ps = 2  -> steady block.         Ps = 3  -> blinking underline.         Ps = 4  -> steady underline.         Ps = 5  -> blinking bar (xterm).         Ps = 6  -> steady bar (xterm). 

Your terminal must support DECSCURSR (like xterm, urxvt, iTerm2). TMUX also supports these (if you set TERM=xterm-256color outside tmux).

like image 20
laktak Avatar answered Oct 05 '22 19:10

laktak