powershell中给命令定义一个别名该怎么做?

查看:141
本文介绍了powershell中给命令定义一个别名该怎么做?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

在linux中别名可以如下:

alias tnpm="npm --registry=https://registry.npm.taobao.org"

那么在powershell中该怎么定义?

解决方案

Set-Alias 但是这个方法应该不支持语句,只能给 function 或者 program 设置 alias。

以下这样(给 program 设置 alias)

Set-Alias emacs 'gvim.exe'

或者(给 function 设置 alias)

Set-Alias ll Get-ChildItem

是合法的

但是如果是语句,比如:

Set-Alias psconfig 'code $PROFILE'

就会报错。这种时候的方法是新建 function,然后设置 alias:

function Edit-PowershellConfig {
    code $PROFILE
}
Set-Alias psconfig Edit-PowershellConfig

值得注意的是 powershell 的 verb(也就是 function 的第一个单词,上面那个就是 Edit)是有限制的,所有可用的 verb 可以通过 Get-Verb 这个方法得到。

这个时候你应该这么写:

function Start-npmWithTaobao {
    $argList = $args -Join ' '
    Start-Process -FilePath 'npm' -ArgumentList "--registry=https://registry.npm.taobao.org $argList"  -NoNewWindow 
}
Set-Alias tnpm Start-npmWithTaobao 

这篇关于powershell中给命令定义一个别名该怎么做?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆