system() 在 vi​​m 中使用 powershell [英] system() with powershell in vim

查看:29
本文介绍了system() 在 vi​​m 中使用 powershell的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 Vim 中编写一个简单的函数来返回 powershell 命令的结果.不过,我一直在结果中胡言乱语.

I'm trying to write a simple function in Vim to return the results of a powershell command. I keep getting gibberish in the results though.

我认为这可能是一个编码问题,但正如您将看到的,这个问题很奇怪,因为它有点工作".不过我也没有办法解决这个问题.

I think this may be an encoding problem, but as you'll see the problem is strange since it "sort of works". I don't have any solution to the problem though.

在 vim 中设置以下非默认 shell 选项:

With the following non-default shell options set in vim:

set shell=powershell
set shellcmdflag=-c

给定以下函数:

function! Test()
   let result = system("ls")
   call setline(1, result)
endfunction

当我运行时(从 C:\Windows):

When I run (from C:\Windows):

:call Test()

以下内容写入我的缓冲区:

The following is written to my buffer:

^@^@    Directory: C:\Windows^@^@^@    Mode            LastWriteTime   Length Name ^@-------
 ....continues

但是当我运行以下命令时:

However when I run the following command:

:r!ls

我得到了我所期望的(即 ls 的 powershell 结果)更有趣的是当我运行命令时:

I get back exactly what I would expect (i.e. the powershell results of ls) Even more interesting is when I run the command:

:echo system("ls")

结果看起来是正确的

我尝试修改我原来的函数如下:

I've tried modifying my original function as follows:

function! Test()
   let result = system("ls")
   echo result
   call setline(1, result)
endfunction

并且回显出来的值正是我所期望的 - 但我仍然看到胡言乱语

and the value echo'ed out is exactly what I would expect - yet I still see gibberish

我还尝试对我的函数进行以下修改:

I've also tried the following modification to my function:

function! Test()
   let result = system("ls")
   let conv = iconv(result, "utf-8", &enc)
   call setline(1, conv)
endfunction

但结果完全相同(即它们包括 ^@^@ 符号和其他乱码)

But the results are exactly the same (i.e. they include the ^@^@ symbols and other gibberish)

我对正在发生的事情的猜测是使用 > 重定向的 powershell cmds 产生 utf-16 输出,而 vim 无法处理这个.我从 powershell 得到以下信息(这里的文件是 gnu32 程序):

My guess as to what's happening is that powershell cmds which are redirected using > produce utf-16 output, and vim is unable to deal with this. I get the following from powershell (file in this case is the gnu32 program):

PS> ls > test
PS> file test
test; Little-endian UTF-16 Unicode text, with CRLF, CR line terminator

我也尝试过使用 $OutputEncoding,但没有成功,如下所述:http://blogs.msdn.com/b/powershell/archive/2006/12/11/outputencoding-to-the-rescue.aspx

I've also tried playing around with $OutputEncoding, without any success, as described here: http://blogs.msdn.com/b/powershell/archive/2006/12/11/outputencoding-to-the-rescue.aspx

有人知道我在这里做错了什么吗?

Anyone have any ideas what I'm doing wrong here?

推荐答案

我终于想通了.

对我的函数的以下修改几乎完全符合我的要求:

The following modification to my function does pretty much exactly what I want:

function! Test()
  let @r = system("ls")
  put! r
endfunction

我认为 Artomegus 是正确的,问题确实是回车,而不是编码问题.

I think Artomegus is correct, the problem is really the carriage return, and not an encoding problem.

顺便提一下,当我的 .vimrc 文件中只有以下内容时,我遇到了问题:

As a side note, I ran into problems when I had only the following in my .vimrc file:

set shell=powershell
set shellcmdflag=-c

当我运行 :call Test() 时,仅使用这些设置,我会收到关于 Vim 无法读取临时文件的错误.

With just these settings when I ran :call Test() I would get an error about Vim being unable to read the temp file.

解决此问题的方法是将以下内容添加到您的 .vimrc 文件中:

The fix for this problem is to add the following to your .vimrc file:

set shell=powershell
set shellcmdflag=-c
set shellquote=\"
set shellxquote= 

必须将 shellquote 设置为 \" 并将 shellxquote 设置为空格(即空),因为默认情况下,在 Windows 上,shellxquote 设置为 \",这会覆盖 shellquote 的值.这在使用系统函数时会出现问题,该函数在后台运行 vimrun.exe 程序.

You must set the shellquote to be \" and shellxquote to be a blank space (i.e. empty) because by default on windows, shellxquote is set to \" which overrides the value of shellquote. This is problematic when using the system function, which runs the vimrun.exe program behind the scenes.

希望这对其他人有所帮助.我已经坚持了很长时间,但现在 powershell 与 Vim 完美配合.

Hopefully this helps someone else. I've been stuck on this for a long time, but now powershell works perfectly for me with Vim.

这篇关于system() 在 vi​​m 中使用 powershell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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