diff options
author | 2022-10-03 21:42:20 +0530 | |
---|---|---|
committer | 2022-10-03 21:42:20 +0530 | |
commit | af120ab348f2e1a5a39dec035ed9dcf84189a64e (patch) | |
tree | 2a3aadd7ce1b7b771dfe3fe7c983569726c8d7ed /zsh/oh-my-zsh/plugins/magic-enter | |
download | dotfiles-af120ab348f2e1a5a39dec035ed9dcf84189a64e.tar.gz dotfiles-af120ab348f2e1a5a39dec035ed9dcf84189a64e.tar.bz2 dotfiles-af120ab348f2e1a5a39dec035ed9dcf84189a64e.zip |
dotfiles
Diffstat (limited to 'zsh/oh-my-zsh/plugins/magic-enter')
-rw-r--r-- | zsh/oh-my-zsh/plugins/magic-enter/README.md | 17 | ||||
-rw-r--r-- | zsh/oh-my-zsh/plugins/magic-enter/magic-enter.plugin.zsh | 38 |
2 files changed, 55 insertions, 0 deletions
diff --git a/zsh/oh-my-zsh/plugins/magic-enter/README.md b/zsh/oh-my-zsh/plugins/magic-enter/README.md new file mode 100644 index 0000000..78514c6 --- /dev/null +++ b/zsh/oh-my-zsh/plugins/magic-enter/README.md @@ -0,0 +1,17 @@ +## Magic Enter plugin + +This plugin makes your enter key magical, by binding commonly used commands to it. + +To use it, add `magic-enter` to the plugins array in your zshrc file. You can set the +commands to be run in your .zshrc, before the line containing plugins. If no command +is specified in a git directory, `git status` is executed; in other directories, `ls`. + +```zsh +# defaults +MAGIC_ENTER_GIT_COMMAND='git status -u .' +MAGIC_ENTER_OTHER_COMMAND='ls -lh .' + +plugins=(... magic-enter) +``` + +**Maintainer:** [@dufferzafar](https://github.com/dufferzafar) diff --git a/zsh/oh-my-zsh/plugins/magic-enter/magic-enter.plugin.zsh b/zsh/oh-my-zsh/plugins/magic-enter/magic-enter.plugin.zsh new file mode 100644 index 0000000..55b8935 --- /dev/null +++ b/zsh/oh-my-zsh/plugins/magic-enter/magic-enter.plugin.zsh @@ -0,0 +1,38 @@ +# Default commands +: ${MAGIC_ENTER_GIT_COMMAND:="git status -u ."} # run when in a git repository +: ${MAGIC_ENTER_OTHER_COMMAND:="ls -lh ."} # run anywhere else + +magic-enter() { + # Only run MAGIC_ENTER commands when in PS1 and command line is empty + # http://zsh.sourceforge.net/Doc/Release/Zsh-Line-Editor.html#User_002dDefined-Widgets + if [[ -n "$BUFFER" || "$CONTEXT" != start ]]; then + return + fi + + if command git rev-parse --is-inside-work-tree &>/dev/null; then + BUFFER="$MAGIC_ENTER_GIT_COMMAND" + else + BUFFER="$MAGIC_ENTER_OTHER_COMMAND" + fi +} + +# Wrapper for the accept-line zle widget (run when pressing Enter) + +# If the wrapper already exists don't redefine it +(( ! ${+functions[_magic-enter_accept-line]} )) || return 0 + +case "$widgets[accept-line]" in + # Override the current accept-line widget, calling the old one + user:*) zle -N _magic-enter_orig_accept-line "${widgets[accept-line]#user:}" + function _magic-enter_accept-line() { + magic-enter + zle _magic-enter_orig_accept-line -- "$@" + } ;; + # If no user widget defined, call the original accept-line widget + builtin) function _magic-enter_accept-line() { + magic-enter + zle .accept-line + } ;; +esac + +zle -N accept-line _magic-enter_accept-line |