最近实在被git命令和poetry命令搞烦了,每次都要输入好长的命令,并且都差不太多,所以就搜索了怎么配置alias,下面是我的配置过程,主要资料来自[1]。
配置
因为我用的是Windows Terminal
,主要使用的Powershell
环境,所以一下教程主要是以Powershell
为例,配置的Alias主要是关于Git
命令的,其它环境(CMD
、WSL
等)请自行百度,其它软件的命令Alias和本教程类似。
- 生成并打开
Powershell
配置文件
code $profile # 使用vs code新建并打开配置文件"D:\Documents\PowerShell\Microsoft.PowerShell_profile.ps1"
# or
notepad $profile # 使用notepad 新建并打开配置文件
这个指令会调用你本机的默认编程软件新建并打开Powershell
的配置文件,如下图
- 获取git常用aliases
基本语法参见官方文档[2],如果你自定义的alias
和官方默认的重复冲突了,可以删除默认项。
Remove-Alias gpv -Force # 强制移除gpv这个alias,必须写到配置文件里,否则打开新的powersell又会加载默认配置
最新版去这找[1:1],FileList
->aliases.ps1
。
下面是我常用的alias
,刚好没有和powershell
默认的alias
冲突。
[更新内容]
Remove-Alias gp -Force# common use
function cl {clear
}# root alias
function cdc {cd D:\Data\00Code
}
function cdb {cd D:\Data\00Code\Git\blogv2
}# git alias
function gp {git add .git commit -m $argsgit pullgit push origin main
}# picgo u
function pu {picgo u
}# hugo alias
function hn {cdbhugo new --kind post blog/$args.md
}# notepad alias
function np {notepad $args
}
后面的内容部分是关于hexo的。
{% tabs tabs, 1 %}
# common use
function cl {clear
}# root alias
function cdc {cd D:\Data\00Code
}function cdb {cd D:\Data\00Code\Git\blog
}
# notepad alias
function np {notepad $args
}
# hexo alias
function hc {hexo clean
}function hg {hexo generate
}function hs {hexo server
}function hcgs {hexo clean && hexo generate && hexo server
}function hna {hexo new page $args
}function hno {hexo new post $args
}
# python alias
function pa {poetry add $args
}function pn {poetry new $args
}function pini {poetry init
}function pins {poetry install
}function peu {poetry env use $args
}function pei {poetry env info
}function pel {poetry env list
}function per {poetry env remove $args
}
# git alias
function ga {git add .
}function gcam {git commit -a -m $args # 相当于git add .加git commit -m
}function gbr {git branch --remote $args
}function gb {git branch $args
}function gcb {git checkout -b $args
}function gcf {git config --list $args
}function gcl {git clone --recursive $args
}function gd {git diff $args
}function glgp {git log --stat --color -p $args
}function gm {git merge $args
}function ggl {git pull origin $CurrentBranch
}function ggp {git push origin $CurrentBranch
}
{% endtabs %}
{% endtabs %}
- 重启Powershell并测试配置是否生效
cdc # 转换目录到你的code文件夹就说明alias修改生效了
- 结束🎉
参考资料
PowerShell Gallery | git-aliases 0.3.5 ↩︎ ↩︎
Set-Alias (Microsoft.PowerShell.Utility) - PowerShell | Microsoft Learn ↩︎