How to disable zsh substitution/autocomplete with URL and backslashes
Asked Answered
K

4

38

I am using zsh with oh-my-zsh on Ubuntu:14.04.

The shell autocompletes escape character with backslash when I paste a URL.

For example with environment variables:

$ wget http://{DEFAULT_IP}/index.html
It will become:
$ wget http://\{DEFAULT_IP\}/index.html

How can I disable this function?

Keratosis answered 2/9, 2014 at 1:22 Comment(1)
Another workaround is to add quotes to the URL.Hamal
M
59

update 2019-05-12:

new version(> 486fa10) oh-my-zsh have a configuration for this, add DISABLE_MAGIC_FUNCTIONS=true before source $ZSH/oh-my-zsh.sh:

DISABLE_MAGIC_FUNCTIONS=true
source $ZSH/oh-my-zsh.sh

via: https://github.com/robbyrussell/oh-my-zsh/commit/486fa1010df847bfd8823b4492623afc7c935709


Original answer:

This is a bug in zsh 5.1.1 ~ 5.2(current).

The plugin bracketed-paste-magic did not works in the zsh versions.

The issue is here:

I suggest you disable bracketed-paste-magic.

Comment these code from oh-my-zsh's ~/.oh-my-zsh/lib/misc.zsh solve the problem:

if [[ $ZSH_VERSION != 5.1.1 ]]; then
  for d in $fpath; do
    if [[ -e "$d/url-quote-magic" ]]; then
      if is-at-least 5.1; then
        autoload -Uz bracketed-paste-magic
        zle -N bracketed-paste bracketed-paste-magic
      fi
      autoload -Uz url-quote-magic
      zle -N self-insert url-quote-magic
      break
    fi
  done
fi

via

Monumentalize answered 25/11, 2016 at 7:27 Comment(5)
What is bracketed-paste-magic this plugin used for? Why the official ohmyzsh doesn't disable it.Stria
Ubuntu 14.04 does not have zsh 5.1 (which introduced bracketed paste), which has been released in August 2015, thus after the question was asked.Kearse
@ErtuğrulAltınboğa It is waitting fix. github.com/robbyrussell/oh-my-zsh/issues/5499Monumentalize
Just fyi, I'm seeing this behaviour with zsh 5.4.2 (x86_64-apple-darwin17.3.0) tooIndevout
Correction: DISABLE_MAGIC_FUNCTIONS="true"Hamal
I
20

I ended up at this answer but still could not understand why pasting a URL was escaping characters like & ? [ ] and causing my curl commands to fail.

The culprit (for me) was iTerm2 on Mac.

To disable the behavior, go into iTerm2 > Preferences > Profiles > Terminal and UNCHECK Terminal may enable paste bracketing.

Make sure you do this for the correct profile.

iTerm2 - Disable paste bracketing

Immersion answered 7/2, 2020 at 18:24 Comment(1)
If you use oh-my-zsh in Iterm2 you need to insert DISABLE_MAGIC_FUNCTIONS=true before source oh-my-zsh.sh as described above as well otherwise you will still see the same issue.Inventive
K
11

If the URL is not quoted, the backslashes may be necessary, that's why zsh adds them (via url-quote-magic). If you do not like them, then quote the URL:

$ wget '

then paste the URL and type the ending quote:

$ wget 'http://{DEFAULT_IP}/index.html'

To disable the url-quote-magic feature entirely:

zstyle ':urlglobber' url-other-schema

EDIT: As of version 5.1, zsh supports bracketed paste in some terminals, in which case url-quote-magic is no longer involved (bracketed-paste-magic replaces it for pastes).

Kearse answered 2/9, 2014 at 11:57 Comment(8)
How do I disabed this function in oh-my-zsh? It is a little inconvenient for me. Is any reason for zsh doing this?Stria
@LiMingHung I've updated my answer. And zsh (actually oh-my-zsh) does this because this is what most users expect, in particular due to the & character that can be found in many URL's and thus needs to be quoted.Kearse
This zstyle command looks like only effect on logined user. If other users try to login again. It is useless. Not a system global settings. If I try to add it in zshrc, also useless.Stria
@LiMingHung Try to add it at the end of your .zshrc and at the end of your .zlogin (both may be necessary: .zshrc because .zlogin is not always sourced, and .zlogin because it is sourced after .zshrc).Kearse
Ooops. It is also useless.Stria
For me, backslashes are added even if I put in quotes. I tried adding the last bit of your answer to my .zshrc file, but it didn't work. I can't find the .zlogin file, unfortunately.Campus
@SkeletonBow I suspect that the issue you see with quotes is due to something else: bracketed paste. I think that you should either disable bracketed-paste-magic or use backward-extend-paste so that bracketed-paste-magic can see the quote in the context. Note: I've edited my answer to mention bracketed paste, which is a new feature.Kearse
THANK YOU. The zstyle disable did the trick. Love you!!Collator
O
4

show my zsh version

echo $ZSH_VERSION
5.3

open misc.zsh

vim ~/.oh-my-zsh/lib/misc.zsh

you will see the following:

autoload -Uz is-at-least

# *-magic is known buggy in some versions; disable if so
if [[ $DISABLE_MAGIC_FUNCTIONS != true ]]; then
  for d in $fpath; do
    if [[ -e "$d/url-quote-magic" ]]; then
        if is-at-least 5.1; then
            autoload -Uz bracketed-paste-magic
            zle -N bracketed-paste bracketed-paste-magic
        fi
        autoload -Uz url-quote-magic
        zle -N self-insert url-quote-magic
      break
    fi
  done
fi

## jobs
setopt long_list_jobs

env_default 'PAGER' 'less'
env_default 'LESS' '-R'

## super user alias
alias _='sudo'

## more intelligent acking for ubuntu users
if which ack-grep &> /dev/null; then
  alias afind='ack-grep -il'
else
  alias afind='ack -il'
fi

# only define LC_CTYPE if undefined
if [[ -z "$LC_CTYPE" && -z "$LC_ALL" ]]; then
    export LC_CTYPE=${LANG%%:*} # pick the first entry from LANG
fi

# recognize comments
setopt interactivecomments

add the following line at the top of the file

DISABLE_MAGIC_FUNCTIONS=true
Ostraw answered 17/7, 2019 at 3:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.