内容简介:My shell means everything to me. It is the first program I start in the morning and the last one I close in the evening. It is the program that I regularly tweak to boost my overall productivity. From my perspective, Microsoft’s release of Windows Terminal
My shell means everything to me. It is the first program I start in the morning and the last one I close in the evening. It is the program that I regularly tweak to boost my overall productivity. From my perspective, Microsoft’s release of Windows Terminal 1.0 and WSL 2.0 will boost overall Shell adoption. Over the past decade, I have seen numerous users installing and using different shells. However, the number of people that invest time in configuring their shell to be more productive is just a small percentage. And that is a shame!
You can also stick with tools like Finder or Windows Explorer if you install and use a shell without customizing it. Shells like ZSH shine once they have individual configurations: configurations that make you - the user - super productive. I wrote this article for inspiration. It should demonstrate how you can increase your overall productivity with simple things like adopting the 5 types of aliases in ZSH. Aliases are the first step towards a tailored shell experience. Aliases are a super individual thing.
I use Docker, Kubernetes, and Microsoft Azure every day. That said, it makes sense for me to have aliases supporting me with these tools and environments. However, maybe you are using different clouds and command-line tools so that you will end up with different aliases. The key takeaway should be that you create and use aliases to help you get your job done.
All your aliases are defined in ~/.zshrc
. ZSH loads the configuration file during startup. However, you can always force to reload your configuration file use source ~/.zshrc
.
This article will teach you how to create and use these four types of aliases:
Simple Aliases
Simple Aliases are a good starting point. I use them to reduce the number of keystrokes to fire up a command. The definition of a simple alias uses the alias my-alias="command"
pattern.
# navigation aliases alias dev="cd ~/dev/" alias personal="cd ~/dev/thorstenhans" alias business="cd ~/dev/thinktecture" # kubectl aliases alias k="kubectl" alias kgx="kubectl config get-contexts" # docker aliases alias d="docker" alias dps="docker ps"
Having those aliases registered in my ~/.zshrc
, I can navigate quickly to commonly used directories and to fire-up commands that I use frequently using just a few keystrokes.
Suffix Aliases
Suffix Aliases are defined using the -s
flag. With suffix aliases, you can launch files with a specific extension (or suffix) in your favorite tool. To register a suffix alias, we use the alias -s extension=name-of-the-tool
pattern. The following samples assume that VisualStudio Code is installed and can be launched using the code
command from the terminal.
# Azure CLI files alias -s azcli=code # Markdown files alias -s md=code # JSON files alias -s json=code
Load the modified ~/.zshrc
and execute the following:
echo "#Hello World" > sample.md # now type the name of the file and commit via ENTER sample.md
The suffix alias translates sample.md
into code sample.md
, and you should see VisualStudio Code launching and showing the content of sample.md
.
Functions for Aliases With Parameters
Sometimes you want to create aliases the require some contextual information. Parameters are used to describe this contextual information for shell commands. The registration of aliases with parameters looks pretty similar to a function definition in regular programming languages. We use the following pattern to define aliases with parameters:
aliasname() { command $firstParam $secondParam }
For example, consider you want to optimize the process of listing Azure Kubernetes Service (AKS) instances in your Azure Subscription. To achieve this, Azure CLI provides the az aks list
command. The command allows further configuration by adding several parameters.
The following alias accepts two parameters—first, the Resource Group Name (Resource Groups are logical containers in Azure). The second parameter specifies the desired output format.
getaks() { az aks list -g $1 -o $2 }
We can now use the getaks
alias with parameters to look for AKS instances in resource-group-1
and print results in colored JSON ( jsonc
) using getaks resource-group-1 jsonc
.
Global Aliases
Global aliases are defined using the -g
flag. A global alias is aggressive. Once registered, it replaces all occurrences of the alias name with the specified command. The definition follows the pattern alias -g aliasName="command"
.
# I have to ask for Azure Resource Ids frequently alias -g qId="--query id -o tsv"
Having qId
registered as a global alias, I can optimize my performance when asking for unique resource identifiers using Azure CLI. See the following snippet comparing the regular command, to the optimized one using my global alias:
# proper command to query the identifier of an AKS instance az aks show -n myaks2020 -g rg-demo --query id -o tsv # command to query the identifier of an AKS instance with global alias az aks show -n myaks2020 -g rg-demo qId
Operating system specific aliases
An operating system specific alias is not a real type. However, I think platform-specific aliases are important—especially when using multiple platforms. I use macOS as a daily driver, so I also have some aliases tied to my rig.
However, I love VisualStudio Codespaces that are using Linux as an operating system. In both situations, I want to use my dotfiles. To achieve this, I use simple if
-statements.
# macOS aliasses if [[ $OSTYPE == darwin* ]]; then alias flush='dscacheutil -flushcache' # Apps alias browse="open -a /Applications/Google\ Chrome.app" # * Browse Azure Portal alias azure="browse https://preview.portal.azure.com" fi
See the following image showing all my platform related aliases for macOS:
Use Aliases To Edit and Reload .zshrc
I have two aliases in my ~/.zshrc
, which are super-efficient. I open my ~/.zshrc
in my favorite editor with ec
and source it (apply the current state of ~/.zshrc
to your ZSH session) with sc
. The alias definition for both looks like this:
# open ~/.zshrc in using the default editor specified in $EDITOR alias ec="$EDITOR $HOME/.zshrc" # source ~/.zshrc alias sc="source $HOME/.zshrc"
Conclusion
You have seen four different types of ZSH aliases that will boost your productivity. Foud different types of aliases used to achieve almost everything in your shell.
Take your time. Do some research. Identify the command-line tools you are using daily or regularly, find patterns, and create corresponding aliases that increase your productivity by reducing keystrokes.
Having powerful shells on all operating systems (finally including windows using WSL 2.0), you should care about your shell performance. Make ZSH your own.
I do a lot of conference speaking; if you are presenting at conferences or meetups too, you should start your presentation by quickly explaining the aliases you will use throughout the talk to onboard your audience. Maybe they are using kc
instead of k
for kubectl
. :joy:
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
人工智能的未来
Jeff Hawkins、Sandra Blakeslee / 贺俊杰、李若子、杨倩 / 陕西科学技术出版社 / 2006.1 / 18.5
陕西科技出版社最新引进美国图书《人工智能的未来》(On Intelligence)一书,是由杰夫•霍金斯,一位在硅谷极其成功、受人尊敬的计算机工程师、企业家与桑德拉•布拉克斯莉,《纽约日报》的栏目作家共同撰写。本书对人类大脑皮层所具有的知觉、认识、行为和智能功能新理论提出了新的理论构想。这一理论的独到之处在于对大脑皮层的现行认识提出了新的观点,对大脑的工作原理,即霍金斯称之为“真正智能”而非计算机......一起来看看 《人工智能的未来》 这本书的介绍吧!