如何禁用zsh中带有URL和反斜杠的替换/自动补全功能

32

我正在使用zshoh-my-zsh在Ubuntu:14.04上。
当我粘贴URL时,Shell将转义字符自动补全为反斜杠。
例如,对于环境变量:

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

我该如何禁用这个功能?


另一个解决方法是在URL中添加引号。 - DimiDak
4个回答

55

更新于2019年5月12日:

新版本(> 486fa10)的oh-my-zsh已经有了这个配置,只需在source $ZSH/oh-my-zsh.sh之前添加DISABLE_MAGIC_FUNCTIONS=true即可:

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

通过链接查看:https://github.com/robbyrussell/oh-my-zsh/commit/486fa1010df847bfd8823b4492623afc7c935709


原始回答:

这是zsh 5.1.1 ~ 5.2(当前版本)中的一个错误。

插件bracketed-paste-magic在zsh版本中无法正常工作。

问题出在这里:

我建议您禁用bracketed-paste-magic
将这些代码从oh-my-zsh的~/.oh-my-zsh/lib/misc.zsh中注释掉即可解决问题。
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


2
bracketed-paste-magic插件是用来做什么的?为什么官方的ohmyzsh不禁用它。 - Ertuğrul Altınboğa
Ubuntu 14.04没有zsh 5.1(引入了括号粘贴功能),该版本是在2015年8月发布的,因此在提问之后发布。 - vinc17
@ErtuğrulAltınboğa 这个问题正在等待修复。https://github.com/robbyrussell/oh-my-zsh/issues/5499 - alswl
只是提供信息,我也在使用 zsh 5.4.2 (x86_64-apple-darwin17.3.0) 时看到了这种行为。 - Sankalp
更正:DISABLE_MAGIC_FUNCTIONS="true" - DimiDak

19

我看到了这个答案,但还是不明白为什么粘贴URL会转义像& ? [ ]这样的字符,导致我的curl命令失败。

罪魁祸首(对我来说)是Mac上的iTerm2。

要禁用这个行为,请进入iTerm2 > 首选项 > 配置文件 > 终端并取消勾选终端可能启用粘贴括号

确保您针对正确的配置文件执行此操作。

iTerm2 - 禁用粘贴括号


3
如果您在Iterm2中使用oh-my-zsh,您需要在上述描述中插入DISABLE_MAGIC_FUNCTIONS=true,然后再source oh-my-zsh.sh,否则您仍将看到相同的问题。 - Jeremy S.

11

如果URL没有加引号,反斜杠可能是必需的,这就是为什么zsh会添加它们(通过url-quote-magic)。如果你不喜欢它们,那么请给URL加上引号:

如果URL未被引用,则可能需要反斜杠,这就是 zsh 通过 url-quote-magic 添加反斜杠的原因。如果您不喜欢它们,请引用URL:
$ wget '

然后粘贴URL并输入结束引号:

$ wget 'http://{DEFAULT_IP}/index.html'
禁用整个 url-quote-magic 功能,请执行以下操作:
zstyle ':urlglobber' url-other-schema

编辑:从5.1版本开始,zsh在某些终端中支持方括号粘贴,此时url-quote-magic不再参与(bracketed-paste-magic取而代之用于粘贴)。


如何在oh-my-zsh中禁用这个功能?对我来说有点不方便。zsh这样做有什么原因吗? - Ertuğrul Altınboğa
这个zstyle命令似乎只对已登录的用户有效。如果其他用户尝试重新登录,则无效。它不是系统全局设置。如果我尝试将其添加到zshrc中,也是无效的。 - Ertuğrul Altınboğa
@LiMingHung 尝试将它添加到您的 .zshrc 结尾和您的 .zlogin 结尾(两者都可能需要:.zshrc 因为 .zlogin 不总是被调用,.zlogin 因为它在 .zshrc 之后被调用)。 - vinc17
哎呀,这也没用。 - Ertuğrul Altınboğa
@SkeletonBow 我怀疑你看到的引号问题是由其他原因引起的:括号粘贴。我认为你应该禁用 bracketed-paste-magic 或使用 backward-extend-paste,这样 bracketed-paste-magic 就可以在上下文中看到引号。注意:我已经编辑了我的答案,提到了一个新功能——括号粘贴。 - vinc17
显示剩余3条评论

3

显示我的zsh版本

echo $ZSH_VERSION
5.3

打开 misc.zsh。
vim ~/.oh-my-zsh/lib/misc.zsh

您将会看到以下内容:
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

在文件顶部添加以下行:
DISABLE_MAGIC_FUNCTIONS=true

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接