简体   繁体   English

bash/powershell 中有没有一种方法可以根据上下文在命令中自动包含一个单词?

[英]Is there a way in bash/powershell to automatically include a word in a command based on context?

Currently, when inside git repos, I type git status .目前,当在 git 存储库中时,我输入git status Is there any way for my shell to understand this is a git repo and automatically append git to relevant commands?我的 shell 有什么办法可以理解这是一个 git 回购并自动 append git到相关命令

eg,例如,

# bash example
# before
git status

# after
status # "status" isn't a registered command, but "git status" is, so bash uses the latter

I'd probably only want this enabled when I'm in a repo, but is there any way to get functionality similar to this in any of the popular shells?我可能只希望在回购时启用此功能,但是有没有办法在任何流行的 shell 中获得与此类似的功能?

I recognize this isn't always preferred or the best idea, but it could save me from typing a word.我承认这并不总是首选或最好的主意,但它可以让我免于输入单词。

This is what aliases were invented for in bash.这就是在 bash 中发明的别名。 You'd add a line to your bashrc that looked like this:您将在 bashrc 中添加一行,如下所示:

alias status='git status'

If you'd like, you could add one for every git subcommand you ran frequently:如果您愿意,可以为您经常运行的每个 git 子命令添加一个:

alias commit='git commit'
alias checkout='git checkout'
alias add='git add'
alias push='git push'
alias pull='git pull'
alias config='git config'

Your environment is your own, and if you wanna make a shorthand for git commands, you're free to do so!你的环境是你自己的,如果你想为 git 命令做一个简写,你可以这样做!

Also, these aliases will pass any arguments to the underlying command.此外,这些别名会将任何 arguments 传递给底层命令。 So if you write:所以如果你写:

commit -m "My message"

This will translate to git commit -m "My message" .这将转换为git commit -m "My message"

These aliases should be added to a file like the .bashrc file in your home directory, if one exists.这些别名应该添加到主目录中的.bashrc文件之类的文件中(如果存在)。

See here for a more in-depth explanation of aliases.有关别名的更深入解释,请参见此处。

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

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