Provide functions to toggle commandline prefix/suffix

This introduces two functions to
- toggle a process prefix, used for adding "sudo"
- add a job suffix, used for adding "&| less"

Not sure if they are very useful; we'll see.

Closes #7905
This commit is contained in:
Johannes Altmanninger 2021-06-23 08:55:33 +02:00
parent be0b451207
commit 7c2dd694e0
7 changed files with 41 additions and 30 deletions

View File

@ -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

View File

@ -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
--------

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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