如何在oh-my-zsh中创建永久别名?


43

在我的.zshrc中,我尝试使用一些别名。我调查了很多地方,但找不到可行的方法。我在下面使用了这段代码:

# Set personal aliases, overriding those provided by oh-my-zsh libs, 
# plugins, and themes. Aliases can be placed here, though oh-my-zsh 
# users are encouraged to define aliases within the ZSH_CUSTOM folder. 
# For a full list of active aliases, run alias. # # Example aliases
alias zshconfig="mate ~/.zshrc"
alias ohmyzsh="mate ~/.oh-my-zsh"
alias n= "nano"  
alias m= "mkdir"
alias w= "cd ~/Documents/UoMWorkspace/Semester2"  
alias j= "cd ~/Documents/UoMWorkspace/Semester2/COMP17412"

然后我写了一条命令source ~/.zshrc。仍然无法解决问题。我收到类似的错误消息zsh: command not found: j

谁能为我提供任何建议,让我知道我做错了什么?


3
从(多个)注释和错误来看,我想每个别名实际上都设置在一行上,对吗?
kos

Answers:


67

=别名和别名定义之间不能有空格。

alias zshconfig="mate ~/.zshrc"
alias ohmyzsh="mate ~/.oh-my-zsh"
alias n="nano"
alias m="mkdir"
alias w="cd ~/Documents/UoMWorkspace/Semester2"
alias j="cd ~/Documents/UoMWorkspace/Semester2/COMP17412"

顺便说一句:如果您正在寻找一种缩短目录名称的方法,我建议您研究命名目录和该AUTO_CD选项而不是别名:

hash -d w=~/Documents/UoMWorkspace/Semester2
hash -d j=~/Documents/UoMWorkspace/Semester2/COMP17412

这使您可以使用~w代替~/Documents/UoMWorkspace/Semester2~j代替~/Documents/UoMWorkspace/Semester2/COMP17412(或~w/COMP17412)。因此cd ~j与相同cd ~/Documents/UoMWorkspace/Semester2。它还可以用作路径的一部分,例如cat ~j/somedir/somefile

setopt AUTO_CD

zshcd如果在命令行中作为命令给出,并且目录不是实际命令的名称,它将自动进入目录。例如

% /usr
% pwd
/usr
% ~w
/home/YOURUSERNAME/Documents/UoMWorkspace/Semester2

作为进一步的解释,为什么不在等号sign之前和之后使用空格:例如a = b:这将被视为a具有两个参数(b=)的命令,并且会引发错误:zsh: command not found: a。如果放在alias那个(alias a = b)之前,它将有些相似。
Timo

我只是使用“ vi〜/ .zshrc”并编辑了文件,export PATH=/Users/andres/flutter/bin:$PATH原因是我使用了颤动
Andres Paladines
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.