Join GitHub today
GitHub is home to over 20 million developers working together to host and review code, manage projects, and build software together.
support history expansion (e.g., bash's !!, bang bang, and !$, bang dollar, tokens) #288
Comments
|
It is discussed in documentation FAQs : http://fishshell.com/docs/current/faq.html#faq-history |
cometpeak
commented
Aug 21, 2012
|
Thanks for the clarification. I understand the rationale for removing history characters. However, while I understand the rationale for this and that it is possible to use C-e and C-a instead of End and Home, for many users, especially those used to !! and those on a laptop that do not have Home and End keys, it is still much easier to type sudo !! than Up+C-a+sudo. Would you still consider restoring functionality of history substitution tokens? |
|
Since it's a rare case where !! is more comfortable to use, try the following function: function sudo!!
eval sudo $history[1]
endwhich is called just by "sudo!!". Or the other possibility which I use: function .runsudo --description 'Run current command line as root'
commandline -C 0
commandline -i 'sudo '
commandline -f execute
end
bind \es .runsudoThen by pressing M-S I run the current command line with sudo put just in front of it. |
pooriaazimi
commented
Apr 18, 2013
|
I made a slight variation of the last command:
It saves the cursor position instead of outright executing it. |
|
!$ would also be nice to have |
function sudo
if test "$argv" = !!
eval command sudo $history[1]
else
command sudo $argv
end
end
|
cometpeak
commented
Aug 16, 2013
|
Thanks. There is a very sizable chunk of people who use sudo !! give that this syntax is supported across many other shells including bash and zsh. sudo !! is easier to type than using the home and end keys since those keys are farther away. I would suggest having the above function be included with fish-shell by default. |
|
!! is a good candidate for an abbreviation (#731) |
SirCmpwn
commented
Oct 28, 2013
|
Expressing support for !! and related shortcuts. It doesn't hurt to add it and it feels like fish is just shoving personal philosophy at its users. |
rahulsundaram
commented
Jan 16, 2014
|
Adding myself |
AndrewHynes
commented
Jan 30, 2014
|
Despite it being old, it's a feature that really should come as default in Fish. There's no reason not to have it - if you don'r want to use it personally, that's fine, but having an extra command doesn't really hurt anyone - people like doing things different ways. |
|
The way !! is implemented in most shells does hurt people. For example, consider this command:
in bash / zsh / tcsh, this will do something unexpected and horrible, because the !! is magic syntax. This is the sort of weird interactions that fish tries very hard to avoid. My suggestion was to implement !! not as magic syntax, but as an alias. This means it would only do something special in 'command position' - it won't expand in arguments. It also keeps the feature set of fish down, which is desirable. To make that useful, we would need to teach fish about subcommands, with |
SirCmpwn
commented
Jan 31, 2014
|
Your proposed solution falls apart when you consider |
|
With aliases, only commands get expanded, and fish would know which arguments are (sub)commands and which are not. So SirCmpwn is correct that requiring comprehensive function signatures is a disadvantage of the alias approach, and also that other syntactic elements are confusing. Real-life examples include the annoyance of brace expansion with git (#434) or wildcards (#967, among others). Syntax comes with a high cost, and we try to eliminate syntax when it's redundant with other features (e.g. #354). Anyways that's my argument for why supporting !! as an elementary syntactic element is a no-go. However I'm open to introducing it in ways that don't require modifying fish syntax. |
SirCmpwn
commented
Jan 31, 2014
|
I think the comparison with |
|
I claim that However, |
codeurge
commented
May 25, 2014
|
Adding myself - just ran into this my first weekend with fish. Should definitely be supported. |
LeonardDrs
commented
Jun 5, 2014
|
Adding myself - need this. |
|
For those desiring this, the function and bindings mentioned earlier in the discussion have given me an easier workflow for prepending You should give them a try. It made me go revisit my readline and zsh configs to have similar bindings. It turns seven keystrokes (two of those shifted) into a single binding. |
AndrewHynes
commented
Jun 10, 2014
|
Regardless of what you like - is there really any reason for !! not to be supported? Everyone has different things that work for them, and though some people might enjoy using the up arrow, a lot of people enjoy using !!, and honestly, why would it be a bad thing? Those who don't want it don't have to use it. Surely it's not that hard to implement?... Yes, I know you can hack it together with fish scripting, but it does not work in the same way as in bash or similar shells, which was quite convenient a lot of the time. |
|
See my comment above for my rationale for why I don't want to introduce this the way that bash and zsh do it (as a new syntactic element), and my proposal for a way it could be done that keeps with the fish philosophy. |
gillham
commented
Mar 9, 2015
|
The argument of "echo fish is great!!" being better than using '!!' for history is a bit weak, but I understand the point. Seems like a simple "set shell_history advanced" or something could allow it for people willing to risk "echo fish is great!!" getting an error message. Nevertheless, my use case of '!!' is similar but could be avoided with some feature enhancement perhaps. For example on my mac with bash: So I somewhat regularly use the output of the previous command for a new command. Unfortunately even though fish is fairly proud of using () for sub-commands instead of "confusing" backticks and such, it doesn't really treat them as such until you hit enter. At least that is what I see. If fish would allow a history search after the first '(' (aka a subcommand) I think it would be easier to handle not having ! history manipulation. |
|
If I understand the suggestion, it's that when you press up arrow, it should perform history replacement only within the innermost subcommand instead of the entire command line. |
SirCmpwn
commented
Mar 9, 2015
|
I still support simply implementing bash-style |
|
I'd prefer :up_arrow:, Ctrl+p and command history search working in subcommand positions over introducing !! The entire history API of other shells is confusing enough that people usually only remember !! from it. |
gillham
commented
Mar 9, 2015
|
Correct. That would be convenient and fairly easily replace the '!!' Or !keyword use. |
ElijahLynn
commented
Mar 9, 2015
|
Really like the idea of |
mdsib
commented
Mar 13, 2015
|
It looks like abbreviations in the most recent nightly builds are only allowing abbreviations in the command position, hopefully it'll be updated to be able to be used as arguments for cases like this one. |
ghost
commented
Mar 14, 2015
|
This feature is overrated, just use |
ElijahLynn
commented
Mar 16, 2015
|
@bucaran I am not following exactly. The Do you mean make a variable called Thanks |
ElijahLynn
commented
Mar 16, 2015
|
@mdsib Yeah, I think I remember that there was a discussion in another issue about not allowing abbreviations in the argument position. At the time it was preferred to not implement that. This would definitely change that. |
SirCmpwn
commented
Mar 16, 2015
|
I think the takeaway from this thread is that it's not happening any time soon, and I should unsubscribe from this Github issue and switch to zsh. Adios! |
SirCmpwn
commented
Mar 17, 2015
|
Not because |
gillham
commented
Mar 17, 2015
|
It is funny to see people trying to convince others that a feature isn't a big deal. Sure, you don't want it, so you don't care.. It is like you're trying convince people not to like green because you think blue is better. I made ~/bin/!! that looks like this:
For the short term it helps me workaround the missing feature and my tendency to type !!.
Not ideal, but enough that my shell tends to do what I expect more often now. |
|
@SirCmpwn Is it not obvious to me that
Should this use of !! be supported or not? Users who say "fish should support !!" may have different ideas of what that means. You feel strongly about this, so I encourage you (or anyone) to author a proposal for how !! would work in fish (the semantics, not implementation). What syntax would be supported exactly? How would this expansion relate to other types of expansion, like variable or process expansion? How would argument splitting occur in the expanded string? Could you use escapes to prevent it? This is a sincere request. A feature is not defined by a single use case; we need a concrete proposal to discuss. If you say "I want this case to work, and I don't care how," then that's fine and valid. But someone has to work out the details before anything can be implemented. |
zanchey
added
the
enhancement
label
Mar 17, 2015
|
In the near term, one concrete plan is to implement global aliases and then make !! into one. An alternative is to merge the docopt work, give a function signature for sudo, and then make !! into a regular alias. Which direction we go hinges on whether we think !! should expand everywhere, or just in command position. |
gillham
commented
Mar 17, 2015
|
@ridiculousfish You're absolutely right that a '!!' feature request needs to be fairly detailed. I've never used the particular example you gave as I always do something like this: Obviously this doesn't work either. That would be separate feature request for me, that I haven't bothered with since I only started testing fish out recently. "fish: Encountered redirection when expecting a command name. Fish does not allow a redirection operation before a command." Seems like it would be easy to treat '^' in the command position as a substitution since it isn't conflicting with existing functionality. But that is a separate issue, just mentioning it since you're right that there should be a clear definition of what '!!' (or just history substitution in general) means. Here are a couple examples I use:
|
|
Thank you @gillham , information like that is very helpful when designing a feature. |
|
I also note that @nyarly has a clever workaround for |
|
Better fix #5: As for @nyarly's
|
|
Note that erasing from history doesn't require bash shenanigans:
|
AerysBat
commented
Jun 9, 2015
|
This functionality can be replicated quite well within Fish using https://github.com/fish-shell/fish-shell/wiki/Bash-Style-History-Substitution-(!!-and-!$) -- perhaps the issue should be closed? |
|
|
|
I think this is a slippery slope. If this is implemented in fish, what about single !s, and what about ksh history features? What about $()? Fish is a csh-style shell. If people can't/won't adapt, there's zsh... |
tetra-archos
commented
Oct 7, 2015
|
What I have gleamed from this discussion as a tl;dr for people who find this. (over 3 years in)
I am one of the latter, while I hope this get's resolved I cannot reasonably use fish and add that workaround with the number of machines I use without a considerable amount of effort (I need different config setups in different machines), and I use !! enough where it is a requirement for me. So I will be following this discussion in the hopes that one day fish will meet my use case. |
|
@tetra-archos Nobody has put forth a concrete proposal yet, so we don't have a firm grasp of what "the feature" is. Since you obviously feel strongly about this, if you were so inclined, it would be a big help if you were to suggest what the behavior ought to be, as a starting point. Failing that, it would be useful to at least list the history substitution forms you use. Note that the full bash history substitution behavior is very complex, and also configurable, so "just do what bash does" is a non-starter. |
@tetra-archos: This is absolutely critical. So far, most of this has been 'We want "!!"!!' and maybe "!$" (which is "insert last argument of the last command"). But there's a lot of details that need to be banged out if it (or a substitute) is to be implemented. For example:
If it's just muscle-memory, you can also put the following in your fish_user_key_bindings: bind '!!' 'commandline -i "$history[1]"'
bind '!$' 'history-token-search-backward'(the issue this has, why we probably shouldn't take it in the default bindings, is that it then waits after you've entered "!" to check for a second character) |
andreaskern
commented
Nov 10, 2015
|
I think that The command
I suggest adding a message to tell people to use the line editing commands ^p and ^a instead of using the history function.
|
|
I hate
|
tetra-archos
commented
Nov 10, 2015
|
Because you don't like a way of doing something doesn't make it less valid.
|
|
The reverse is true, too. Both sides of this issue have legitimate needs and concerns. I think the main holdup is finding the right way to do it instead of the quick and dirty way |
tetra-archos
commented
Nov 10, 2015
|
I agree and I have nothing against the other ways to do it, they just don't
|
|
To reiterate the approaches mentioned in the early discussion and to expand on them a little: I have found keybindings to work better for me than "!!". I've written this function which can be bound to some key (^S for me, function prepend_command
set -l prepend $argv[1]
if test -z "$prepend"
echo "prepend_command needs one argument."
return 1
end
set -l cmd (commandline)
if test -z "$cmd"
commandline -r $history[1]
end
set -l old_cursor (commandline -C)
commandline -C 0
commandline -i "$prepend "
commandline -C (math $old_cursor + (echo $prepend | wc -c))
endSome people said configuring something like this is too complicated to do. A possible solution might be to include something functionally similar to my function in fish so configuring this becomes a one-liner at least. Of course, this doesn't answer other demands like |
ElijahLynn
commented
Feb 23, 2016
|
@ahti That is actually badass, just got it working, looks like it will be a treat to use, will report back in a while! |
ElijahLynn
commented
Feb 24, 2016
|
Yeah, so far this beats Personally I never used the |
ElijahLynn
commented
Feb 24, 2016
|
Coincidentally I just tried out fish_vi_mode and really like it. However, in Vi mode |
ElijahLynn
commented
Feb 24, 2016
|
Okay, I had the key binding in config.fish. I found #2254 and then found faho/fish-shell@f7e8ad6 and then found http://stackoverflow.com/a/16675092/292408 which confirmed I should place my bindings in a fish_user_key_bindings function which didn't yet exist. That worked and now when I am in Vi command mode |
|
We already have Alt-P to run the current pipeline in the pager; I wonder whether we should add Alt-S as well? |
hubbcaps
commented
Mar 15, 2016
|
I think @ahti's is a pretty elegant solution to what everyone has been looking for. Direct to a keybind with no worry about a random expansion or weird edge cases. It simply places text for you to use or cancel out as you wish. |
orzechow
commented
May 11, 2016
|
Oy! This thread is almost four years old now. I recently bought ǃǃ.com and ǃǃ.net (which Github won't let me hyperlink, apparently, so copy-and-paste) if anyone wants them. |
Timopheym
commented
May 11, 2016
|
Hey! That's cool! @geoff-codes How did you do that?) |
ghost
commented
May 11, 2016
•
|
I didn't know you could register !!.com, that's crazy. The lesson learned from this issue: if you ever write a Unix shell, the only feature you should make sure to implement is sudo !!, because it seems people can't live without it. Seriously though, I've got word from people saying that this is working great: |
|
@bucaran Yes, indeed, that seems to be the case |
|
Internationalized Domain Names - IDNs: https://en.wikipedia.org/wiki/Internationalized_domain_name edit: too slow with this post, and also registrations like those @geoff-codes! That's got to be a wise investment. A two character (sorta) .com!
|
andreaskern
commented
May 11, 2016
|
In the bash for Windows presentation the guy presenting typed something $> echo hello Windows!! And I hope that that you can all guess what happened.
|
krader1961
changed the title from
sudo: !!: command not found
to
sudo: !!: command not found (request support for bang bang token)
May 19, 2016
krader1961
referenced this issue
May 21, 2016
Closed
Add keywords to reuse the arguments to the previous command #3051
krader1961
changed the title from
sudo: !!: command not found (request support for bang bang token)
to
support history expansion (e.g., bash's !!, bang bang, and !$, bang dollar, tokens)
May 22, 2016
added a commit
to simotek/tackle
that referenced
this issue
May 24, 2016
added a commit
to simotek/tackle
that referenced
this issue
May 24, 2016
added a commit
to simotek/tackle
that referenced
this issue
May 24, 2016
added a commit
to simotek/tackle
that referenced
this issue
May 24, 2016
base698
commented
Jun 9, 2016
|
So, I should switch to bash? |
|
@base698 - I wound up putting out a fisherman plugin to support the two most common use cases of history substitution: !! for the whole last command and !$ for the last argument. Honestly, I've mostly broken my own habits because fish's command line editing are so good. Still, I really like being able to
to examine a script for instance You can install the plugin with |
andreaskern
commented
Jun 10, 2016
|
for the sake of completeness (and giving a correct example) https://www.youtube.com/watch?v=2dB0igTfhfg&feature=youtu.be&t=760 He wants to execute results in |
pjrt
commented
Oct 28, 2016
|
As much as I love fish, !! is just too useful. Not sure
zsh expands !! so that you can edit it. So gonna have to go back to zsh. |
|
If you're using fisherman (which you very much should be) you can install |
pjrt
commented
Oct 28, 2016
•
|
@nyarly Yeah, I tried that. But I don't like how it expands immediately after the second EDIT: Nevermind, I couldn't do it. I had forgotten how annoying setting ZSH to be what I want it to be was. nyarly's plugin will have to do. |
Bahaa-Addin
commented
Mar 5, 2017
|
[Solved] Here: |
andreaskern
referenced this issue
Mar 5, 2017
Closed
Update index.hdr.in list keybindings for history #3879
pgan002
commented
May 27, 2017
•
|
I'm so glad this was not implemented! It violates Fish's Law of orthogonality and Law of user focus. Bash's history replacement requires too much thinking beyond Some users miss |
davidawad
commented
Jun 5, 2017
•
|
Here's a fix for anyone who wants something to copy and paste: put this into ~/.config/fish/functions/fish_user_key_bindings.fish function bind_bang
switch (commandline -t)
case "!"
commandline -t $history[1]; commandline -f repaint
case "*"
commandline -i !
end
end
function bind_dollar
switch (commandline -t)
case "!"
commandline -t ""
commandline -f history-token-search-backward
case "*"
commandline -i '$'
end
end
function fish_user_key_bindings
bind ! bind_bang
bind '$' bind_dollar
end |
|
@siteshwar URL linked to in #288 (comment) is broken. Could you edit your comment to fix it for people reading this still-open issue? |
|
I edited his comment to point to where we host the FAQ now. |


cometpeak commentedAug 20, 2012
It appears that the double-bang does not work and is somehow interpreted as a program which sudo unsuccessfully tries to locate. This occurs on Ubuntu 12.04 LTS with fish-shell compiled from source.
Steps to repro:
Execute any command:
$ ls
Execute the previous command as root:
$ sudo !!
sudo: !!: command not found
Expected behavior:
Command
sudo !!executes previously executed command (ls) as root.I also checked that the issue isn't caused by
sudoby following repro steps in bash, which behaved as expected.Also, note that an earlier version of fish-shell on Mac OS X 10.7 did not have this problem and
sudo !!worked as expected.