zsh 或 bash 可以扩展引用目录的历史表达式吗?
例如,假设我刚刚复制了一些内容:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我在 cygwin 中使用 zsh:
我刚刚尝试了以下操作:
然后我尝试了上面提到的制表符补全:
它工作正常,最后一个参数扩展如下:
I am using zsh in cygwin:
I just tried the following:
I then tried the tab completion mentioned above:
and it worked fine, the last argument being expanded as follows:
您可以在 bash 中对其进行伪破解:
然后,实际尝试扩展:
现在您无法在 bash 中进行制表符扩展。明确地说不是。这是因为 bash 扩展的处理顺序不同。
You can pseudo-hack it in bash:
Then, to actually try an expansion:
Now you can't do tab expansion in bash. Unequivocally no. That's because of the order in which bash expansion is processed.
非常适合我使用
zsh 4.3.17
。听起来您可能有一个可能值得在zsh-上报告的错误用户邮件列表。但是,至少还有五个其他键绑定可以实现您想要的功能:默认情况下绑定到expand-word
的Cx *
和Esc Space
> 或Meta
-Space
或Esc !
或Meta
-!
这些都是默认情况下绑定到expand-history
。 (Meta
对很多人来说意味着Alt
键,尽管它取决于您的终端设置。)话虽如此,
Esc .
(或Meta
-.
或Alt-.
) 是从历史记录中的上一行检索最后一个单词的更好方法,因为它提供即时视觉反馈。您还可以通过重复按键盘快捷键来选择旧行中的最后一个单词,甚至可以通过在快捷键前添加Alt
-< 来选择上一行的最后一个单词 n em>n(或Meta
-n 或Esc
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 toexpand-word
, andEsc Space
orMeta
-Space
orEsc !
orMeta
-!
which are all bound toexpand-history
by default. (Meta
means theAlt
key for many people, although it depends on your terminal setup.)Having said that,
Esc .
(orMeta
-.
orAlt-.
) 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 withAlt
-n (orMeta
-n orEsc
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)我已经尝试过你所描述的,但我认为 bash 也不支持这一点。
I've tried what you've described, and I don't think bash supports this either.
在 bash 中,Alt-.通常绑定到 yank-last-arg,这会给你你想要的。
这是可以绑定到击键的历史相关命令的完整列表的链接
http://www.gnu.org/software/bash /manual/bashref.html#Commands-For-History
例如,
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,