ce9104c4f3d9ea424346d711f0653cd9e5d81303

Author
Alex Matheson <Alex.kmatheson@gmail.com>
Committer
GitHub <noreply@github.com>
Date

Message

feat(npm): toggle `npm install` / `npm uninstall` by pressing F2 twice (#9717)

Co-authored-by: Marc Cornellà <hello@mcornella.com>

Diff

 1diff --git a/plugins/npm/README.md b/plugins/npm/README.md
 2index e970c3c7af2e7d667349bfe0b395e7851b5f8721..8eafc6d6127ac8c84b14941bc75151e853cc060e 100644
 3--- a/plugins/npm/README.md
 4+++ b/plugins/npm/README.md
 5@@ -29,3 +29,20 @@ plugins=(... npm)
 6 | `npmI`  | `npm init`                   | Run npm init                                                    |
 7 | `npmi`  | `npm info`                   | Run npm info                                                    |
 8 | `npmSe` | `npm search`                 | Run npm search                                                  |
 9+
10+## `npm install` / `npm uninstall` toggle
11+
12+The plugin adds a function that toggles between `npm install` and `npm uninstall` in
13+the current command or the last command, for up to 2 previous commands. **The default
14+key binding is pressing <kbd>F2</kbd> twice**.
15+
16+You can change this key binding by adding the following line to your zshrc file:
17+
18+```zsh
19+bindkey -M emacs '<seq>' npm_toggle_install_uninstall
20+bindkey -M vicmd '<seq>' npm_toggle_install_uninstall
21+bindkey -M viins '<seq>' npm_toggle_install_uninstall
22+```
23+
24+where `<seq>` is a key sequence obtained by running `cat` and pressing the keyboard
25+sequence you want.
26diff --git a/plugins/npm/npm.plugin.zsh b/plugins/npm/npm.plugin.zsh
27index f7d6d393978d17027afe923ad5dbdbba66519cec..6c768d614f5f1c60615a4eda7ef9da054e3225a9 100644
28--- a/plugins/npm/npm.plugin.zsh
29+++ b/plugins/npm/npm.plugin.zsh
30@@ -69,3 +69,43 @@ alias npmi="npm info"
31 
32 # Run npm search
33 alias npmSe="npm search"
34+
35+npm_toggle_install_uninstall() {
36+  # Look up to the previous 2 history commands
37+  local line
38+  for line in "$BUFFER" \
39+    "${history[$((HISTCMD-1))]}" \
40+    "${history[$((HISTCMD-2))]}"
41+  do
42+    case "$line" in
43+      "npm uninstall"*)
44+        BUFFER="${line/npm uninstall/npm install}"
45+        (( CURSOR = CURSOR + 2 )) # uninstall -> install: 2 chars removed
46+        ;;
47+      "npm install"*)
48+        BUFFER="${line/npm install/npm uninstall}"
49+        (( CURSOR = CURSOR + 2 )) # install -> uninstall: 2 chars added
50+        ;;
51+      "npm un "*)
52+        BUFFER="${line/npm un/npm install}"
53+        (( CURSOR = CURSOR + 5 )) # un -> install: 5 chars added
54+        ;;
55+      "npm i "*)
56+        BUFFER="${line/npm i/npm uninstall}"
57+        (( CURSOR = CURSOR + 8 )) # i -> uninstall: 8 chars added
58+        ;;
59+      *) continue ;;
60+    esac
61+    return 0
62+  done
63+
64+  BUFFER="npm install"
65+  CURSOR=${#BUFFER}
66+}
67+
68+zle -N npm_toggle_install_uninstall
69+
70+# Defined shortcut keys: [F2] [F2]
71+bindkey -M emacs '^[OQ^[OQ' npm_toggle_install_uninstall
72+bindkey -M vicmd '^[OQ^[OQ' npm_toggle_install_uninstall
73+bindkey -M viins '^[OQ^[OQ' npm_toggle_install_uninstall