简体   繁体   English

别名一切= <C-a> bash或zsh中的“ sudo”

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

Are there any ways to alias a string with ^A, ^E, etc? 有什么方法可以用^ A,^ E等为字符串加上别名? Or something like this via keyboard shortcut? 还是通过键盘快捷键这样的东西?

You could use GNU Readline to do what you want to do. 您可以使用GNU Readline来完成您想做的事情。 In Bash, you could use the bind builtin to display ( bind -P ) or modify the readline key bindings. 在Bash中,可以使用内置bind来显示( bind -P )或修改readline键绑定。 For instance, bind '"\\e[15~":"top\\Cm"' would map F5 to "top\\n". 例如, bind '"\\e[15~":"top\\Cm"'会将F5映射到“ top \\ n”。 You can also use the inputrc configuration file to store these settings. 您还可以使用inputrc配置文件存储这些设置。

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. zsh ,情况会略有不同,因为zsh不使用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. 相反,它使用自己更强大的zle:命令和语法可能略有不同,但结果应该大致相同。

In zsh I use the following config to make two Esc do something similar to what you described: 在zsh中,我使用以下配置使两个Esc执行与您所描述的类似的操作:

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 "

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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