Question

I want to customize vi mode in bash. Two things I want to do very badly.

  1. Map Esc to CAPS_LOCK and CAPS_LOCK to SHIFT+CAPS_LOCK
  2. Use 'm' to mark current directory to a character 'a-z' and use ' to cd to that directory.

In general, is there a way to extend vi mode in bash?

Was it helpful?

Solution

Bash uses GNU readline to provide a usable command line prompt. Readline supports vi mode that provides a basic set of keys and a modal interface for it.

Mappings of caps lock and others is not bash's or readline's job. If you are willing to make those bindings global, you can use Xmodmap to achieve satisfactory results.

As for the second question: Unfortunately, the configurability of readline is very limited. But you could achieve something like that by writing functions that you init via a loop. The following kind of works:

Set_Ma () {
    DIR_a=`pwd`
}
Go_Ma (){
    cd "$DIR_a"
}
set -o vi
bind -m vi-command -x '"ma":"Set_Ma"'
bind -m vi-command -x '"'"'"'a":"Go_Ma"'

You won't see any effect immediately after typing 'a because it doesn't redraw the prompt to match new CWD. You could also use an associative array for storing the marks but I won't go there.

May I suggest jumping in the ZSH bandwagon. Zsh doesn't use readline. Instead they wrote a more flexible library for line editing that can be properly scripted by ordinary zsh functions.

OTHER TIPS

You can change options from within vi by using the ex command :set. In addition, whenever vi is started up, it reads a file in your home directory called .exrc for further operating instructions. By placing :set commands in this file, you can modify the way vi acts whenever you use it.

You can also set up .exrc files in local directories to initialize various options that you want to use in different environments. For example, you might define one set of options for editing English text, but another set for editing source programs. The .exrc file in your home directory will be executed first, then the one in your current directory.

Finally, any commands stored in the shell variable EXINIT will be executed by vi on startup. If there is a conflict between settings made in .exrc and EXINIT, those in .exrc take precedence.

Hope this might help you

Thanks

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top