programing

Powershell을 사용한 Vim

projobs 2021. 1. 16. 09:10
반응형

Powershell을 사용한 Vim


Windows에서 gvim을 사용하고 있습니다.

내 _vimrc에 다음을 추가했습니다.

set shell=powershell.exe
set shellcmdflag=-c
set shellpipe=>
set shellredir=>

function! Test()
  echo system("dir -name")
endfunction

command! -nargs=0 Test :call Test()

이 함수 (: Test)를 실행하면 말도 안되는 문자 (숫자 / 문자 ASCII 문자가 아님)가 표시됩니다.

cmd를 쉘로 사용하면 (-name없이) 작동하므로 powershell에서 vim으로 출력을 얻는 데 문제가있는 것 같습니다.

흥미롭게도 이것은 훌륭하게 작동합니다.

:!dir -name

이렇게 :

:r !dir -name

업데이트 : David가 언급 한 행동 확인

_vimrc에서 위에서 언급 한 set 명령을 실행하면 : Test는 넌센스를 출력합니다. 그러나 _vimrc 대신 vim에서 직접 실행하면 : Test가 예상대로 작동합니다.

또한 인코딩 문제인 경우 iconv를 사용해 보았습니다.

:echo iconv( system("dir -name"), "unicode", &enc )

그러나 이것은 어떤 차이도 만들지 않았습니다. 그래도 잘못된 인코딩 유형을 사용할 수 있습니다.

누구든지 이것을 작동시키는 방법을 알고 있습니까?


약간의 해킹이지만 다음은 Vim 7.2에서 작동합니다. CMD 세션 내에서 Powershell을 실행하고 있습니다.

if has("win32")
    set shell=cmd.exe
    set shellcmdflag=/c\ powershell.exe\ -NoLogo\ -NoProfile\ -NonInteractive\ -ExecutionPolicy\ RemoteSigned
    set shellpipe=|
    set shellredir=>
endif

function! Test()
  echo system("dir -name")
endfunction

다음으로 테스트했습니다 ...

  • :!dir -name
  • :call Test()

나는 여기에서 많은 사람들이 설명한 비슷한 문제에 직면했습니다.

특히, 전화

:set shell=powershell

vim 내에서 수동으로 powershell이 ​​제대로 작동하지만 다음을 추가하자마자

set shell=powershell

내 vimrc 파일에 "Unable to open temp file ...."오류가 표시됩니다.

문제는 기본적으로 쉘이 수정 될 때 vim이 자동으로 shellxquote를 "로 설정한다는 것입니다. 즉, 쉘 명령이 다음과 같이 보일 것입니다.

 powershell -c "cmd > tmpfile"

vim이 임시 파일을 읽으려면이 명령이 다음과 같아야합니다.

 powershell -c "cmd" > tmpfile

내 vimrc 파일에서 shellquote를 "로 설정하고 shellxquote를 설정 해제 (즉, 공백으로 설정)하면 내 모든 문제가 해결되는 것 같습니다.

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

I've also tried taking this further and scripting vim a bit using the system() call: system() with powershell in vim


I suspect that the problem is that Powershell uses the native String encoding for .NET, which is UTF-16 plus a byte-order-mark.

When it's piping objects between commands it's not a problem. It's a total PITA for external programs though.

You can pipe the output through out-file, which does support changing the encoding, but still formats the output for the terminal that it's in by default (arrgh!), so things like "Get-Process" will truncate with ellipses, etc. You can specify the width of the virtual terminal that Out-File uses though.

Not sure how useful this information is, but it does illuminate the problem a bit more.


Try replacing

"dir \*vim\*"

with

 " -command { dir \*vim\* }"

EDIT: Try using cmd.exe as the shell and put "powershell.exe" before "-command"


Interesting question - here is something else to add to the confusion. Without making any changes to my .vimrc file, if I then run the following commands in gvim:

:set shell=powershell.exe
:set shellcmdflag=-noprofile
:echo system("dir -name")

It behaves as expected!

If I make the same changes to my .vimrc file, though (the shell and shellcmdflag options), running :echo system("dir -name") returns the nonsense characters!


The initial example code works fine for me when I plop it in vimrc.

So now I'm trying to figure out what in my vimrc is making it function. Possibly:

set encoding=utf8

Edit: Yep, that appears to do it. You probably want to have VIM defaulting to unicode anyway, these days...


None of the answers on this page were working for me until I found this hint from https://github.com/dougireton/mirror_pond/blob/master/vimrc - set shellxquote= [space character] was the missing piece.

if has("win32") || has("gui_win32") 
     if executable("PowerShell") 
        " Set PowerShell as the shell for running external ! commands 
        " http://stackoverflow.com/questions/7605917/system-with-powershell-in-vim      
        set shell=PowerShell 
        set shellcmdflag=-ExecutionPolicy\ RemoteSigned\ -Command 
        set shellquote=\" 
        " shellxquote must be a literal space character. 
        set shellxquote=  
   endif 
endif 

I propose an hackish solution. It doesn't really solve the problem, but it get the job done somehow.

This Vim plugin automate the creation of a temporary script file, powershell call through cmd.exe and paste of the result. It's not as nice as a proper powershell handling by vim, but it works.


Try instead set shellcmdflag=\ -c


Explanation:

Vim uses tempname() to generate a temp file path that system() reads.

If &shell contains 'sh' and &shellcmdflag starts with '-' then tempname() generates a temp file path with forward slashes.

Thus, if set shell=powershell set shellcmdflag=-c then Vim will try to read a temp file with forward slashes that cannot be found.

A remedy is to set instead set shellcmdflag=\ -c that is, add a whitespace to &shellcmdflag so that the first character is no longer '-' and tempname() produces a temp file path with backward slashes that can be found by system().


I remarked on the vim_dev mailing list ( https://groups.google.com/forum/#!topic/vim_dev/vTR05EZyfE0 ) that this deserves better documentation.


actf answer works for me, but because of Powershell built in DIFF (which is different from the Linux one) you must add this line to your Powershell profile to have diff working again in VIM:

Remove-Item Alias:diff -force

I don't use VIM but Powershell's default output is Unicode. Notepad can read unicode, you could use it to see if you are getting the output you expect.

ReferenceURL : https://stackoverflow.com/questions/94382/vim-with-powershell

반응형