如何使用类似第四次的交换操作来操作 PowerShell 的位置堆栈? [英] How do I manipulate PowerShell's location stack with a forth-like swap operation?

查看:35
本文介绍了如何使用类似第四次的交换操作来操作 PowerShell 的位置堆栈?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 PowerShell 中,您可以使用 pushd(Push-Location 的别名)和 popd(Pop-Location 的别名)) 将项目推入和弹出位置堆栈.

这与 bash 的 pushdpopd 功能非常相似,用于目录堆栈.

但是 bash 的另一个不错的特性是您可以单独使用 pushd 来交换堆栈中的前两个位置.因此,您可以在一个位置和另一个位置之间快速来回翻转.它还需要一个像 pushd +3 这样的参数,这样你就可以交换堆栈中几个元素的位置.

有没有办法用 PowerShell 做到这一点?

举个例子,在 bash 中:

$ pwd/bin$推~~/bin$密码/家/漏斗$推/斌~$密码/bin$推~/bin$密码/家/漏斗

解决方案

PowerShell 社区扩展 多年 :-) 例如:

<前>48 > cd C:\inetpubC:\inetpub49 > cd C:\Windows\System32C:\Windows\System3250 > cd -C:\inetpub51 > cd +C:\Windows\System3252 > cd# 目录栈:--- ----------------0 C:\用户\基思1 C:\inetpub-> 2 C:\Windows\System3253 > cd -0C:\用户\基思

虽然不是完全互换,但向后 (-) 和向前 (+) 的比喻如今已经非常成熟.您可以通过指定 - 跳转到位置历史记录中的任何位置.事实上,这是 PSCX 用户建议的一个非常方便的补充,您可以 CD 到文件文件(它只是 cds 到文件所在的目录)例如:

cd $profile

为了在 PSCX 中启用此功能,您必须在导入模块时指定 Pscx.UserPreference.ps1 文件,例如:

Import-Module PSCX -arg ~\Pscx.UserPreferences.ps1

在这种情况下,我将文件从 PSCX 目录复制到我的主目录并进行修改以适合我的口味.有关更多信息,请执行:

cd -?

man cd -full

这个嵌套模块的完整源代码是这里(在 CodePlex 上).

In PowerShell you can use pushd (an alias for Push-Location) and popd (an alias for Pop-Location) to push items onto and pop items off of a location stack.

This is very similar to bash's pushd and popd functionality for a stack of directories.

But one other nice feature of bash is that you can use pushd all by itself to swap the top two locations on the stack. So you can flip back and forth very quickly between one location and another. And it also takes an argument like pushd +3 so you can swap with a location that's a few elements down in your stack.

Is there any way to do this with PowerShell?

As an example, in bash:

$ pwd
/bin
$ pushd ~
~ /bin
$ pwd
/home/hopper
$ pushd
/bin ~
$ pwd
/bin
$ pushd
~ /bin
$ pwd
/home/hopper

解决方案

There has been similar functionality built into the PowerShell Community Extensions for years :-) e.g.:

48 >  cd C:\inetpub
C:\inetpub
49 >  cd C:\Windows\System32
C:\Windows\System32
50 >  cd -
C:\inetpub
51 >  cd +
C:\Windows\System32
52 >  cd

     # Directory Stack:
   --- ----------------
     0 C:\Users\Keith
     1 C:\inetpub
->   2 C:\Windows\System32

53 >  cd -0
C:\Users\Keith

Although it isn't quite a swap, the backward (-) and forward (+) metaphor is pretty well established these days. And you skip to any location in the location history by specifying -<num>. In fact, and this was a very handy addition suggested by a PSCX user, you can CD to a file file (which just cds to the dir the file is in) e.g.:

cd $profile

In order to enable this functionality in PSCX you have to specify the Pscx.UserPreference.ps1 file when you import the module e.g.:

Import-Module PSCX -arg ~\Pscx.UserPreferences.ps1

In this case, I copy the file from the PSCX dir to my home dir and modify to suit my tastes. For more info execute:

cd -?

or

man cd -full

The full source for this nested module is here (on CodePlex).

这篇关于如何使用类似第四次的交换操作来操作 PowerShell 的位置堆栈?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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