Let __fish_prepend_sudo use the last commandline if there is no current one (#7079)

* docs/faq: Mention prepend_sudo

[ci skip]

* __fish_prepend_sudo: Use $history[1] if commandline is empty

Currently, if you press alt+s with an empty commandline, it inserts
"sudo", which seems fairly useless.

Now, it inserts "sudo " followed by the last history entry, which
makes it a replacement for `sudo !!`.

* docs
This commit is contained in:
Fabian Homborg 2020-06-04 18:25:02 +02:00 committed by GitHub
parent 3692d63188
commit e646285bcb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 7 additions and 3 deletions

View File

@ -226,7 +226,9 @@ Change the value of the variable ``fish_greeting`` or create a ``fish_greeting``
Why doesn't history substitution ("!$" etc.) work?
--------------------------------------------------
Because history substitution is an awkward interface that was invented before interactive line editing was even possible. Instead of adding this pseudo-syntax, fish opts for nice history searching and recall features. Switching requires a small change of habits: if you want to modify an old line/word, first recall it, then edit. E.g. don't type "sudo !!" - first press Up, then Home, then type "sudo ".
Because history substitution is an awkward interface that was invented before interactive line editing was even possible. Instead of adding this pseudo-syntax, fish opts for nice history searching and recall features. Switching requires a small change of habits: if you want to modify an old line/word, first recall it, then edit.
Most of the time, it's used as ``sudo !!``. In that case just press :kbd:`Alt`\ +\ :kbd:`S`, and it will recall your last commandline with `sudo` prefixed (or toggle a `sudo` prefix on the current commandline if there is anything).
Fish's history recall is very simple yet effective:

View File

@ -1431,7 +1431,7 @@ Some bindings are shared between emacs- and vi-mode because they aren't text edi
- :kbd:`Alt`\ +\ :kbd:`V` Same as :kbd:`Alt`\ +\ :kbd:`E`.
- :kbd:`Alt`\ +\ :kbd:`S` Prepends `sudo` to the current commandline.
- :kbd:`Alt`\ +\ :kbd:`S` Prepends `sudo` to the current commandline. If the commandline is empty, prepend `sudo` to the last commandline.
- :kbd:`Control`\ +\ :kbd:`Space` Inserts a space without expanding an :ref:`abbreviation <abbreviations>`. For vi-mode this only applies to insert-mode.

View File

@ -1,7 +1,9 @@
function __fish_prepend_sudo -d "Prepend 'sudo ' to the beginning of the current commandline"
set -l cmd (commandline -po)
set -l cursor (commandline -C)
if test "$cmd[1]" != sudo
if test -z "$cmd"
commandline -r "sudo $history[1]"
else if test "$cmd[1]" != sudo
commandline -C 0
commandline -i "sudo "
commandline -C (math $cursor + 5)