Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Command expansions based on the history like !! #601

Closed
meltinglava opened this issue Sep 4, 2019 · 23 comments
Closed

Command expansions based on the history like !! #601

meltinglava opened this issue Sep 4, 2019 · 23 comments
Labels
enhancement New feature or request exempt label to exempt from stale bot good first issue Good for newcomers
Milestone

Comments

@meltinglava
Copy link

Feature request:
Implement bang bang: sudo !!

@sophiajt
Copy link
Contributor

sophiajt commented Sep 4, 2019

Can you talk a bit more about this feature and how it will work with nu?

@meltinglava
Copy link
Author

https://stackoverflow.com/questions/15137461/what-is-sudo-bang-bang

Bang bang is a placeholder for the last command typed. Mostly used to prepend sudo in front of the last command.

@wycats wycats added the enhancement New feature or request label Sep 10, 2019
@wycats
Copy link
Contributor

wycats commented Sep 10, 2019

What if we made sudo with no arguments have this behavior?

@meltinglava
Copy link
Author

What if we made sudo with no arguments have this behavior?

Would say that we do bought. Bang bang has been around for a long time.

Although if we add strict aliases (aka aliases that cant be added on to) so you can make an strict alias sudo be equal to sudo !! If that is a consideration we would only have to implement bang bang. Also bang bang have more than a few use cases where you would not use sudo.

Regardless i say its not an alternative, more an addition.

@wycats
Copy link
Contributor

wycats commented Sep 13, 2019

Are there common use-cases for !! other than "whoops! forgot sudo!"?

@meltinglava
Copy link
Author

Are there common use-cases for !! other than "whoops! forgot sudo!"?

Well its just a placeholder for the last command typed. We are humans and make mistakes.

I just found it strange working in a linux shell that does not have the option. bash and zsh have them by default, and fish has an extention for it.

@zcdunn
Copy link

zcdunn commented Oct 15, 2019

!! is useful for jumping back into an editor or ssh session when I've exited before finishing something. And !! is only one of the standard history commands that standard shells have. There are some other useful commands. !v runs the last command that starts with v. So i might have a session like this:

vim -O multiple source files # opens the listed files in vertical splits, side by side
# Do some editing and exit vim
mix build # or `make` or `cargo build`
!v # to reopen vim with the same files and same splits

I use this one all the time. There are a few more commands that are explained here: http:https://craig-russell.co.uk/2011/09/28/bang-bang-command-recall-in-linux.html

I reach for the parameters fetching and substitution regularly.

@corbinu
Copy link

corbinu commented Feb 19, 2020

Just started using nushell a few days ago as my primary driver after playing with it for a few months. I have run into this a couple times. I am fine if the name changes could even be like "lastline" or something but ran into it with sudo and rm when I forgot --recurse

@bltavares
Copy link

I would like to share some of the experience with bash and using nushell all time recently.

I also often rely much on !$ due to muscle memory to access the last argument of the last command, such as:

cp folder/wrong/file.so ~/repos/my-build/vendor
# Woops, wrong file
cp folder/correct/file.so !$
git clone [email protected]:nushell/nushell /opt/nushell
cd !$

I would love to have some type of shortcut similar to how zsh and bash have, but with a more memorable alias that would fit the nushell philosophy.

@fdncred
Copy link
Collaborator

fdncred commented Jun 20, 2020

Totally agree. I'd love to have all the ! commands implemented in NuShell. The ones I've used most are !!, !$, and !<history_index>. Although, this may be a rustyline thing. Not sure.

https://www.redhat.com/sysadmin/bash-bang-commands

@brightly-salty
Copy link
Contributor

I use !! in zsh when I want I am developing and want to compile over and over again without typing the command over and over again. I agree that this would be a good feature to implement.

@github-actions
Copy link

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days.

@github-actions github-actions bot added the Stale used for marking issues and prs as stale label Jun 24, 2021
@corbinu
Copy link

corbinu commented Jun 24, 2021

Just saw this went stale. How about a “papercut” label for this so it stays open but keeps in mind it is a small thing.

I will say I am a daily driver on Nushell at this point. LOVE that it has let me standardize on one shell given I work on Mac, Linux and Windows machines daily (Windows Terminal+nushell is amazing). This feature is the only thing that bites me daily at this point. Thanks for such great work!

@fdncred
Copy link
Collaborator

fdncred commented Jun 24, 2021

I agree. I was thinking there must be some way to do alias !! = do last command in history especially with this PR #3611 but i haven't looked closely enough to try something out.

@fdncred fdncred removed the Stale used for marking issues and prs as stale label Jun 24, 2021
@github-actions
Copy link

This issue is being marked stale because it has been open for 90 days without activity. If you feel that this is in error, please comment below and we will keep it marked as active.

@github-actions github-actions bot added the Stale used for marking issues and prs as stale label Sep 23, 2021
@brightly-salty
Copy link
Contributor

seems like since #3611 has been merged, it could be implemented now

(also commenting to remove stale label)

@fdncred fdncred removed the Stale used for marking issues and prs as stale label Sep 23, 2021
@danjenson
Copy link

danjenson commented Oct 16, 2021

hmmm how would this be implemented using #3611 ? I'm trying ^$"sudo (history | last)", but it's parsing it all as once string and says things like command sudo ls /etc not found

@fdncred fdncred added the exempt label to exempt from stale bot label Oct 16, 2021
@fdncred
Copy link
Collaborator

fdncred commented Oct 17, 2021

@danjenson i think it only works with externals like how the PR shows. I can't get it to work with your example either.

@corbinu
Copy link

corbinu commented Feb 11, 2022

I don't ever mean to be a nag but it has been a year and I am/have been a daily driver with nushell on Mac, Linux and Windows for longer than that. I run into this issue at least every other day. I also still recommend zsh+oh-my-zsh to others due to only this one papercut. I know it seams small and I am a sample size of one, but it is the one thing both annoying me on almost a daily basis and is the only reason I don't tell everybody know to use nushell

@fdncred
Copy link
Collaborator

fdncred commented Feb 11, 2022

ya, i agree. it's been way too long. sorry about that. @corbinu what are the specific bang commands you're looking for that you would then feel comfortable recommending nushell.

btw - this kind of works ^$"(history | last 1 | get 0)" but it apparently shells out to execute whatever the command is.

@sholderbach
Copy link
Member

There might be two ways to go about the bash last command builtins:

  1. expand them at evaluation time to fill in the history
  2. expand them in the line editor (I think this is also the default behavior in zsh)

Apart from !! for full last command, bash knows !$ for the last token (don't know the exact def) of the last command (e.g. less file.txt then vim !$) and ^search^replace to muck with the last history entry

@sholderbach sholderbach added the good first issue Good for newcomers label Feb 11, 2022
@sholderbach sholderbach changed the title bang bang Command expansions based on the history like !! Feb 11, 2022
@hustcer hustcer added this to the v0.60.0 milestone Mar 5, 2022
@hustcer hustcer added the 0.60 label Mar 5, 2022
@hustcer
Copy link
Contributor

hustcer commented Mar 5, 2022

@corbinu @meltinglava @elferherrera This issue seems can be closed by this PR: #4735 on the latest main branch?

@hustcer hustcer closed this as completed Mar 5, 2022
@johnallsup
Copy link

johnallsup commented May 4, 2023

Aside from !!, with e.g. !ls it recalls the last command beginning with ls. If histverify is set, the resulting command is not executed, so you can edit it. You can quickly collect previous commands into a custom function, for example

mkdir -p $DIR/bin $DIR/lib
ls /
myfunc() { !ls ; $mk ; }
will expand to
myfunc() { ls / ; mkdir -p $DIR/bin $DIR/lib ; }

Then e.g. echo !hello
will recall the last command that began with hello, and prepend echo, and with histverify set will allow you to edit the resulting command before execution.

Also each history item is numbered, so if the command you want to recall is 456, then !456 will recall it, and you can e.g. type

!456 ; !458 ; !454

to make a single line executing the three previous commands.

This is very useful once you're used to it. I'm currently trying to find out how to recall lines from the history in nushell.
(e.g. find the last command beginning with nvim).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request exempt label to exempt from stale bot good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests