banner
LegalGeek

LegalGeek

搞产品的法律人

Setting alias for terminal commands

Summary: Use the alias command to create abbreviations for commonly used terminal commands and improve terminal operation efficiency.

1. Introduction to Alias#


In Linux systems, the alias command is used to set aliases for commands. Users can use aliases to customize command names.

Syntax: alias [alias]=[command name]

If only the alias command is entered, all current alias settings will be listed. The effect of the alias command is only valid for the current login session. If you want to automatically set aliases every time you log in, you can set the command aliases in .profile or .cshrc.

2. Setting Aliases for macOS System Bash or Zsh#


Repeatedly typing commonly used commands can be time-consuming. You can simplify operations by using aliases.

For bash Terminal#

  1. Edit ~/.bash_profile, for example, add PS:= without spaces around it:
alias co='git checkout'
alias myip="ifconfig | grep '192'"
alias fetch=' git fetch upstream'
alias merge='git merge'

PS: If there is no .bash_profile file, create a .bash_profile file and then edit it.

touch .bash_profile
vim .bash_profile
  1. Execute as usual (or restart the terminal).
source ~/.bash_profile

For zsh Terminal#

If you are using zsh and have configured oh-my-zsh, you will find that the above management configuration is invalid. This is because after configuring zsh, opening a new terminal will not execute .bash_profile in the same way as bash. source ~/.bash_aliases is not executed, so it does not take effect. Instead, it uses the .zshrc file.

For simple operations, you can directly modify ~/.zshrc, copy the original aliases into it, usually adding aliases to the end of the file, and then source it.

nano ~/.zshrc
## Add aliases to the end and save, for example: alias rezsh="source ~/.zshrc"
source ~/.zshrc

3. Setting Aliases for Windows System Powershell#


Overview of the Principle#

If you search for the keywords "windows powershell set user alias", Google will usually give you Microsoft's official documentation. However, this document only tells us how to set temporary aliases in scripts. What if you want to set permanent aliases? In fact, this kind of "alias", also known as alias, is almost nonexistent in all scripting languages. When we use scripting tools such as Linux bash and Cmder to open the terminal, the system will execute a script file by default (bash is .bashrc in the user's home directory, Cmder is config/user_aliases.cmd), and this script file contains alias definitions. This is why we must log out and log in again or source .bashrc after modifying .bashrc in Linux-like systems.

Therefore, we only need to modify the file executed when Windows Powershell starts. Many forums say that the default script executed is $Home\Documents\profile.ps1, which is C:\Users\your username\Documents\profile.ps1, but this is not correct. The best way is to start PowerShell first and then execute echo $profile, so that the obtained file path is the default execution file path of PowerShell.

Viewing Existing Aliases#

  • View all aliases set in this session: Get-Alias or gal
PS C:\> Get-Alias
CommandType     Name
-----------     ----
Alias           % -> ForEach-Object
Alias           ? -> Where-Object
Alias           ac -> Add-Content
Alias           asnp -> Add-PSSnapin
  • View the original command of a certain alias, such as the original command of ls: > Get-Alias ls
  • View the aliases of a certain original command, such as the aliases of Get-ChildItem: > Get-Alias -Definition Get-ChildItem

Creating Permanent Aliases#

Aliases created directly using the Set-Alias or New-Alias command in PowerShell will become invalid after closing this session. To prevent this phenomenon, write this command into the Windows PowerShell profile file.
View the location of this file on the computer:

PS C:\> $profile

Generally, this file does not exist before it is created. Use the following command to create a profile command for the current user and return the file address:

PS C:\> New-Item -Type file -Force $profile

Generally, the created file is located at ~\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1

Alias format: function alias { command to be replaced }

Open the file and create aliases upmywiki and upkibana:

function upmywiki {d:;cd D:\mywiki\mywiki;tiddlywiki --server 23456}
function upkibana {c:;cd C:\kibana-7.8.0-windows-x86_64\kibana-7.8.0-windows-x86_64\bin;./kibana.bat}

Restart PowerShell to see the effect:

PS C:\> upmywiki

If Powershell Reports an Error#

If it prompts a permission issue, try the following steps:

  • Open Powershell as an administrator and execute the command Set-ExecutionPolicy RemoteSigned
  • Restart PowerShell, it should work now
Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.