简体   繁体   English

如何创建在与shell相同的进程中运行的Powershell别名(或函数或cmdlet)?

[英]How do I create a Powershell alias (or function, or cmdlet) that runs in the same process as my shell?

In Unix, I create a lot of aliases/functions. 在Unix中,我创建了很多别名/函数。 Here is my workflow: 这是我的工作流程:

* I have a file named 'aliases.txt' in my $HOME directory
* I have a quick command to edit that file
* I have an alias named 'sa' that sources that alias file. I.e.

  alias sa='. $HOME/aliases.txt

So I can quickly and easily create, modify, and use new aliases. 因此,我可以快速轻松地创建,修改和使用新别名。

I'm trying to re-create the same work flow in Powershell and it doesn't work. 我试图在Powershell中重新创建相同的工作流程,但它不起作用。 I've got the aliases.ps1 file in my $HOME directory. 我的$ HOME目录中有aliases.ps1文件。 I tried creating an 'sa' alias, but of course a Powershell alias can't contain an argument. 我尝试创建一个'sa'别名,但当然Powershell别名不能包含参数。 So I've tried 所以我试过了

function sa {. $HOME\aliases.ps1 }

I can run it but changes in my aliases.pl1 file don't get reflected in my shell session. 我可以运行它,但我的aliases.pl1文件中的更改不会反映在我的shell会话中。 I'm assuming it's because Windows runs the 'sa' function in a new process, while Unix runs functions and aliases in the same process. 我假设它是因为Windows在新进程中运行'sa'函数,而Unix在同一进程中运行函数和别名。 How can I get my 'sa' back? 我怎样才能让我的'sa'回来?

(BTW, "Type '. $HOME\\aliases.ps1' at the command line each time" is not the answer I'm looking for. (BTW,“每次在命令行输入'。$ HOME \\ aliases.ps1'”不是我正在寻找的答案。

Thanks in advance. 提前致谢。

Would

. sa

be terse enough? 足够简洁? You just need to dot-source the function call into the current scope. 您只需要将函数调用点源到当前范围。 (Note there needs to be a space between the dot and the function name). (注意点和函数名之间需要有一个空格)。

What are you trying to do? 你想做什么? Some parts of your question doesn't make sense. 你问题的某些部分没有意义。

Ok, I understand better now. 好的,我现在明白了。

Import-Alias is exactly for this purpose! Import-Alias就是为了这个目的! Edit the file and do Import-Alias file and you get the new aliases that you have added. 编辑文件并执行Import-Alias file ,您将获得已添加的新别名。


Your flow: 你的流程:

Use the Set-Alias command to setup an alias and add that to your profile or just add that function to profile. 使用Set-Alias命令设置别名并将其添加到配置文件或只是将该功能添加到配置文件。

notepad $profile
(add the next in notepad)
function sa {. HOME\aliases.ps1 }

Now sa, should be available and the aliases.ps1 can be setup to use Set-Alias and add the aliases. 现在sa应该可用,并且可以设置aliases.ps1以使用Set-Alias并添加别名。

You are trying to map what you do in unix EXACTLY to Powershell. 您正试图将您在unix中完成的操作映射到Powershell。 You should try and use what powershell has to offer: 您应该尝试使用powershell提供的功能:

Powershell flow: Powershell流程:

 notepad $profile
 (in notepad)
 Set-Alias <alias> <command>

Just add Set-Alias command to your profile. 只需将Set-Alias命令添加到您的配置文件即可。

Or use Export-Alias to export into a file and add Import-Alias to import from the file in your profile. 或者使用Export-Alias导出到文件中,并添加Import-Alias以从配置文件中的文件导入。

All these are excellently explained here: http://technet.microsoft.com/en-us/library/ee692685.aspx 所有这些都在这里得到了很好的解释: http//technet.microsoft.com/en-us/library/ee692685.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM