zsh 或 bash 可以扩展引用目录的历史表达式吗?

发布于 2024-11-29 22:23:21 字数 657 浏览 0 评论 0原文

例如,假设我刚刚复制了一些内容:

mv foo_file.txt ~/to/some/long/path/that/i/do/not/want/to/retype

并且我想像这样使用历史替换:

mv bar_file.txt !!:2

我很惊讶当我点击 [tab 时 zsh 没有为我扩展 !!:2 ]。在对历史论点进行更复杂的引用时,我可能真的希望在返回之前进行扩展,这样我就可以确定地知道我引用了正确的论点。有什么办法让它做到这一点吗? (我希望这是默认行为。这是我无意中禁用或破坏的默认行为吗?)

如果 zsh 不能做到这一点,那么 bash 可以吗?

更新:如果 zsh 引用文件而不是目录,则 zsh 将扩展历史表达式:

mv foo_file.txt foo_bar_file.txt
mv bar_file.txt !!:2[TAB]

如果它只是任意字符串,它将扩展它:

echo one two three four
echo !!:1[TAB]

但如果您试图将某些内容移动到一个目录。看起来越来越像这肯定是一个错误。

For example, let's suppose I just copied something:

mv foo_file.txt ~/to/some/long/path/that/i/do/not/want/to/retype

and I'd like to use history substitution like so:

mv bar_file.txt !!:2

I'm surprised that zsh is not expanding the !!:2 for me when I hit [tab]. In a more complex reference to a historical argument I might really want the expansion before I hit return, just so I know with certainty that I referred to the correct argument. Is there any way to make it do that? (I would expect that to be the default behavior. Is it the default behavior, that I have somehow inadvertently disabled or broken?)

If zsh can't do it, can bash?

UPDATE: zsh will expand the history expression if it refers to a file, but not a directory:

mv foo_file.txt foo_bar_file.txt
mv bar_file.txt !!:2[TAB]

It will expand it if it is just an arbitrary string:

echo one two three four
echo !!:1[TAB]

But not if you're trying to move something to a directory. It looks more and more like this must be a bug.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

初见 2024-12-06 22:23:21

我在 cygwin 中使用 zsh:

$ zsh --version
zsh 4.3.12 (i686-pc-cygwin)

$ setopt
interactive
monitor
shinstdin
zle

我刚刚尝试了以下操作:

$ touch foo_file.txt bar_file.txt
$ mkdir -p ~/to/some/long/path/that/i/do/not/want/to/retype
$ mv foo_file.txt ~/to/some/long/path/that/i/do/not/want/to/retype

然后我尝试了上面提到的制表符补全:

$ mv bar_file.txt !!:2[TAB]

它工作正常,最后一个参数扩展如下:

$ mv bar_file.txt ~/to/some/long/path/that/i/do/not/want/to/retype

I am using zsh in cygwin:

$ zsh --version
zsh 4.3.12 (i686-pc-cygwin)

$ setopt
interactive
monitor
shinstdin
zle

I just tried the following:

$ touch foo_file.txt bar_file.txt
$ mkdir -p ~/to/some/long/path/that/i/do/not/want/to/retype
$ mv foo_file.txt ~/to/some/long/path/that/i/do/not/want/to/retype

I then tried the tab completion mentioned above:

$ mv bar_file.txt !!:2[TAB]

and it worked fine, the last argument being expanded as follows:

$ mv bar_file.txt ~/to/some/long/path/that/i/do/not/want/to/retype
眼眸里的快感 2024-12-06 22:23:21

您可以在 bash 中对其进行伪破解:

$ shopt -s histreedit
$ shopt -s histverify

然后,实际尝试扩展:

$ echo !!:2 [now hit enter]
$ echo histverify

现在您无法在 bash 中进行制表符扩展。明确地说不是。这是因为 bash 扩展的处理顺序不同。

You can pseudo-hack it in bash:

$ shopt -s histreedit
$ shopt -s histverify

Then, to actually try an expansion:

$ echo !!:2 [now hit enter]
$ echo histverify

Now you can't do tab expansion in bash. Unequivocally no. That's because of the order in which bash expansion is processed.

苏璃陌 2024-12-06 22:23:21

非常适合我使用 zsh 4.3.17。听起来您可能有一个可能值得在zsh-上报告的错误用户邮件列表。但是,至少还有五个其他键绑定可以实现您想要的功能:默认情况下绑定到 expand-wordCx *Esc Space > 或 Meta-SpaceEsc !Meta-! 这些都是默认情况下绑定到 expand-history。 (Meta 对很多人来说意味着 Alt 键,尽管它取决于您的终端设置。)

话虽如此,Esc . (或 Meta-.Alt-.) 是从历史记录中的上一行检索最后一个单词的更好方法,因为它提供即时视觉反馈。您还可以通过重复按键盘快捷键来选择旧行中的最后一个单词,甚至可以通过在快捷键前添加 Alt-< 来选择上一行的最后一个单词 n em>n(或 Meta-nEsc n)。因此,例如要从历史记录的第三行中检索倒数第二个单词,序列将为:

  • Meta-.(返回历史记录的一行,选择最后一个单词从该行)
  • Meta-.(返回另一个,再次选择最后一个单词)
  • Meta-2 <代码>元 - <代码>。(去返回另一个,但这次选择该行中的倒数第二个单词)

Works perfectly for me with zsh 4.3.17. Sounds like you probably have a bug which might be worth reporting on the zsh-user mailing list. However there are at least five other keybindings which should accomplish what you want: C-x * which is by default bound to expand-word, and Esc Space or Meta-Space or Esc ! or Meta-! which are all bound to expand-history by default. (Meta means the Alt key for many people, although it depends on your terminal setup.)

Having said that, Esc . (or Meta-. or Alt-.) is a nicer way of retrieving the last word from the previous line in the history, since it provides instant visual feedback. You can also choose the last word from older lines by repeatedly pressing the keyboard shortcut, or even the n th last word on a previous line by prefixing the shortcut with Alt-n (or Meta-n or Esc n). So for example to retrieve the penultimate word from the 3rd newest line of history, the sequence would be:

  • Meta-. (goes back one line of history, selecting the last word from that line)
  • Meta-. (goes back another, again selecting the last word)
  • Meta-2 Meta-. (goes back another, but this time selects the penultimate word from that line)
如歌彻婉言 2024-12-06 22:23:21

我已经尝试过你所描述的,但我认为 bash 也不支持这一点。

I've tried what you've described, and I don't think bash supports this either.

旧人哭 2024-12-06 22:23:21

在 bash 中,Alt-.通常绑定到 yank-last-arg,这会给你你想要的。

这是可以绑定到击键的历史相关命令的完整列表的链接
http://www.gnu.org/software/bash /manual/bashref.html#Commands-For-History

例如,

ls /etc/passwd /etc/group
cat 
# press Alt+. (Alt dot) here, bash adds /etc/group
cat /etc/group
# press space, then press Alt+1 Alt+.
# bash adds the first argument of the previous command /etc/passwd
cat /etc/group /etc/passwd

In bash, Alt-. is typically bound to yank-last-arg, which will give you what you want.

Here is a link to the whole list of history related commands that can be bound to keystrokes
http://www.gnu.org/software/bash/manual/bashref.html#Commands-For-History

For example,

ls /etc/passwd /etc/group
cat 
# press Alt+. (Alt dot) here, bash adds /etc/group
cat /etc/group
# press space, then press Alt+1 Alt+.
# bash adds the first argument of the previous command /etc/passwd
cat /etc/group /etc/passwd
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文