Jon*_*han 1 zsh keyboard-shortcuts oh-my-zsh
我经常发现自己想要重复一个命令,虽然!!很有用,但我想将其绑定到 ctrl-w 或类似的东西。有没有办法做到这一点?
编辑:我知道向上箭头可以实现我想要的功能,但是我不想离开主行。作为一名狂热的 Vim 用户,我知道了坚持使用 Home 键的价值。
我查看了这篇关于添加访问命令的快捷方式的文章info,并尝试从中推断出一些内容,但没有成功。Zsh 对我大喊大叫,说 zle 不活跃什么的。
我知道这将依赖于我的 shell 的配置方式的知识,因此下面我粘贴了一些相关代码,以及整个 .zshrc 和点文件的链接。
# oh-my-zsh plugins. zsh-aliases and drush are custom plugins.
plugins=( git z tmux web-search colored-man zsh-aliases drush)
ZSH_TMUX_AUTOSTART=true
#... $PATH, start background process (clipboard integration for tmux,
# glances system monitor), history options, editor, all truncated for brevity.
# use vim mode
bindkey -v
#show insert/normal mode in prompt
function zle-line-init zle-keymap-select {
RPS1="${${KEYMAP/vicmd/NORMAL}/(main|viins)/INSERT}"
RPS2=$RPS1
zle reset-prompt
}
zle -N zle-line-init
zle -N zle-keymap-select
# rebind ctrl-r
bindkey -M vicmd '^R' history-incremental-search-backward
bindkey -M viins '^R' history-incremental-search-backward
Run Code Online (Sandbox Code Playgroud)
完整配置: https: //github.com/yramagicman/dotfiles
只是 .zshrc:https://github.com/yramagicman/dotfiles/blob/master/.zshrc
自定义插件:
要从历史记录中获取最后一个命令,您可以使用该up-history小部件。默认情况下,它在vicmd模式下绑定到Ctrl+ ,因此按+然后(调用 widget )就可以了。PEscCtrlPEnteraccept-line
如果您想将其绑定到单个快捷方式,则必须编写自己的小部件。您可以将其添加到您的~/.zshrc:
# define function that retrieves and runs last command
function run-again {
# get previous history item
zle up-history
# confirm command
zle accept-line
}
# define run-again widget from function of the same name
zle -N run-again
# bind widget to Ctrl+X in viins mode
bindkey -M viins '^X' run-again
# bind widget to Ctrl+X in vicmd mode
bindkey -M vicmd '^X' run-again
Run Code Online (Sandbox Code Playgroud)
对于这个例子,我选择Ctrl+X作为快捷方式,因为默认情况下它在vicmd模式下未绑定,并在viins模式下自我插入,而Ctrl+W已经绑定到viinsvi-backward-kill-word中。当然,如果您不使用默认绑定或仅在模式下绑定小部件,您可以覆盖默认绑定。