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

Add support for undo #1367

Closed
creack opened this issue Mar 26, 2014 · 18 comments · Fixed by #6570
Closed

Add support for undo #1367

creack opened this issue Mar 26, 2014 · 18 comments · Fixed by #6570

Comments

@creack
Copy link

creack commented Mar 26, 2014

It would be nice to have an undo feature.

I don't know about bash, but zsh and emacs use <ctrl + _> for this and it is very very useful.

@ridiculousfish ridiculousfish added this to the fish-future milestone Mar 26, 2014
@zanchey zanchey added the vi-mode label Apr 9, 2015
@Granjow
Copy link

Granjow commented Nov 4, 2015

FYI: Bash supports undo with Ctrl+_

@Granjow
Copy link

Granjow commented Nov 4, 2015

Just for clarification, it is not undoing a command (like, undo rm -f *), but undoing an edit command like delete word (Alt+d) or kill to EOL (Ctrl+k)?

@kenenbek
Copy link

Sorry, is still this feature unimplemented?

@faho
Copy link
Member

faho commented Feb 13, 2017

Yes.

@kek
Copy link

kek commented Apr 13, 2017

I didn't know about the use case for undo in a command line, because if I would delete something by mistake with Ctrl-K, Ctrl-W or Ctrl-U, I would just use Ctrl-Y to paste it back. It seems to work in fish too. Could that be considered a workaround?

@adambyrtek
Copy link
Contributor

I don't think it's just about line editing commands. I often use undo (in zsh) for cancelling the last Tab expansion that for some reason went too far (for example due to a typo).

@Ambrevar
Copy link
Contributor

Ambrevar commented Jun 5, 2017

From http://fishshell.com/docs/current/index.html#editor:

u undoes the most recent action.

I guess that was a little too anticipated then... :p

@krader1961
Copy link
Contributor

I guess that was a little too anticipated then.

Yes, that dates back to when vi mode was implemented with commit f68f91b on 2014-11-08. Obviously it was a bit of wishful thinking. I've removed that line from the docs. At the moment it is bound to history-search-backward.

@creack creack closed this as completed Apr 28, 2018
@faho faho reopened this Apr 28, 2018
@nertpinx
Copy link

Any updates on this? I am in the process of switching to fish, everything is awesome, but the undo...
Just so others know when this is useful, it might be that the intelligent fuzzy completion completes something long on <TAB>, but you did not want that (be it a typo or something). On weird terminals or other non-standard situations it might happen that a key combination that you did not want to press does actually paste the escape sequence. Or you paste something and realize that is not what you wanted to paste. Instead of clearing all that you could just hit undo. Both bash and zsh work with Ctrl+/ although zsh on my system behaves a bit better (undoing some typing goes back by words and not the whole action, which is what bash does).

@ridiculousfish
Copy link
Member

Nobody is working on it but I think it's a good idea 👍

@floam
Copy link
Member

floam commented Apr 17, 2019

After the commandline has been significantly mutated, we could put the previous command line in as an autosuggestion until the user types anything further. This could facilitate undoing without an undo command.

@faho
Copy link
Member

faho commented Apr 17, 2019

After the commandline has been significantly mutated, we could put the previous command line in as an autosuggestion

  1. If I change the commandline, it's usually on purpose and correct, so a different autosuggestion would help me go forward, not back. Suggestions are prime real estate as there can be only one, using them for undo seems like a bit of a waste.

  2. While this might work for deletions, what about additions or changes? And what about deletions in the middle, not the end of the line?

@pkoch
Copy link

pkoch commented Apr 21, 2019

What would be a good first patch to start exploring this?

PS: I'm also switching shells from bash, and this is really bumming me out. :/

@ridiculousfish
Copy link
Member

We would need to:

  1. Add a key binding for undo
  2. Build an undo (and presumably redo?) stack
  3. Teach the undo stack how to manipulate the command line

faho added a commit to faho/fish-shell that referenced this issue May 27, 2019
We add two "undo_list_t", one for undo and one for redo.

If we do anything that changes the commandline, we add an undo
element, if we undo, we add a redo element.

Also we coalesce small changes like normal string insertions and
single-character deletions.

This is only lightly tested and might not work for everything.

Fixes fish-shell#1367.
@faho faho mentioned this issue May 27, 2019
3 tasks
faho added a commit to faho/fish-shell that referenced this issue May 28, 2019
We add two "undo_list_t", one for undo and one for redo.

If we do anything that changes the commandline, we add an undo
element, if we undo, we add a redo element.

Also we coalesce small changes like normal string insertions and
single-character deletions.

This is only lightly tested and might not work for everything.

Fixes fish-shell#1367.
@lilyball
Copy link
Contributor

In terms of manipulating the command line, I would suggest adding undo manipulation as commandline flags.

@faho
Copy link
Member

faho commented Jun 25, 2019

adding undo manipulation as commandline flags.

I don't think that's necessary or helpful?

If you think about undoing, you probably think in terms of bindings - "I pressed this key, which changed the commandline from ABC to ABCXYZ, so if I press undo I expect it to be ABC again".

The exception being just typing characters and removing one character at a time with backspace - successive ones should be "squashed" together and treated as one change.

@lilyball
Copy link
Contributor

I meant as a way for the keybinding to actually invoke undo. The alternative is defining undo as a special input function, which I suppose would work as well.

@faho
Copy link
Member

faho commented Jun 25, 2019

The alternative is defining undo as a special input function, which I suppose would work as well.

That's the more typical thing to do (and what I did in #5897).

krobelus added a commit to krobelus/fish-shell that referenced this issue Feb 6, 2020
Add the input function undo which is bound to `\c_` (control + / on
some terminals). Redoing the most recent chain of undos is supported,
redo is bound to `\e/` for now.

Closes fish-shell#1367.
This approach should not have the issues discussed in fish-shell#5897.

Every single modification to the commandline (except for history search)
can be undone individually, except for adjacent single-character
inserts, which are coalesced into one undo group. One more exception:
the coalescing stops at space characters, so normally each word can be
undone separately.

When moving between history search entries, the undo history is reset.
This means that when accepting and editing search entry, undo can only
go back only as far as to the unmodified search entry.
Special handling for the original commandline ensures that when a
history search is cancelled (with Escape), or the user moves back to
the commandline, its undo history is preserved.

TODO:

- [ ] decide on the redo hotkey; `\e/` does not seem to work on xterm
      (it is tempting to add [emacs-style redo] but that would probably be less popular)
- [ ] vi mode: find some suitable bindings (`u`/`\cr` is taken)
- [ ] vi mode: find out how to bind in insert mode (which should then also work in the pager's search field)
- [x] update documentation

These areas should be mostly alright but there may be room for improvement:

- make sure the undo grouping behavior works nicely in practise
- make sure the interaction with history search feels right
- make sure the interaction with the pager feels right
- add some tests (interactive tests are missing - expect-based tests are tedious to write last time I tried)

[emacs-style redo]: <https://www.gnu.org/software/emacs/manual/html_node/emacs/Undo.html>
@krobelus krobelus mentioned this issue Feb 6, 2020
5 tasks
krobelus added a commit to krobelus/fish-shell that referenced this issue Feb 6, 2020
Add the input function undo which is bound to `\c_` (control + / on
some terminals). Redoing the most recent chain of undos is supported,
redo is bound to `\e/` for now.

Closes fish-shell#1367.
This approach should not have the issues discussed in fish-shell#5897.

Every single modification to the commandline can be undone individually,
except for adjacent single-character inserts, which are coalesced, so
they are undone with a single undo. There is one exception: the coalescing
stops at space characters, so each word can be undone separately.

When moving between history search entries, only the current history
search entry is reachable via the undo history. This allows to go back
to the original search string with a single undo, or by pressing the
escape key.
krobelus added a commit to krobelus/fish-shell that referenced this issue Feb 6, 2020
Add the input function undo which is bound to `\c_` (control + / on
some terminals). Redoing the most recent chain of undos is supported,
redo is bound to `\e/` for now.

Closes fish-shell#1367.
This approach should not have the issues discussed in fish-shell#5897.

Every single modification to the commandline can be undone individually,
except for adjacent single-character inserts, which are coalesced,
so they can be reverted with a single undo. Coalescing is not done for
space characters, so each word can be undone separately.

When moving between history search entries, only the current history
search entry is reachable via the undo history. This allows to go back
to the original search string with a single undo, or by pressing the
escape key.
Similarly, when moving between pager entries, only the most recent
selection in the pager can be undone.
krobelus added a commit to krobelus/fish-shell that referenced this issue Feb 7, 2020
Add the input function undo which is bound to `\c_` (control + / on
some terminals). Redoing the most recent chain of undos is supported,
redo is bound to `\e/` for now.

Closes fish-shell#1367.
This approach should not have the issues discussed in fish-shell#5897.

Every single modification to the commandline can be undone individually,
except for adjacent single-character inserts, which are coalesced,
so they can be reverted with a single undo. Coalescing is not done for
space characters, so each word can be undone separately.

When moving between history search entries, only the current history
search entry is reachable via the undo history. This allows to go back
to the original search string with a single undo, or by pressing the
escape key.
Similarly, when moving between pager entries, only the most recent
selection in the pager can be undone.
krobelus added a commit that referenced this issue Feb 7, 2020
Add the input function undo which is bound to `\c_` (control + / on
some terminals). Redoing the most recent chain of undos is supported,
redo is bound to `\e/` for now.

Closes #1367.
This approach should not have the issues discussed in #5897.

Every single modification to the commandline can be undone individually,
except for adjacent single-character inserts, which are coalesced,
so they can be reverted with a single undo. Coalescing is not done for
space characters, so each word can be undone separately.

When moving between history search entries, only the current history
search entry is reachable via the undo history. This allows to go back
to the original search string with a single undo, or by pressing the
escape key.
Similarly, when moving between pager entries, only the most recent
selection in the pager can be undone.
@krobelus krobelus modified the milestones: fish-future, fish 3.2.0 Feb 7, 2020
@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 7, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.