diff options
author | Alex Matheson <Alex.kmatheson@gmail.com> | 2021-12-29 07:12:52 -0600 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-12-29 14:12:52 +0100 |
commit | ce9104c4f3d9ea424346d711f0653cd9e5d81303 (patch) | |
tree | 245e5b16ec2ddd201ab0bc545eff2d46596cb277 /plugins/npm | |
parent | 2acb3071cade6395c1dbcee31b6b8d198ebed66a (diff) | |
download | zsh-ce9104c4f3d9ea424346d711f0653cd9e5d81303.tar.gz zsh-ce9104c4f3d9ea424346d711f0653cd9e5d81303.tar.bz2 zsh-ce9104c4f3d9ea424346d711f0653cd9e5d81303.zip |
feat(npm): toggle `npm install` / `npm uninstall` by pressing F2 twice (#9717)
Co-authored-by: Marc Cornellà <hello@mcornella.com>
Diffstat (limited to 'plugins/npm')
-rw-r--r-- | plugins/npm/README.md | 17 | ||||
-rw-r--r-- | plugins/npm/npm.plugin.zsh | 40 |
2 files changed, 57 insertions, 0 deletions
diff --git a/plugins/npm/README.md b/plugins/npm/README.md index e970c3c7a..8eafc6d61 100644 --- a/plugins/npm/README.md +++ b/plugins/npm/README.md @@ -29,3 +29,20 @@ plugins=(... npm) | `npmI` | `npm init` | Run npm init | | `npmi` | `npm info` | Run npm info | | `npmSe` | `npm search` | Run npm search | + +## `npm install` / `npm uninstall` toggle + +The plugin adds a function that toggles between `npm install` and `npm uninstall` in +the current command or the last command, for up to 2 previous commands. **The default +key binding is pressing <kbd>F2</kbd> twice**. + +You can change this key binding by adding the following line to your zshrc file: + +```zsh +bindkey -M emacs '<seq>' npm_toggle_install_uninstall +bindkey -M vicmd '<seq>' npm_toggle_install_uninstall +bindkey -M viins '<seq>' npm_toggle_install_uninstall +``` + +where `<seq>` is a key sequence obtained by running `cat` and pressing the keyboard +sequence you want. diff --git a/plugins/npm/npm.plugin.zsh b/plugins/npm/npm.plugin.zsh index f7d6d3939..6c768d614 100644 --- a/plugins/npm/npm.plugin.zsh +++ b/plugins/npm/npm.plugin.zsh @@ -69,3 +69,43 @@ alias npmi="npm info" # Run npm search alias npmSe="npm search" + +npm_toggle_install_uninstall() { + # Look up to the previous 2 history commands + local line + for line in "$BUFFER" \ + "${history[$((HISTCMD-1))]}" \ + "${history[$((HISTCMD-2))]}" + do + case "$line" in + "npm uninstall"*) + BUFFER="${line/npm uninstall/npm install}" + (( CURSOR = CURSOR + 2 )) # uninstall -> install: 2 chars removed + ;; + "npm install"*) + BUFFER="${line/npm install/npm uninstall}" + (( CURSOR = CURSOR + 2 )) # install -> uninstall: 2 chars added + ;; + "npm un "*) + BUFFER="${line/npm un/npm install}" + (( CURSOR = CURSOR + 5 )) # un -> install: 5 chars added + ;; + "npm i "*) + BUFFER="${line/npm i/npm uninstall}" + (( CURSOR = CURSOR + 8 )) # i -> uninstall: 8 chars added + ;; + *) continue ;; + esac + return 0 + done + + BUFFER="npm install" + CURSOR=${#BUFFER} +} + +zle -N npm_toggle_install_uninstall + +# Defined shortcut keys: [F2] [F2] +bindkey -M emacs '^[OQ^[OQ' npm_toggle_install_uninstall +bindkey -M vicmd '^[OQ^[OQ' npm_toggle_install_uninstall +bindkey -M viins '^[OQ^[OQ' npm_toggle_install_uninstall |