如何在不資源化 .bashrc 之類檔案的情況下建立永久別名?

如何在不資源化 .bashrc 之類檔案的情況下建立永久別名?

我真的很喜歡 bash 別名,但每次我想添加新別名時,我都要輸入兩個命令,這讓我很煩惱:

echo "alias \"short-cmd\"='long-command'" >> ~/.bash_aliases
source ~/.bash_aliases

有什麼方法可以使用單一命令永久建立別名嗎?

答案1

這是我的解決方法,該函數palias(又名永久別名):

function palias ()  {

    if [ $# -ne "2" ] ; then
        error "Usage: palias short-alias \'long-alias\'"
        return
    fi

    alias $1="$2"
    alias $1 >> ~/.bash_aliases
}

答案2

我的“兩次擊鍵解決方案”(一個字母和返回鍵)是設定此別名:

alias a='. ~/.bash_aliases'

然後每當我更新 .bash_aliases 檔案時我只需輸入areturn

更進一步的是使用 github 跨機器維護它們:

bup () { [ $# = 1 ] && { cp -v ~/$1 ~/Dropnot/setups; cd ~/Dropnot/setups; git fetch; git merge origin/master; git add $1; git commit -m"$1 update"; git push origin master; cp -v $1 ~; cd -; } || echo "Error - no filename passed!";}

用法:bup [file] # file must be in ~, is usually a dot file]

答案3

當您登入 shell 時,會執行多個文件,包括 ~/.bash_profile 以及可能的 ~/.bashrc 和 ~/.bash_aliases(如果 ~/.bash_profile 文件中引用了這些文件)。要在每次登入時都有可用的別名,只需將建立別名的命令放入這些檔案之一即可。

http://shreevatsa.wordpress.com/2008/03/30/zshbash-startup-files-loading-order-bashrc-zshrc-etc/

相關內容