diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 38690a2a4..6f0183902 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -65,7 +65,8 @@ New or improved bindings - The :kbd:`Control-Space` binding can be correctly customised (:issue:`7922`). - ``exit`` works correctly in bindings (:issue:`7967`). - The :kbd:`F1` binding, which opens the manual page for the current command, now works around a bug in certain ``less`` versions that fail to clear the screen (:issue:`7863`). -- ``__fish_prepend_sudo`` now toggles sudo even when it took the commandline from history instead of only adding it. +- The binding for :kbd:`Alt-S` now toggles sudo even when it took the commandline from history instead of only adding it. +- The new interactive functions ``fish_commandline_prepend`` and ``fish_commandline_prepend`` allow to toggle a prefix/suffix on the commandline. (:issue:`7905`). - ``backward-kill-path-component`` :kbd:`Control-W`) no longer erases parts of two tokens when the cursor is positioned immediately after ``/``. (:issue:`6258`). Improved prompts diff --git a/doc_src/cmds/bind.rst b/doc_src/cmds/bind.rst index 32d2793cc..f790ed3c5 100644 --- a/doc_src/cmds/bind.rst +++ b/doc_src/cmds/bind.rst @@ -217,6 +217,10 @@ The following functions are included as normal functions, but are particularly u - ``fish_clipboard_paste``, paste the current selection from the system clipboard before the cursor +- ``fish_commandline_append``, append the argument to the command-line. If the command-line already ends with the argument, this removes the suffix instead. Starts with the last command from history if the command-line is empty. + +- ``fish_commandline_prepend``, prepend the argument to the command-line. If the command-line already starts with the argument, this removes the prefix instead. Starts with the last command from history if the command-line is empty. + Examples -------- diff --git a/share/functions/__fish_paginate.fish b/share/functions/__fish_paginate.fish index 55cd35391..8fe789a61 100644 --- a/share/functions/__fish_paginate.fish +++ b/share/functions/__fish_paginate.fish @@ -1,16 +1,8 @@ function __fish_paginate -d "Paginate the current command using the users default pager" - set -l cmd less if set -q PAGER echo $PAGER | read -at cmd end - if test -z (commandline -j | string join '') - commandline -i $history[1] - end - - if commandline -j | not string match -q -r "$cmd *\$" - commandline -aj " &| $cmd" - end - + fish_commandline_append " &| $cmd" end diff --git a/share/functions/__fish_prepend_sudo.fish b/share/functions/__fish_prepend_sudo.fish index f514766df..1c72e8f08 100644 --- a/share/functions/__fish_prepend_sudo.fish +++ b/share/functions/__fish_prepend_sudo.fish @@ -1,19 +1,3 @@ -function __fish_prepend_sudo -d "Prepend 'sudo ' to the beginning of the current commandline" - # If there is no commandline, insert the last item from history - # and *then* toggle - if not commandline | string length -q - commandline -r "$history[1]" - end - - set -l cmd (commandline -po) - set -l cursor (commandline -C) - - if test "$cmd[1]" != sudo - commandline -C 0 - commandline -i "sudo " - commandline -C (math $cursor + 5) - else - commandline -r (string sub --start=6 (commandline -p)) - commandline -C -- (math $cursor - 5) - end +function __fish_prepend_sudo -d " DEPRECATED: use fish_commandline_prepend instead. Prepend 'sudo ' to the beginning of the current commandline" + fish_commandline_prepend sudo end diff --git a/share/functions/__fish_shared_key_bindings.fish b/share/functions/__fish_shared_key_bindings.fish index 93ae2e8bf..ebab94dae 100644 --- a/share/functions/__fish_shared_key_bindings.fish +++ b/share/functions/__fish_shared_key_bindings.fish @@ -98,8 +98,7 @@ function __fish_shared_key_bindings -d "Bindings shared between emacs and vi mod bind --preset $argv \ed 'set -l cmd (commandline); if test -z "$cmd"; echo; dirh; commandline -f repaint; else; commandline -f kill-word; end' bind --preset $argv \cd delete-or-exit - # Prepend 'sudo ' to the current commandline - bind --preset $argv \es __fish_prepend_sudo + bind --preset $argv \es "fish_commandline_prepend sudo" # Allow reading manpages by pressing F1 (many GUI applications) or Alt+h (like in zsh). bind --preset $argv -k f1 __fish_man_page diff --git a/share/functions/fish_commandline_append.fish b/share/functions/fish_commandline_append.fish new file mode 100644 index 000000000..88aea1195 --- /dev/null +++ b/share/functions/fish_commandline_append.fish @@ -0,0 +1,15 @@ +function fish_commandline_append --description "Append the given string to the command-line, or remove the suffix if already there" + if not commandline | string length -q + commandline -r $history[1] + end + + set -l escaped (string escape --style=regex -- $argv[1]) + set -l job (commandline -j | string collect) + if set job (string replace -r -- $escaped\$ "" $job) + set -l cursor (commandline -C) + commandline -rj -- $job + commandline -C $cursor + else + commandline -aj -- $argv[1] + end +end diff --git a/share/functions/fish_commandline_prepend.fish b/share/functions/fish_commandline_prepend.fish new file mode 100644 index 000000000..478be01cc --- /dev/null +++ b/share/functions/fish_commandline_prepend.fish @@ -0,0 +1,16 @@ +function fish_commandline_prepend --description "Prepend the given string to the command-line, or remove the prefix if already there" + if not commandline | string length -q + commandline -r $history[1] + end + + set -l escaped (string escape --style=regex -- $argv[1]) + set -l process (commandline -p | string collect) + if set process (string replace -r -- "^$escaped " "" $process) + commandline --replace -p -- $process + else + set -l cursor (commandline -C) + commandline -C 0 -p + commandline -i -- "$argv[1] " + commandline -C (math $cursor + (string length -- "$argv[1] ")) + end +end