# HG changeset patch # User Bram Moolenaar # Date 1625079605 -7200 # Node ID beff72446e2e8f90cd16b0652fb8463df2d9b744 # Parent 05c4686c56d26e0da3deb203fc3ba69081719a07 patch 8.2.3079: Powershell core not supported by default Commit: https://github.com/vim/vim/commit/a3d1b29bd36487167c98b3cefa30f06c529e412d Author: Mike Williams Date: Wed Jun 30 20:56:00 2021 +0200 patch 8.2.3079: Powershell core not supported by default Problem: Powershell core not supported by default. Solution: Set option defaults for "pwsh". (Mike Williams, closes https://github.com/vim/vim/issues/8481) diff --git a/runtime/doc/eval.txt b/runtime/doc/eval.txt --- a/runtime/doc/eval.txt +++ b/runtime/doc/eval.txt @@ -9878,11 +9878,13 @@ sha256({string}) *sha256()* shellescape({string} [, {special}]) *shellescape()* Escape {string} for use as a shell command argument. - On MS-Windows, when the 'shell' contains powershell then it - will enclose {string} in single quotes and will double up all - internal single quotes. With other values for 'shell' when - 'shellslash' is not set, it will enclose {string} in double - quotes and double all double quotes within {string}. + When the 'shell' contains powershell (MS-Windows) or pwsh + (MS-Windows, Linux, and MacOS) then it will enclose {string} + in single quotes and will double up all internal single + quotes. + On MS-Windows, when 'shellslash' is not set, it will enclose + {string} in double quotes and double all double quotes within + {string}. Otherwise it will enclose {string} in single quotes and replace all "'" with "'\''". @@ -11042,8 +11044,8 @@ tempname() *tempname()* *temp-file-n :exe "redir > " . tmpfile < For Unix, the file will be in a private directory |tempfile|. For MS-Windows forward slashes are used when the 'shellslash' - option is set or when 'shellcmdflag' starts with '-' except - when when 'shell' contains powershell. + option is set, or when 'shellcmdflag' starts with '-' and + 'shell' does not contain powershell or pwsh. term_ functions are documented here: |terminal-function-details| diff --git a/runtime/doc/options.txt b/runtime/doc/options.txt --- a/runtime/doc/options.txt +++ b/runtime/doc/options.txt @@ -6633,6 +6633,8 @@ A jump table for the options with a shor "bash", "fish", "ash" or "dash" the default becomes "2>&1| tee". This means that stderr is also included. Before using the 'shell' option a path is removed, thus "/bin/sh" uses "sh". + For Unix and MS-Windows, when the 'shell' option is "pwsh" the default + becomes ">%s 2>&1" and the output is not echoed to the screen. The initialization of this option is done after reading the ".vimrc" and the other initializations, so that when the 'shell' option is set there, the 'shellpipe' option changes automatically, unless it was @@ -6648,9 +6650,7 @@ A jump table for the options with a shor security reasons. *'shellquote'* *'shq'* -'shellquote' 'shq' string (default: ""; Win32, when 'shell' does not - contain powershell but contains "sh" - somewhere: "\"") +'shellquote' 'shq' string (default: "") global Quoting character(s), put around the command passed to the shell, for the "!" and ":!" commands. The redirection is kept outside of the @@ -6658,9 +6658,7 @@ A jump table for the options with a shor probably not useful to set both options. This is an empty string by default. Only known to be useful for third-party shells on MS-Windows-like systems, such as the MKS Korn - Shell or bash, where it should be "\"". The default is adjusted - according the value of 'shell', to reduce the need to set this option - by the user. See |dos-shell|. + Shell or bash, where it should be "\"". See |dos-shell|. This option cannot be set from a |modeline| or in the |sandbox|, for security reasons. @@ -6677,12 +6675,12 @@ A jump table for the options with a shor The default is ">". For Unix, if the 'shell' option is "csh" or "tcsh" during initializations, the default becomes ">&". If the 'shell' option is "sh", "ksh", "mksh", "pdksh", "zsh", "zsh-beta", - "bash" or "fish", the default becomes ">%s 2>&1". This means that - stderr is also included. For Win32, the Unix checks are done and + "bash", "fish", or "pwsh", the default becomes ">%s 2>&1". This means + that stderr is also included. For Win32, the Unix checks are done and additionally "cmd" is checked for, which makes the default ">%s 2>&1", and "powershell" is checked for which makes the default - "2>&1 | Out-File -Encoding default". Also, the same names with ".exe" - appended are checked for. + "2>&1 | Out-File -Encoding default" (see |dos-powershell|). Also, the + same names with ".exe" appended are checked for. The initialization of this option is done after reading the ".vimrc" and the other initializations, so that when the 'shell' option is set there, the 'shellredir' option changes automatically unless it was @@ -6697,7 +6695,7 @@ A jump table for the options with a shor global {only for MS-Windows} When set, a forward slash is used when expanding file names. This is - useful when a Unix-like shell is used instead of cmd.exe or + useful when a Unix-like shell is used instead of cmd.exe, pwsh.exe, or powershell.exe. Backward slashes can still be typed, but they are changed to forward slashes by Vim. Note that setting or resetting this option has no effect for some @@ -6767,10 +6765,10 @@ A jump table for the options with a shor then ')"' is appended. When the value is '(' then also see 'shellxescape'. This is an empty string by default on most systems, but is known to be - useful for on Win32 version, either for cmd.exe and powershell.exe - which automatically strips off the first and last quote on a command, - or 3rd-party shells such as the MKS Korn Shell or bash, where it - should be "\"". The default is adjusted according the value of + useful for on Win32 version, either for cmd.exe, powershell.exe, or + pwsh.exe which automatically strips off the first and last quote on a + command, or 3rd-party shells such as the MKS Korn Shell or bash, where + it should be "\"". The default is adjusted according the value of 'shell', to reduce the need to set this option by the user. See |dos-shell|. This option cannot be set from a |modeline| or in the |sandbox|, for diff --git a/runtime/doc/os_dos.txt b/runtime/doc/os_dos.txt --- a/runtime/doc/os_dos.txt +++ b/runtime/doc/os_dos.txt @@ -304,40 +304,80 @@ Vim sets the 'shellcmdflag' and 'shellqu set as described above. ============================================================================== -10. PowerShell *dos-powershell* +10. PowerShell *dos-powershell* *dos-pwsh* -Vim also supports Windows PowerShell. If 'shell' has been set to -"powershell.exe" at startup then VIM sets 'shellcmdflag', 'shellxquote', -'shellpipe', and 'shellredir' options to the following values: +Vim supports PowerShell Desktop and PowerShell Core. PowerShell Desktop is +the version of PowerShell that is installed with Windows, while PowerShell +Core is a separate downloadable version that works cross-platform. To see +which version you are using then enter the following in a PowerShell prompt - +$PSVersionTable.PSEdition + +If 'shell' includes "powershell" in the filename at startup then VIM sets +'shellcmdflag', 'shellxquote', 'shellpipe', and 'shellredir' options to the +following values: 'shellcmdflag' -Command 'shellxquote' " 'shellpipe' 2>&1 | Out-File -Encoding default 'shellredir' 2>&1 | Out-File -Encoding default +If 'shell' includes "pwsh" in the filename at startup then VIM sets +'shellcmdflag', 'shellxquote', 'shellpipe', and 'shellredir' options to the +following values: + +'shellcmdflag' -c +'shellxquote' " +'shellpipe' >%s 2>&1 +'shellredir' >%s 2>&1 + If you find that PowerShell commands are taking a long time to run then try -setting 'shellcmdflag' to "-NoProfile -Command". Note this will prevent any -PowerShell environment setup by the profile from taking place. +with "-NoProfile" at the beginning of the 'shellcmdflag'. Note this will +prevent any PowerShell environment setup by the profile from taking place. If you have problems running PowerShell scripts through the 'shell' then try -setting 'shellcmdflag' to "-ExecutionPolicy RemoteSigned -Command". See -online Windows documentation for more information on PowerShell Execution -Policy settings. +with "-ExecutionPolicy RemoteSigned -Command" at the beginning of +'shellcmdflag'. See online Windows documentation for more information on +PowerShell Execution Policy settings. + +See |option-backslash| about including spaces in 'shellcmdflag' when using +multiple flags. The 'shellpipe' and 'shellredir' option values re-encode the UTF-16le output -from Windows PowerShell to your currently configured console codepage. The +from PowerShell Desktop to your currently configured console codepage. The output can be forced into a different encoding by changing "default" to one of the following: unicode - UTF-16le (default output from PowerShell 5.1) bigendianunicode - UTF-16 utf8 - UTF-8 - utf7 - UTF-7 (no-BOM) + utf7 - UTF-7 (no BOM) utf32 - UTF-32 ascii - 7-bit ASCII character set default - System's active code page (typically ANSI) oem - System's current OEM code page -Note Multi-byte Unicode encodings include a leading BOM. +Note The abovce multi-byte Unicode encodings include a leading BOM unless +otherwise indicated. + +By default PowerShell Core's output is UTF-8 encoded without a BOM. If you +want to force the output of PowerShell Core into a different encoding then set +'shellredir' and 'shellpipe' to "2>&1 | Out-File -Encoding encoding" where +encoding is one of the following: + + ascii - 7-bit ASCII character set + bigendianunicode - UTF-16be + bigendianutf32 - UTF-32be + oem - System's current OEM code page + unicode - UTF-16le + utf7 - UTF-7 + utf8 - UTF-8 + utf8BOM - UTF-8, with BOM + utf8NoBOM - UTF-8, no BOM (default output from PowerShell Core) + utf32 - UTF-32 + +Since PowerShell Core 6.2, the Encoding parameter also supports specifying a +numeric ID of a registered code page (-Encoding 1251) or string names of +registered code pages (-Encoding "windows-1251"). The .NET documentation for +Encoding.CodePage has more information vim:tw=78:ts=8:noet:ft=help:norl: diff --git a/runtime/doc/os_win32.txt b/runtime/doc/os_win32.txt --- a/runtime/doc/os_win32.txt +++ b/runtime/doc/os_win32.txt @@ -30,6 +30,7 @@ File formats |dos-file-formats| Interrupting |dos-CTRL-Break| Temp files |dos-temp-files| Shell option default |dos-shell| +PowerShell defaults |dos-powershell| Win32 GUI |gui-w32| diff --git a/src/fileio.c b/src/fileio.c --- a/src/fileio.c +++ b/src/fileio.c @@ -5222,6 +5222,7 @@ vim_tempname( WCHAR *chartab = L"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; char_u *retval; char_u *p; + char_u *shname; long i; wcscpy(itmp, L""); @@ -5247,8 +5248,10 @@ vim_tempname( // "sh". NOTE: This also checks 'shellcmdflag' to help those people who // didn't set 'shellslash' but only if not using PowerShell. retval = utf16_to_enc(itmp, NULL); - if ((strstr((char *)gettail(p_sh), "powershell") == NULL - && *p_shcf == '-') || p_ssl) + shname = gettail(p_sh); + if ((*p_shcf == '-' && !(strstr((char *)shname, "powershell") != NULL + || strstr((char *)shname, "pwsh") != NULL )) + || p_ssl) for (p = retval; *p; ++p) if (*p == '\\') *p = '/'; diff --git a/src/misc2.c b/src/misc2.c --- a/src/misc2.c +++ b/src/misc2.c @@ -1414,8 +1414,9 @@ vim_strsave_shellescape(char_u *string, char_u *escaped_string; int l; int csh_like; + char_u *shname; + int powershell; # ifdef MSWIN - int powershell; int double_quotes; # endif @@ -1425,9 +1426,12 @@ vim_strsave_shellescape(char_u *string, // Csh also needs to have "\n" escaped twice when do_special is set. csh_like = csh_like_shell(); + // PowerShell uses it's own version for quoting single quotes + shname = gettail(p_sh); + powershell = strstr((char *)shname, "pwsh") != NULL; # ifdef MSWIN - // PowerShell only accepts single quotes so override p_ssl. - powershell = strstr((char *)gettail(p_sh), "powershell") != NULL; + powershell = powershell || strstr((char *)shname, "powershell") != NULL; + // PowerShell only accepts single quotes so override shellslash. double_quotes = !powershell && !p_ssl; # endif @@ -1445,11 +1449,9 @@ vim_strsave_shellescape(char_u *string, # endif if (*p == '\'') { -# ifdef MSWIN if (powershell) length +=2; // ' => '' else -# endif length += 3; // ' => '\'' } if ((*p == '\n' && (csh_like || do_newline)) @@ -1497,14 +1499,12 @@ vim_strsave_shellescape(char_u *string, # endif if (*p == '\'') { -# ifdef MSWIN if (powershell) { *d++ = '\''; *d++ = '\''; } else -# endif { *d++ = '\''; *d++ = '\\'; diff --git a/src/option.c b/src/option.c --- a/src/option.c +++ b/src/option.c @@ -933,8 +933,8 @@ set_init_3(void) } } # ifdef MSWIN - // PowerShell 5.1/.NET outputs UTF-16 with BOM so re-encode to the - // current codepage + // Windows PowerShell output is UTF-16 with BOM so re-encode to the + // current codepage. else if ( fnamecmp(p, "powershell") == 0 || fnamecmp(p, "powershell.exe") == 0 ) @@ -965,6 +965,7 @@ set_init_3(void) || fnamecmp(p, "fish") == 0 || fnamecmp(p, "ash") == 0 || fnamecmp(p, "dash") == 0 + || fnamecmp(p, "pwsh") == 0 # ifdef MSWIN || fnamecmp(p, "cmd") == 0 || fnamecmp(p, "sh.exe") == 0 @@ -976,6 +977,7 @@ set_init_3(void) || fnamecmp(p, "bash.exe") == 0 || fnamecmp(p, "cmd.exe") == 0 || fnamecmp(p, "dash.exe") == 0 + || fnamecmp(p, "pwsh.exe") == 0 # endif ) { @@ -985,7 +987,10 @@ set_init_3(void) # ifdef MSWIN p_sp = (char_u *)">%s 2>&1"; # else - p_sp = (char_u *)"2>&1| tee"; + if (fnamecmp(p, "pwsh") == 0) + p_sp = (char_u *)">%s 2>&1"; + else + p_sp = (char_u *)"2>&1| tee"; # endif options[idx_sp].def_val[VI_DEFAULT] = p_sp; } @@ -1011,6 +1016,7 @@ set_init_3(void) * p_shcf p_sxq * cmd.exe - "/c" "(" * powershell.exe - "-Command" "\"" + * pwsh.exe - "-c" "\"" * "sh" like shells - "-c" "\"" * * For Win32 p_sxq is set instead of p_shq to include shell redirection. diff --git a/src/os_win32.c b/src/os_win32.c --- a/src/os_win32.c +++ b/src/os_win32.c @@ -2135,6 +2135,7 @@ executable_exists(char *name, char_u **p char_u *pathbuf = NULL; char_u *pathext = NULL; char_u *pathextbuf = NULL; + char_u *shname = NULL; int noext = FALSE; int retval = FALSE; @@ -2142,8 +2143,10 @@ executable_exists(char *name, char_u **p return FALSE; // Using the name directly when a Unix-shell like 'shell'. - if (strstr((char *)gettail(p_sh), "powershell") == NULL - && strstr((char *)gettail(p_sh), "sh") != NULL) + shname = gettail(p_sh); + if (strstr((char *)shname, "sh") != NULL && + !(strstr((char *)shname, "powershell") != NULL + || strstr((char *)shname, "pwsh") != NULL)) noext = TRUE; if (use_pathext) diff --git a/src/testdir/test_shell.vim b/src/testdir/test_shell.vim --- a/src/testdir/test_shell.vim +++ b/src/testdir/test_shell.vim @@ -19,7 +19,8 @@ func Test_shell_options() \ ['ash', '-c', '2>&1| tee', '', '>%s 2>&1', '', ''], \ ['dash', '-c', '2>&1| tee', '', '>%s 2>&1', '', ''], \ ['csh', '-c', '|& tee', '', '>&', '', ''], - \ ['tcsh', '-c', '|& tee', '', '>&', '', '']] + \ ['tcsh', '-c', '|& tee', '', '>&', '', ''], + \ ['pwsh', '-c', '>%s 2>&1', '', '>%s 2>&1', '', '']] endif if has('win32') let shells += [['cmd', '/c', '>%s 2>&1', '', '>%s 2>&1', '"&|<>()@^', ''], @@ -28,6 +29,8 @@ func Test_shell_options() \ '', '2>&1 | Out-File -Encoding default', '"&|<>()@^', '"'], \ ['powershell', '-Command', '2>&1 | Out-File -Encoding default', '', \ '2>&1 | Out-File -Encoding default', '"&|<>()@^', '"'], + \ ['pwsh.exe', '-c', '>%s 2>&1', '', '>%s 2>&1', '"&|<>()@^', '"'], + \ ['pwsh', '-c', '>%s 2>&1', '', '>%s 2>&1', '"&|<>()@^', '"'], \ ['sh.exe', '-c', '>%s 2>&1', '', '>%s 2>&1', '"&|<>()@^', '"'], \ ['ksh.exe', '-c', '>%s 2>&1', '', '>%s 2>&1', '"&|<>()@^', '"'], \ ['mksh.exe', '-c', '>%s 2>&1', '', '>%s 2>&1', '"&|<>()@^', '"'], @@ -61,6 +64,7 @@ func Test_shell_options() let str1 = "'cmd \"arg1\" '\\''arg2'\\'' \\!%#'" let str2 = "'cmd \"arg1\" '\\''arg2'\\'' \\\\!\\%\\#'" elseif e[0] =~# '.*powershell$' || e[0] =~# '.*powershell.exe$' + \ || e[0] =~# '.*pwsh$' || e[0] =~# '.*pwsh.exe$' let str1 = "'cmd \"arg1\" ''arg2'' !%#'" let str2 = "'cmd \"arg1\" ''arg2'' \\!\\%\\#'" else @@ -76,9 +80,14 @@ func Test_shell_options() let [&shellcmdflag, &shellpipe, &shellquote, &shellredir, \ &shellxescape, &shellxquote] = e[1:6] new - r !echo hello - call assert_equal('hello', substitute(getline(2), '\W', '', 'g'), e[0]) - bwipe! + try + r !echo hello + call assert_equal('hello', substitute(getline(2), '\W', '', 'g'), e[0]) + catch + call assert_report('Failed to run shell command, shell: ' .. e[0]) + finally + bwipe! + endtry endif endfor set shell& shellcmdflag& shellpipe& shellquote& @@ -149,6 +158,8 @@ func Test_shellslash() " ".*\\\\[^\\\\]*$" let shells = [['cmd', '/c', '\\', '/'], \ ['powershell', '-Command', '\\', '/'], + \ ['pwsh', '-Command', '\\', '/'], + \ ['pwsh', '-c', '\\', '/'], \ ['sh', '-c', '/', '/']] for e in shells exe 'set shell=' .. e[0] .. ' | set shellcmdflag=' .. e[1] diff --git a/src/version.c b/src/version.c --- a/src/version.c +++ b/src/version.c @@ -756,6 +756,8 @@ static char *(features[]) = static int included_patches[] = { /* Add new patch number below this line */ /**/ + 3079, +/**/ 3078, /**/ 3077,