Zsh-autosuggestions: Suggestions not cleared after paste when using bracketed-paste-magic

Created on 1 Jul 2018  路  23Comments  路  Source: zsh-users/zsh-autosuggestions

Pretty sure this started somewhat recently, but now, if I paste something into the terminal and then hit the right arrow on the keyboard, it will append the suggestion that was in terminal before the paste.

zsh_autosuggestions

has-workaround plugin-compatibility pull-request-welcome reproduced

Most helpful comment

This seems to work for me, thanks. I thought I had tried this before, but it all seems to be working as I'd hope, so far. Here's my full setup:

# This speeds up pasting w/ autosuggest
# https://github.com/zsh-users/zsh-autosuggestions/issues/238
pasteinit() {
  OLD_SELF_INSERT=${${(s.:.)widgets[self-insert]}[2,3]}
  zle -N self-insert url-quote-magic # I wonder if you'd need `.url-quote-magic`?
}

pastefinish() {
  zle -N self-insert $OLD_SELF_INSERT
}
zstyle :bracketed-paste-magic paste-init pasteinit
zstyle :bracketed-paste-magic paste-finish pastefinish

# https://github.com/zsh-users/zsh-autosuggestions/issues/351
ZSH_AUTOSUGGEST_CLEAR_WIDGETS+=(bracketed-paste)

All 23 comments

Looks like an issue with bracketed-paste-magic. I'll look into it but for now, you could try one of these:

  • disabling bracketed-paste-magic (it's enabled by default in oh my zsh)
  • adding it to the list of widgets that clear the suggestion (see config section in the readme)
  • disabling suggestions while pasting (https://github.com/zsh-users/zsh-autosuggestions/issues/238#issuecomment-389324292)

A little more info: It looks like this was not an issue with zsh version 5.4.2, but is an issue with 5.5.1.

I do have this in my misc.zsh so I think bracketed pasted magic is disabled for me?

#autoload -Uz bracketed-paste-magic
#zle -N bracketed-paste bracketed-paste-magic

Regarding https://github.com/zsh-users/zsh-autosuggestions/issues/238#issuecomment-389324292, in which file should I put those methods?

I think bracketed pasted magic is disabled for me?

Ok I am able to reproduce it with bracketed-paste-magic, but not without, so I suspect you may still somehow have it enabled.

What do you get when you run echo $functions[bracketed-paste-magic]?

You may also try running zsh -f and sourcing this plugin manually to see if it is caused by something else in your zshrc. If that's the case you can systematically comment out things from your zshrc until the problem goes away to determine what line is causing the issue.

Regarding #238 (comment), in which file should I put those methods?

You can put them in your .zshrc or in any of the files loaded by your .zshrc, but they will only make a difference if you are using bracketed-paste-magic.

Ah, it appears overriding misc.zsh in $ZSH_CUSTOM directory is not working. Confirming disabling bracketed-paste-magic does resolve the issue. https://github.com/zsh-users/zsh-autosuggestions/issues/238#issuecomment-389324292 did speed up pasting, but did not resolve the issue.

Edit: I didn't have misc.zsh in $ZSH_CUSTOM/lib/misc.zsh`

I've reproduced something very similar to this (not yet sure if it deserves a new issue). I also do not have bracketed-paste-magic. echo $functions[bracketed-paste-magic] yields nothing.

For me, I don't need to press the right key to make the double suggestion appear. eg, in this sequence:

  1. I type ls, and the suggestion ls -altr appears.
  2. I then paste .zsh_history and the result is ls .zsh_history-altr.
  3. Hitting enter runs ls .zsh_history, so the appended suggestion is harmless, just confusing.

tester

The exact line that is causing the issue is this one. Commenting out that line (or reverting to the commit before) makes everything work perfectly.

zsh-autosuggestions: rev: 42f5a06f7f29007d91e37f85e1dc5a80f1b7c059
zsh: zsh 5.1.1 (x86_64-ubuntu-linux-gnu)
zplug: 2.4.2

I can also confirm that disabling bracketed-paste-magic fixed the problem with pasting.

@hlascelles Can you reproduce using zsh -f and sourcing the plugin manually?

@hlascelles You are absolutely correct =]
Cheers mate!

@hlascelles Thanks this does fix properly the suggestion being appended. The solution described in https://github.com/zsh-users/zsh-autosuggestions/issues/238#issuecomment-389324292 do not work completely when the paste do not happen on the start : e.g.

  1. Type :
    sh git clone
  2. autosuggestions suggests the previous clone url, e.g. [email protected]:qos-ch/slf4j.git
    sh git clone [email protected]:qos-ch/slf4j.git
  3. Paste something right after git clone (where the cursor is), e.g. [email protected]:zsh-users/zsh-autosuggestions.git
    sh git clone [email protected]:zsh-users/zsh-autosuggestions.git
    The pasted string is inverted.
  4. Press the right arrow
    sh git clone [email protected]:zsh-users/zsh-autosuggestions.git
    becomes
    sh git clone [email protected]:zsh-users/[email protected]:qos-ch/slf4j.git

@hlascelles Can you reproduce using zsh -f and sourcing the plugin manually?

@ericfreese yes:

$ zsh -f
host% autoload -Uz bracketed-paste-magic
host% zle -N bracketed-paste bracketed-paste-magic
host% source zsh-autosuggestions.zsh
host% source <paste anything here, then press left arrow>

Thank you for the simple steps @aaronjensen!

Yes, @ericfreese I can replicate the problem doing sourcing the plugin manually as per the steps above.

@aaronjensen @hlascelles I'm a little confused here. @hlascelles's comment said that he had bracketed-paste-magic disabled, but @aaronjensen's zsh -f repro steps have it enabled. I'm also assuming @aaronjensen meant the _right_ arrow instead of _left_? These seem to be repro steps for the original issue (with bpm enabled).

@hlascelles Are you able to reproduce the issue you posted about using zsh -f?

@hlascelles Are you able to reproduce the issue you posted about using zsh -f?

@ericfreese I've had this issue too, and I'm able reproduce this problem using zsh -f following @aaronjensen's steps.

If I comment out bpm in lib/misc.zsh or preferably, my own at $ZSH_CUSTOM/lib/misc.zsh, the issue goes away.

@aaronjensen @hlascelles I'm a little confused here. @hlascelles's comment said that he had bracketed-paste-magic disabled, but @aaronjensen's zsh -f repro steps have it enabled

You are correct, sorry I didn't read the thread closely enough. My repro steps are for the originally reported issue. I don't know how to repro it w/o bpm.

I'm also assuming @aaronjensen meant the _right_ arrow instead of _left_?

I meant left, as that triggers displaying the suggestion (but not filling it). Right is problematic too as that fills the suggestion.

Ok, that makes sense. Lets keep this thread about the bracketed-paste-magic issue and if there is another issue that we can recreate _without_ bracketed-paste-magic, lets open a new issue for it.

There is a very easy workaround for this issue which is to add bracketed-paste to the list of widgets that clear the suggestion. I've added a spec to the test suite to demonstrate the workaround: https://github.com/zsh-users/zsh-autosuggestions/commit/ce6ee94f30abeaa63cfd5d3c861175e15045f615

Because there's such an easy workaround for this, it'll be pretty low priority for me, but if someone wants to put up a PR that fixes it and doesn't break anything else I'll be happy to merge.

This seems to work for me, thanks. I thought I had tried this before, but it all seems to be working as I'd hope, so far. Here's my full setup:

# This speeds up pasting w/ autosuggest
# https://github.com/zsh-users/zsh-autosuggestions/issues/238
pasteinit() {
  OLD_SELF_INSERT=${${(s.:.)widgets[self-insert]}[2,3]}
  zle -N self-insert url-quote-magic # I wonder if you'd need `.url-quote-magic`?
}

pastefinish() {
  zle -N self-insert $OLD_SELF_INSERT
}
zstyle :bracketed-paste-magic paste-init pasteinit
zstyle :bracketed-paste-magic paste-finish pastefinish

# https://github.com/zsh-users/zsh-autosuggestions/issues/351
ZSH_AUTOSUGGEST_CLEAR_WIDGETS+=(bracketed-paste)

Maybe this will help someone.

I was using the code just above and was getting unused suggestions showing in the terminal buffer after executing a command - zsh-autosuggestions 0.6.3, macOS, iTerm.

I added accept-line to ZSH_AUTOSUGGEST_CLEAR_WIDGETS and it fixed the issue.

ZSH_AUTOSUGGEST_CLEAR_WIDGETS+=(bracketed-paste accept-line)

Example (italic text for gray suggestions. | for cursor position)

% git status --porcelain
% git status| _--porcelain_ [hit enter]
% git status --porcelain [buffer displays this]
[output for git status only]

Flag --porcelain is showing in buffer with white text as if it was executed, but only git status was executed.

Can confirm I have this issue and I have bracketed-paste-magic present as well. Like @ericfreese and others mentioned, the fix is to add ZSH_AUTOSUGGEST_CLEAR_WIDGETS+=(bracketed-paste accept-line) to your ~/.zshrc file.

I used to have both the slow-paste issue and the wrong autocompletion after paste. I used to solve it with similar code to https://github.com/zsh-users/zsh-autosuggestions/issues/351#issuecomment-483938570 before I decided to have a look into oh-my-zsh.

I found this: https://github.com/ohmyzsh/ohmyzsh/blob/master/lib/misc.zsh#L4-L16

... which sets up everything we want to undo.

So setting DISABLE_MAGIC_FUNCTIONS=true before sourcing oh-my-zsh does the trick in a much simpler way. It also skips url-quote-magic which I don't care about.

@levito that's the nuclear option as it disables those things entirely. The above only disables it when it matters and still lets you use them. If you don't want them though, by all means use that approach. If you want bracketed paste magic and url quote magic, then the original comment will work.

This seems to work for me, thanks. I thought I had tried this before, but it all seems to be working as I'd hope, so far. Here's my full setup:

# This speeds up pasting w/ autosuggest
# https://github.com/zsh-users/zsh-autosuggestions/issues/238
pasteinit() {
  OLD_SELF_INSERT=${${(s.:.)widgets[self-insert]}[2,3]}
  zle -N self-insert url-quote-magic # I wonder if you'd need `.url-quote-magic`?
}

pastefinish() {
  zle -N self-insert $OLD_SELF_INSERT
}
zstyle :bracketed-paste-magic paste-init pasteinit
zstyle :bracketed-paste-magic paste-finish pastefinish

# https://github.com/zsh-users/zsh-autosuggestions/issues/351
ZSH_AUTOSUGGEST_CLEAR_WIDGETS+=(bracketed-paste)

This workaround works perfect!!!

# This speeds up pasting w/ autosuggest
# https://github.com/zsh-users/zsh-autosuggestions/issues/238
pasteinit() {
  OLD_SELF_INSERT=${${(s.:.)widgets[self-insert]}[2,3]}
  zle -N self-insert url-quote-magic # I wonder if you'd need `.url-quote-magic`?
}

pastefinish() {
  zle -N self-insert $OLD_SELF_INSERT
}
zstyle :bracketed-paste-magic paste-init pasteinit
zstyle :bracketed-paste-magic paste-finish pastefinish

# https://github.com/zsh-users/zsh-autosuggestions/issues/351
ZSH_AUTOSUGGEST_CLEAR_WIDGETS+=(bracketed-paste)

This breaks the rare case that we want to see the suggestions for the pasted content. Otherwise, seems to work well!

PS: I already had accept-line in ZSH_AUTOSUGGEST_CLEAR_WIDGETS.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kergoth picture kergoth  路  3Comments

NikKovIos picture NikKovIos  路  4Comments

silverneko picture silverneko  路  5Comments

dipam7 picture dipam7  路  3Comments

fgr0 picture fgr0  路  3Comments