commit 33b4d4490efd57cac7f55394d998bbd043e3e509
parent 58df0746831bf85d5018922b3ba87427c8076f0a
Author: Vincent Demeester <vincent@sbr.pm>
Date: Thu, 16 Aug 2018 11:42:55 +0200
Import sudope behavior 👼
See https://github.com/oh-my-fish/plugin-sudope
Signed-off-by: Vincent Demeester <vincent@sbr.pm>
Diffstat:
3 files changed, 58 insertions(+), 0 deletions(-)
diff --git a/fish.nix b/fish.nix
@@ -10,6 +10,8 @@
shellAliases = import ./aliases.nix;
};
xdg.configFile."fish/conf.d/nix-aliases.fish".source = ./fish/nix-aliases.fish;
+ xdg.configFile."fish/conf.d/sudope.fish".source = ./fish/sudope.fish;
+ xdg.configFile."fish/functions/sudope.fish".source = ./fish/sudope.function.fish;
xdg.configFile."fish/functions/fish_prompt.fish".source = ./fish/fish_prompt.fish;
xdg.configFile."fish/functions/fish_right_prompt.fish".source = ./fish/fish_right_prompt.fish;
}
diff --git a/fish/sudope.fish b/fish/sudope.fish
@@ -0,0 +1,2 @@
+# default key sequence: Ctrl+s
+bind \cs sudope
diff --git a/fish/sudope.function.fish b/fish/sudope.function.fish
@@ -0,0 +1,54 @@
+function sudope -d "Quickly toggle sudo prefix"
+
+ # Save the current command line and cursor position.
+ set -l command_buffer (commandline)
+ set -l cursor_position (commandline -C)
+
+ # If the command line is empty, pull the last command from history.
+ if test -z "$command_buffer"
+ set command_buffer $history[1]
+ end
+
+ # Parse the command line (first line only).
+ set -l command_parts (string match -ir '^(\s*)(sudo(\s+|$))?(.*)' $command_buffer[1])
+
+ # Handle multiline commands with extra care.
+ set -l command_lines_count (count $command_buffer)
+ test $command_lines_count -gt 1
+ and set -l command_rest $command_buffer[2..$command_lines_count]
+
+ switch (count $command_parts)
+ case 3
+ # No "sudo".
+
+ # Add "sudo" to the beginning of the command, after any leading whitespace (if present).
+ commandline -r (string join \n (string join '' $command_parts[2] 'sudo ' $command_parts[3]) $command_rest)
+
+ # Push the cursor position ahead if necessary (+5 for 'sudo ').
+ test $cursor_position -ge (string length -- "$command_parts[2]")
+ and set cursor_position (math $cursor_position+5)
+
+ # Place the cursor where it was (or where it should be).
+ commandline -C $cursor_position
+
+ case 5
+ # "sudo" is present in the beginning of the command.
+
+ # Remove "sudo", leave any leading whitespace (if present).
+ commandline -r (string join \n (string join '' $command_parts[2 5]) $command_rest)
+
+ # Push the cursor position back if appropriate ('sudo' and whitespace).
+ set -l lead_length (string length -- "$command_parts[2]")
+ set -l sudo_length (string length -- "$command_parts[3]")
+ if test $cursor_position -ge (math $lead_length+$sudo_length)
+ # The cursor was after "sudo".
+ set cursor_position (math $cursor_position-$sudo_length)
+ else if test $cursor_position -ge $lead_length
+ # The cursor was somewhere on "sudo".
+ set cursor_position $lead_length
+ end
+
+ # Place the cursor where it was (or where it should be).
+ commandline -C -- $cursor_position
+ end
+end