浏览代码

feat(tldr): add tldr plugin (#12429)

danielwerg 11 月之前
父节点
当前提交
dfb436b54a
共有 2 个文件被更改,包括 34 次插入0 次删除
  1. 15 0
      plugins/tldr/README.md
  2. 19 0
      plugins/tldr/tldr.plugin.zsh

+ 15 - 0
plugins/tldr/README.md

@@ -0,0 +1,15 @@
+# tldr plugin
+
+This plugin adds a shortcut to insert tldr before the previous command.
+Heavily inspired from [Man plugin](https://github.com/ohmyzsh/ohmyzsh/tree/master/plugins/man).
+
+To use it, add `tldr` to the plugins array in your zshrc file:
+
+```zsh
+plugins=(... tldr)
+```
+
+# Keyboard Shortcuts
+| Shortcut                           | Description                                                                |
+|------------------------------------|----------------------------------------------------------------------------|
+| <kbd>Esc</kbd> + tldr              | add tldr before the previous command to see the tldr page for this command |

+ 19 - 0
plugins/tldr/tldr.plugin.zsh

@@ -0,0 +1,19 @@
+tldr-command-line() {
+    # if there is no command typed, use the last command
+    [[ -z "$BUFFER" ]] && zle up-history
+
+    # if typed command begins with tldr, do nothing
+    [[ "$BUFFER" = tldr\ * ]] && return
+
+    # get command and possible subcommand
+    # http://zsh.sourceforge.net/Doc/Release/Expansion.html#Parameter-Expansion-Flags
+    local -a args
+    args=(${${(Az)BUFFER}[1]} ${${(Az)BUFFER}[2]})
+
+    BUFFER="tldr ${args[1]}"
+}
+
+zle -N tldr-command-line
+# Defined shortcut keys: [Esc]tldr
+bindkey "\e"tldr tldr-command-line
+