runtime(filetype): make shell filetype detection more robust Commit: https://github.com/vim/vim/commit/5c84d12df104e976e8f56c43f9f975e54d9fd779 Author: Aliaksei Budavei <0x000...@gmail.com> Date: Thu Apr 10 21:52:14 2025 +0200
runtime(filetype): make shell filetype detection more robust closes: https://github.com/vim/vim/issues/17063 Signed-off-by: Aliaksei Budavei <0x000...@gmail.com> Signed-off-by: Christian Brabandt <c...@256bit.org> diff --git a/runtime/autoload/dist/ft.vim b/runtime/autoload/dist/ft.vim index a74030522..4053e51f0 100644 --- a/runtime/autoload/dist/ft.vim +++ b/runtime/autoload/dist/ft.vim @@ -3,7 +3,7 @@ vim9script # Vim functions for file type detection # # Maintainer: The Vim Project <https://github.com/vim/vim> -# Last Change: 2025 Jan 25 +# Last Change: 2025 Apr 10 # Former Maintainer: Bram Moolenaar <b...@vim.org> # These functions are moved here from runtime/filetype.vim to make startup @@ -875,16 +875,16 @@ export def SetFileTypeSH(name: string, setft = true): string if setft && expand("<amatch>") =~ g:ft_ignore_pat return '' endif - if name =~ '\<csh\>' + if name =~ '^csh$' || name =~ '^#!.\{-2,}\<csh\>' # Some .sh scripts contain #!/bin/csh. return SetFileTypeShell("csh", setft) - elseif name =~ '\<tcsh\>' + elseif name =~ '^tcsh$' || name =~ '^#!.\{-2,}\<tcsh\>' # Some .sh scripts contain #!/bin/tcsh. return SetFileTypeShell("tcsh", setft) - elseif name =~ '\<zsh\>' + elseif name =~ '^zsh$' || name =~ '^#!.\{-2,}\<zsh\>' # Some .sh scripts contain #!/bin/zsh. return SetFileTypeShell("zsh", setft) - elseif name =~ '\<ksh\>' + elseif name =~ '^ksh$' || name =~ '^#!.\{-2,}\<ksh\>' b:is_kornshell = 1 if exists("b:is_bash") unlet b:is_bash @@ -892,7 +892,8 @@ export def SetFileTypeSH(name: string, setft = true): string if exists("b:is_sh") unlet b:is_sh endif - elseif exists("g:bash_is_sh") || name =~ '\<bash\>' || name =~ '\<bash2\>' + elseif exists("g:bash_is_sh") || name =~ '^bash2\=$' || + \ name =~ '^#!.\{-2,}\<bash2\=\>' b:is_bash = 1 if exists("b:is_kornshell") unlet b:is_kornshell @@ -900,7 +901,7 @@ export def SetFileTypeSH(name: string, setft = true): string if exists("b:is_sh") unlet b:is_sh endif - elseif name =~ '\<sh\>' || name =~ '\<dash\>' + elseif name =~ '^\%(da\)\=sh$' || name =~ '^#!.\{-2,}\<\%(da\)\=sh\>' # Ubuntu links "sh" to "dash", thus it is expected to work the same way b:is_sh = 1 if exists("b:is_kornshell") diff --git a/runtime/syntax/sh.vim b/runtime/syntax/sh.vim index 298198274..9e5320f1c 100644 --- a/runtime/syntax/sh.vim +++ b/runtime/syntax/sh.vim @@ -9,6 +9,7 @@ " 2025 Jan 18 add bash coproc, remove duplicate syn keywords (#16467) " 2025 Mar 21 update shell capability detection (#16939) " 2025 Apr 03 command substitution opening paren at EOL (#17026) +" 2025 Apr 10 improve shell detection (#17084) " Version: 208 " Former URL: http://www.drchip.org/astronaut/vim/index.html#SYNTAX_SH " For options and settings, please use: :help ft-sh-syntax @@ -23,11 +24,13 @@ endif let b:is_sh = 1 " If the shell script itself specifies which shell to use, use it -if getline(1) =~ '\<ksh\>' +let s:shebang = getline(1) + +if s:shebang =~ '^#!.\{-2,}\<ksh\>' let b:is_kornshell = 1 -elseif getline(1) =~ '\<bash\>' +elseif s:shebang =~ '^#!.\{-2,}\<bash\>' let b:is_bash = 1 -elseif getline(1) =~ '\<dash\>' +elseif s:shebang =~ '^#!.\{-2,}\<dash\>' let b:is_dash = 1 " handling /bin/sh with is_kornshell/is_sh {{{1 " b:is_sh will be set when "#! /bin/sh" is found; @@ -70,6 +73,8 @@ elseif !exists("b:is_kornshell") && !exists("b:is_bash") && !exists("b:is_posix" endif endif +unlet s:shebang + " if b:is_dash, set b:is_posix too if exists("b:is_dash") let b:is_posix= 1 -- -- You received this message from the "vim_dev" maillist. Do not top-post! Type your reply below the text you are replying to. For more information, visit http://www.vim.org/maillist.php --- You received this message because you are subscribed to the Google Groups "vim_dev" group. To unsubscribe from this group and stop receiving emails from it, send an email to vim_dev+unsubscr...@googlegroups.com. To view this discussion visit https://groups.google.com/d/msgid/vim_dev/E1u2y4B-00Chl7-TS%40256bit.org.