├── LICENSE
├── README.md
└── watch.plugin.zsh
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2020 enrico9034
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # WATCH
2 |
3 | Easily prefix your current or previous commands with `watch` by pressing Alt + w
4 |
5 | 1. Clone this repository into `$ZSH_CUSTOM/plugins` (by default `~/.oh-my-zsh/custom/plugins`)
6 |
7 | ```bash
8 | git clone https://github.com/enrico9034/watch-plugin-zsh.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/watch
9 | ```
10 |
11 | 2. To use it, add `watch` to the plugins array in your zshrc file:
12 |
13 | ```zsh
14 | plugins=(... watch)
15 | ```
16 |
17 | 3. Start a new terminal session.
18 |
19 | ## Usage
20 |
21 | ### Current typed commands
22 |
23 | Say you have typed a long command and forgot to add `watch` in front:
24 |
25 | ```console
26 | $ kubectl get pod -n namespace
27 | ```
28 |
29 | By pressing the Alt + w key, you will have the same command with `watch` prefixed without typing:
30 |
31 | ```console
32 | $ watch kubectl get pod -n namespace
33 | ```
34 |
35 | ### Previous executed commands
36 |
37 | Say you want to delete a system file and denied:
38 |
39 | ```console
40 | $ kubectl get pod -n namespace
41 | $
42 | ```
43 |
44 | By pressing the Alt + w key, you will have the same command with `watch` prefixed without typing:
45 |
46 | ```console
47 | $ rm some-system-file.txt
48 | $
49 | $ kubectl get pod -n namespace
50 | $
51 | ```
52 |
53 | ### This plugin is based on official sudo plugin
54 |
55 | sudo official plugin: https://github.com/ohmyzsh/ohmyzsh/tree/master/plugins/sudo
56 |
--------------------------------------------------------------------------------
/watch.plugin.zsh:
--------------------------------------------------------------------------------
1 | # ------------------------------------------------------------------------------
2 | # Description
3 | # -----------
4 | #
5 | # plugin based on official sudo plugin
6 | # watch will be inserted before the command
7 | #
8 | # ------------------------------------------------------------------------------
9 | # Authors
10 | # -------
11 | #
12 | # * Enrico Falco
13 | #
14 | # ------------------------------------------------------------------------------
15 |
16 | watch-command-line() {
17 | [[ -z $BUFFER ]] && LBUFFER="$(fc -ln -1)"
18 |
19 | # Save beginning space
20 | local WHITESPACE=""
21 | if [[ ${LBUFFER:0:1} == " " ]] ; then
22 | WHITESPACE=" "
23 | LBUFFER="${LBUFFER:1}"
24 | fi
25 |
26 | # Expand aliases
27 | unset 'functions[_watch-plugin-expand]' # Clear function set by previous call
28 | functions[_watch-plugin-expand]=${LBUFFER} # Save LBUFFER to functions file, this will expand the aliases
29 | (($+functions[_watch-plugin-expand])) && LBUFFER=${functions[_watch-plugin-expand]#$'\t'} # Get the (expanded) function content back and clear the first tab(s)
30 |
31 | if [[ $BUFFER == watch\ * ]]; then
32 | if [[ ${#LBUFFER} -le 5 ]]; then
33 | RBUFFER="${BUFFER#watch }"
34 | LBUFFER=""
35 | else
36 | LBUFFER="${LBUFFER#watch }"
37 | fi
38 | else
39 | LBUFFER="watch $LBUFFER"
40 | fi
41 |
42 | # Preserve beginning space
43 | LBUFFER="${WHITESPACE}${LBUFFER}"
44 | }
45 | zle -N watch-command-line
46 | # Defined shortcut keys: Alt+w
47 | bindkey -M emacs '^[w' watch-command-line
48 | bindkey -M vicmd '^[w' watch-command-line
49 | bindkey -M viins '^[w' watch-command-line
50 |
--------------------------------------------------------------------------------