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

feature-request: Add fc "fix command" command, like the shell builtin #5030

Closed
ElijahLynn opened this issue Jun 7, 2018 · 11 comments
Closed
Labels

Comments

@ElijahLynn
Copy link

ElijahLynn commented Jun 7, 2018

I just learned about the bash builtin fc command. It takes the last command and opens it in your editor, so you can tweak it, then when you save it, it runs that new command and adds it to history.

fc: usage: fc [-e ename] [-lnr] [first] [last] or fc -s [pat=rep] [command]

Would love to see fish ship this command in core.

@krader1961
Copy link
Contributor

Fish already has that feature. Simply [up-arrow] or [ctrl-p] to retrieve the command then press [alt-e] or [alt-v].

@icemac
Copy link

icemac commented Jun 7, 2018

It is documented here: https://fishshell.com/docs/current/index.html#shared-binds

But it does not work out of the box in my iTerm2. [alt-e] prints and [alt-v] prints
Anything I missed?

@krader1961
Copy link
Contributor

@icemac You need to configure iTerm2 to treat [alt] as a meta key that prefixes the sequence with the escape char. Open Preferences -> Profiles -> Keys. Bottom right-hand corner change the left and/or right alt key behavior to Esc+. Too, if you type fast enough you can just press [esc] followed by e or v.

@krader1961
Copy link
Contributor

And, of course, you can bind the function to any key of your choosing; e.g., bind \cf edit_command_buffer.

@faho faho added the question label Jun 7, 2018
@icemac
Copy link

icemac commented Jun 8, 2018

@krader1961 Thank you for your tipps. It was the right direction. I eventually found out about fish_user_key_bindings and how to persist the bind.

@ElijahLynn
Copy link
Author

@krader1961 Thanks, I just tried alt+v as well as edit_command_buffer and it opens up in vim, but the command is missing. Fish 2.7.1.

@krader1961
Copy link
Contributor

Works for me and everyone else AFAIK. Note that [alt-v] edits the current command. So if you want to edit a previous command you entered you first have to retrieve it such as by pressing [up-arrow].

@ElijahLynn
Copy link
Author

ElijahLynn commented Jun 9, 2018

Ah, that was it. I wasn't hitting up-arrow. I suppose this solution works for now. I do think a built-in fc function to pop the last command off the stack and run edit_command_buffer would be good, as people coming from bash will inevitably try fc first and may not realize that they can do a up-arrow + alt+e or alt+v.

OR, maybe ship a built-in fc function that when run, does nothing and just tells the user to do the above.

@krader1961
Copy link
Contributor

What about people coming from csh, zsh, xonsh, elvish or any other random shell that has a different mechanism for doing the same thing? I recognize that bash is widely used but the whole point of fish is that it is not bash or any other POSIX shell. As for

they may not realize that they can do a up-arrow + alt+e or alt+v.

That is a valid point. Improvements to the documentation are always welcome and arguably more valuable than changes to the implementation.

@horacimacias
Copy link

I often use fc to re-run several commands again, e.g.

fc -3 -1

is there any way to achieve that using fish?
e.g. "re-run the last 3 commands, in the same order they've been ran"

@faho faho closed this as completed Dec 31, 2019
jspricke added a commit to jspricke/fish-shell that referenced this issue Jun 15, 2020
This is useful to combine and rerun several commands.
For example having the last 4 commands in $history:

$ string join \n $history[1..4]
./test
git clean -dfx
ls
make

$ fc -4 -1

opens an editor where you can drop the ls, maybe move the git command to
the start, join all lines with '; and' and save. Now the prompt contains
one command, ready to be executed.

Note that the numbering is inverted to $history to comply with POSIX.

The implementation is based on share/functions/edit_command_buffer.fish.

fc is specified by POSIX in

https://pubs.opengroup.org/onlinepubs/9699919799/utilities/fc.html

For now only the first and last options are implemented.

Fixes fish-shell#5030
jspricke added a commit to jspricke/fish-shell that referenced this issue Jun 15, 2020
This is useful to combine and rerun several commands.
For example having the last 4 commands in $history:

$ string join \n $history[1..4]
./test
git clean -dfx
ls
make

$ fc -4 -1

opens an editor where you can drop the ls, maybe move the git command to
the start, join all lines with '; and' and save. Now the prompt contains
one command, ready to be executed.

Note that the numbering is inverted to $history to comply with POSIX.

The implementation is based on share/functions/edit_command_buffer.fish.

fc is specified by POSIX in

https://pubs.opengroup.org/onlinepubs/9699919799/utilities/fc.html

For now only the first and last options are implemented.

Fixes fish-shell#5030
jspricke added a commit to jspricke/fish-shell that referenced this issue Jun 15, 2020
This is useful to combine and rerun several commands.
For example having the last 4 commands in $history:

$ string join \n $history[1..4]
./test
git clean -dfx
ls
make

$ fc -4 -1

opens an editor where you can drop the ls, maybe move the git command to
the start, join all lines with '; and' and save. Now the prompt contains
one command, ready to be executed.

Note that the numbering is inverted to $history to comply with POSIX.

The implementation is based on share/functions/edit_command_buffer.fish.

fc is specified by POSIX in

https://pubs.opengroup.org/onlinepubs/9699919799/utilities/fc.html

For now only the first and last options are implemented.

Fixes fish-shell#5030
@lengyijun
Copy link
Contributor

lengyijun commented Jan 23, 2023

https://github.com/lengyijun/fc-fish

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants
@icemac @horacimacias @ElijahLynn @faho @krader1961 @lengyijun and others