简体   繁体   English

如何在bash中从zsh模拟全局别名?

[英]How to emulate global aliases from zsh in bash?

In zsh , you can define global aliases. zsh ,您可以定义全局别名。 For example, 例如,

alias -g G='| grep'
alias -g W='| wc -l'

and then use them like this: 然后像这样使用它们:

some_command G some_text W

Can global aliases be somehow emulated in bash ? 可以在bash模拟全局别名吗?

Assuming you never want to use capital G for any other purpose, you could add this to your .bashrc file: 假设您从不希望将大写字母G用于任何其他目的,可以将其添加到.bashrc文件中:

bind '"G": "| grep"'

This will expand G to | grep 这会将G扩展为| grep | grep as soon as you type it. 键入| grep That is, Shift+g no longer produces a capital G, but the string | grep 也就是说,Shift + g不再产生大写字母G,但字符串| grep | grep . | grep You'd want to choose a longer sequence that you would never expect to otherwise type, either by itself or as part of another word. 您可能想要选择一个更长的序列,您将不会期望自己输入或作为其他单词的一部分来输入。

You can save it into a variable 您可以将其保存到变量中

g='| grep'

Then, your command could be 然后,您的命令可能是

eval some_command $G some_text

OR 要么

some_command $G some_text Ctrl + Alt + E (on other systems, it seems to be ? ctrl+x+* and is configurable) some_command $G some_text Ctrl + Alt + E (在其他系统上,它似乎是ctrl+x+*并且是可配置的)

which expands the command to the desired some_command | grep some_text 这会将命令扩展为所需的some_command | grep some_text some_command | grep some_text , so you can then press Enter. some_command | grep some_text ,因此您可以按Enter。 Variables that should not be expanded would have to be in single quotes. 不应扩展的变量必须用单引号引起来。

Nothing like zsh though. 没什么比zsh好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM