Skip to content

Commit 51a06ec

Browse files
mohd-akramchrisbra
authored andcommitted
runtime(sh): consider sh as POSIX shell by default
Also, do not set g:is_kornshell when g:is_posix is set. BSD shells are POSIX but many are derived from the ash shell. closes: #16939 Signed-off-by: Mohamed Akram <mohd.akram@outlook.com> Signed-off-by: Christian Brabandt <cb@256bit.org>
1 parent 5753084 commit 51a06ec

2 files changed

Lines changed: 49 additions & 49 deletions

File tree

runtime/doc/syntax.txt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*syntax.txt* For Vim version 9.1. Last change: 2025 Mar 15
1+
*syntax.txt* For Vim version 9.1. Last change: 2025 Mar 21
22

33

44
VIM REFERENCE MANUAL by Bram Moolenaar
@@ -3512,25 +3512,25 @@ cases pertain, then the first line of the file is examined (ex. looking for
35123512
/bin/sh /bin/ksh /bin/bash). If the first line specifies a shelltype, then
35133513
that shelltype is used. However some files (ex. .profile) are known to be
35143514
shell files but the type is not apparent. Furthermore, on many systems sh is
3515-
symbolically linked to "bash" (Linux, Windows+cygwin) or "ksh" (Posix).
3515+
symbolically linked to "bash" (Linux, Windows+cygwin) or "ksh" (POSIX).
35163516

35173517
One may specify a global default by instantiating one of the following
35183518
variables in your <.vimrc>:
35193519

35203520
ksh: >
35213521
let g:is_kornshell = 1
3522-
< posix: (using this is nearly the same as setting g:is_kornshell to 1) >
3522+
< posix: (default) >
35233523
let g:is_posix = 1
35243524
< bash: >
35253525
let g:is_bash = 1
3526-
< sh: (default) Bourne shell >
3526+
< dash: >
3527+
let g:is_dash = 1
3528+
< sh: Bourne shell >
35273529
let g:is_sh = 1
35283530
3529-
< (dash users should use posix)
3530-
35313531
If there's no "#! ..." line, and the user hasn't availed himself/herself of a
35323532
default sh.vim syntax setting as just shown, then syntax/sh.vim will assume
3533-
the Bourne shell syntax. No need to quote RFCs or market penetration
3533+
the POSIX shell syntax. No need to quote RFCs or market penetration
35343534
statistics in error reports, please -- just select the default version of the
35353535
sh your system uses and install the associated "let..." in your <.vimrc>.
35363536

runtime/syntax/sh.vim

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
" 2024 Nov 03 by Aliaksei Budavei <0x000c70 AT gmail DOT com> (improved bracket expressions, #15941)
88
" 2025 Jan 06 add $PS0 to bashSpecialVariables (#16394)
99
" 2025 Jan 18 add bash coproc, remove duplicate syn keywords (#16467)
10+
" 2025 Mar 21 update shell capability detection (#16939)
1011
" Version: 208
1112
" Former URL: http://www.drchip.org/astronaut/vim/index.html#SYNTAX_SH
1213
" For options and settings, please use: :help ft-sh-syntax
@@ -17,69 +18,68 @@ if exists("b:current_syntax")
1718
finish
1819
endif
1920

21+
" Ensure this is set unless we find another shell
22+
let b:is_sh = 1
23+
2024
" If the shell script itself specifies which shell to use, use it
2125
if getline(1) =~ '\<ksh\>'
2226
let b:is_kornshell = 1
2327
elseif getline(1) =~ '\<bash\>'
2428
let b:is_bash = 1
2529
elseif getline(1) =~ '\<dash\>'
2630
let b:is_dash = 1
27-
elseif !exists("g:is_kornshell") && !exists("g:is_bash") && !exists("g:is_posix") && !exists("g:is_sh") && !exists("g:is_dash")
28-
" user did not specify which shell to use, and
29-
" the script itself does not specify which shell to use. FYI: /bin/sh is ambiguous.
30-
" Assuming /bin/sh is executable, and if its a link, find out what it links to.
31-
let s:shell = ""
32-
if executable("/bin/sh")
33-
let s:shell = resolve("/bin/sh")
34-
elseif executable("/usr/bin/sh")
35-
let s:shell = resolve("/usr/bin/sh")
36-
endif
37-
if s:shell =~ '\<ksh\>'
38-
let b:is_kornshell= 1
39-
elseif s:shell =~ '\<bash\>'
40-
let b:is_bash = 1
41-
elseif s:shell =~ '\<dash\>'
42-
let b:is_dash = 1
43-
endif
44-
unlet s:shell
45-
endif
46-
4731
" handling /bin/sh with is_kornshell/is_sh {{{1
4832
" b:is_sh will be set when "#! /bin/sh" is found;
4933
" However, it often is just a masquerade by bash (typically Linux)
5034
" or kornshell (typically workstations with Posix "sh").
51-
" So, when the user sets "g:is_bash", "g:is_kornshell",
52-
" or "g:is_posix", a b:is_sh is converted into b:is_bash/b:is_kornshell,
53-
" respectively.
54-
if !exists("b:is_kornshell") && !exists("b:is_bash") && !exists("b:is_dash")
55-
if exists("g:is_posix") && !exists("g:is_kornshell")
56-
let g:is_kornshell= g:is_posix
35+
" So, when the user sets "g:is_kornshell", "g:is_bash",
36+
" "g:is_posix" or "g:is_dash", a b:is_sh is converted into
37+
" b:is_kornshell/b:is_bash/b:is_posix/b:is_dash, respectively.
38+
elseif !exists("b:is_kornshell") && !exists("b:is_bash") && !exists("b:is_posix") && !exists("b:is_dash")
39+
if exists("g:is_kornshell")
40+
let b:is_kornshell= 1
41+
elseif exists("g:is_bash")
42+
let b:is_bash= 1
43+
elseif exists("g:is_dash")
44+
let b:is_dash= 1
45+
elseif exists("g:is_posix")
46+
let b:is_posix= 1
47+
elseif exists("g:is_sh")
48+
let b:is_sh= 1
49+
else
50+
" user did not specify which shell to use, and
51+
" the script itself does not specify which shell to use. FYI: /bin/sh is ambiguous.
52+
" Assuming /bin/sh is executable, and if its a link, find out what it links to.
53+
let s:shell = ""
54+
if executable("/bin/sh")
55+
let s:shell = resolve("/bin/sh")
56+
elseif executable("/usr/bin/sh")
57+
let s:shell = resolve("/usr/bin/sh")
5758
endif
58-
if exists("g:is_kornshell")
59-
let b:is_kornshell= 1
60-
if exists("b:is_sh")
61-
unlet b:is_sh
62-
endif
63-
elseif exists("g:is_bash")
64-
let b:is_bash= 1
65-
if exists("b:is_sh")
66-
unlet b:is_sh
67-
endif
68-
elseif exists("g:is_dash")
69-
let b:is_dash= 1
70-
if exists("b:is_sh")
71-
unlet b:is_sh
72-
endif
59+
if s:shell =~ '\<ksh\>'
60+
let b:is_kornshell= 1
61+
elseif s:shell =~ '\<bash\>'
62+
let b:is_bash = 1
63+
elseif s:shell =~ '\<dash\>'
64+
let b:is_dash = 1
7365
else
74-
let b:is_sh= 1
66+
let b:is_posix = 1
7567
endif
68+
unlet s:shell
69+
endif
7670
endif
7771

7872
" if b:is_dash, set b:is_posix too
7973
if exists("b:is_dash")
8074
let b:is_posix= 1
8175
endif
8276

77+
if exists("b:is_kornshell") || exists("b:is_bash")
78+
if exists("b:is_sh")
79+
unlet b:is_sh
80+
endif
81+
endif
82+
8383
" set up default g:sh_fold_enabled {{{1
8484
" ================================
8585
if !exists("g:sh_fold_enabled")

0 commit comments

Comments
 (0)