From 47852f487feb2561efbbfbbe199e6df23f07437f Mon Sep 17 00:00:00 2001 From: Victor Munoz Date: Tue, 21 Mar 2023 23:22:37 +0100 Subject: [PATCH 1/2] plugins: Add plugin "sudo" --- plugins/sudo/README.md | 50 +++++++++++++++++++++++++++++++++++++ plugins/sudo/sudo.plugin.sh | 11 ++++++++ 2 files changed, 61 insertions(+) create mode 100644 plugins/sudo/README.md create mode 100644 plugins/sudo/sudo.plugin.sh diff --git a/plugins/sudo/README.md b/plugins/sudo/README.md new file mode 100644 index 0000000..1a9f206 --- /dev/null +++ b/plugins/sudo/README.md @@ -0,0 +1,50 @@ +# sudo + +Easily prefix your current or previous commands with `sudo` by pressing esc twice. + +To use it, add `sudo` to the plugins array in your bashrc file: + +```bash +plugins=(... sudo) +``` + +## Usage + +### Current typed commands + +Say you have typed a long command and forgot to add `sudo` in front: + +```console +$ apt-get install build-essential +``` + +By pressing the esc key twice, you will have the same command with `sudo` prefixed without typing: + +```console +$ sudo apt-get install build-essential +``` + +### Previous executed commands + +Say you want to delete a system file and denied: + +```console +$ rm some-system-file.txt +-su: some-system-file.txt: Permission denied +$ +``` + +By pressing the esc key twice, you will have the same command with `sudo` prefixed without typing: + +```console +$ rm some-system-file.txt +-su: some-system-file.txt: Permission denied +$ sudo rm some-system-file.txt +Password: +$ +``` + +## Credits + +Inspired by ohmyzsh sudo plugin. + diff --git a/plugins/sudo/sudo.plugin.sh b/plugins/sudo/sudo.plugin.sh new file mode 100644 index 0000000..64ac4e2 --- /dev/null +++ b/plugins/sudo/sudo.plugin.sh @@ -0,0 +1,11 @@ +#! bash oh-my-bash.module + +function _omb_plugin_sudo_add_sudo { + if [[ -z $READLINE_LINE ]]; then + READLINE_LINE="$(fc -ln -1 | command sed 's/^[[:space:]]\{1,\}//')" + fi + READLINE_LINE="sudo $READLINE_LINE" + ((READLINE_POINT += 5)) +} + +bind -x '"\e\e": _omb_plugin_sudo_add_sudo' From 4ad3482985f0574640b064e8b32261e8e2d22e46 Mon Sep 17 00:00:00 2001 From: Koichi Murase Date: Sun, 9 Apr 2023 00:05:00 +0900 Subject: [PATCH 2/2] plugins/sudo: Add keybindings following OMZ --- plugins/sudo/sudo.plugin.sh | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/plugins/sudo/sudo.plugin.sh b/plugins/sudo/sudo.plugin.sh index 64ac4e2..43835e3 100644 --- a/plugins/sudo/sudo.plugin.sh +++ b/plugins/sudo/sudo.plugin.sh @@ -8,4 +8,6 @@ function _omb_plugin_sudo_add_sudo { ((READLINE_POINT += 5)) } -bind -x '"\e\e": _omb_plugin_sudo_add_sudo' +bind -m emacs -x '"\e\e": _omb_plugin_sudo_add_sudo' +bind -m vi-insert -x '"\e\e": _omb_plugin_sudo_add_sudo' +bind -m vi-command -x '"\e\e": _omb_plugin_sudo_add_sudo'