简体   繁体   中英

Alias anything=<C-a>“sudo ”in bash or zsh

Are there any ways to alias a string with ^A, ^E, etc? Or something like this via keyboard shortcut?

You could use GNU Readline to do what you want to do. In Bash, you could use the bind builtin to display ( bind -P ) or modify the readline key bindings. For instance, bind '"\\e[15~":"top\\Cm"' would map F5 to "top\\n". You can also use the inputrc configuration file to store these settings.

This is not an alias but it might lead you to what you want.

On zsh , things will be slightly different as zsh does not use readline. Instead it uses its own and more powerful zle : the commands and the syntax might be slightly different but the result should be roughly the same.

In zsh I use the following config to make two Esc do something similar to what you described:

sudo-command-line() {
    [[ -z $BUFFER ]] && zle up-history
    [[ $BUFFER != sudo\ * ]] && BUFFER="sudo ${BUFFER% }"
    zle end-of-line
}
zle -N sudo-command-line
bindkey "\e\e" sudo-command-line

对于zsh,请使用bindkey -s

bindkey -s "^A" "sudo "

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM