Highlighting problem with ksh syntax

23 views
Skip to first unread message

Janis Papanagnou

unread,
May 28, 2026, 2:15:42 PMMay 28
to vim...@googlegroups.com
I just cloned https://github.com/vim/vim.git

I copied the various existing files bash.vim, sh.vim, zsh.vim
(which I'm interested in) below my local $HOME/.vim
into their respective directories syntax, indent, and ftplugin .

Opening a local test file that contains (besides other things)

if [[ "$1" = "$2" ]]
...
x=$'\v'

the above two lines were marked as containing errors;
the final closing ] and the $' and terminating ' .

These errors vanish if I type these two commands consecutively
:set syntax=bash
:set syntax=sh
I wonder why that works but not a single :set syntax=sh alone.
(Is that due to some persisting side-effect from the bash setting?)

The errors also vanish if beforehand a #!/bin/ksh line is present.

My question (or request, if allowed) would be whether the correct
syntax highlighting for ksh syntax could be automatically detected
without the double-"set syntax" kludge or requiring a shebang line.

Thanks!

I'm also curious why for bash and zsh there's own :set syntax=...
possible but :set syntax=ksh disables syntax colors completely.

Janis

Christian Brabandt

unread,
May 28, 2026, 2:47:12 PMMay 28
to vim...@googlegroups.com

On Do, 28 Mai 2026, Janis Papanagnou wrote:

> I just cloned https://github.com/vim/vim.git
>
> I copied the various existing files bash.vim, sh.vim, zsh.vim
> (which I'm interested in) below my local $HOME/.vim
> into their respective directories syntax, indent, and ftplugin .
>
> Opening a local test file that contains (besides other things)
>
> if [[ "$1" = "$2" ]]
> ...
> x=$'\v'

Traditionally, the vim sh filetype supports only the very narrow
traditional (Bourne) syntax, not Bash, not ksh and not even POSIX
syntax.

However there were a few complaints over time, so a few heuristics have
been built into the sh syntax script, which tries to guess what sh
dialect is in use, e.g. by setting global variables or by inspecting the
shebang line.

I believe the above is not valid Bourne shell syntax, that's why you see
those errors.

You can read about it a bit more at :h ft-sh-syntax

Note: zsh is a separate filetype and comes with its own syntax rules. It
does in no way depend on the sh syntax. ksh however does not have it's
own filetype but uses the sh syntax script.

>
> the above two lines were marked as containing errors;
> the final closing ] and the $' and terminating ' .
>
> These errors vanish if I type these two commands consecutively
> :set syntax=bash
> :set syntax=sh
> I wonder why that works but not a single :set syntax=sh alone.
> (Is that due to some persisting side-effect from the bash setting?)
>
> The errors also vanish if beforehand a #!/bin/ksh line is present.
>
> My question (or request, if allowed) would be whether the correct
> syntax highlighting for ksh syntax could be automatically detected
> without the double-"set syntax" kludge or requiring a shebang line.
>
> Thanks!
>
> I'm also curious why for bash and zsh there's own :set syntax=...
> possible but :set syntax=ksh disables syntax colors completely.

Some time ago, it was suggested to make bash its own independent
filetype, but this has caused a bit of backlash, mainly for being
backwards incompatible, so we went back to the sh filetype by default
(and kept the unusual configuration via heuristics and additional global
vim variables).

Best,
Christian
--
Something's rotten in the state of Denmark.
-- Shakespeare

Janis Papanagnou

unread,
May 28, 2026, 3:31:18 PMMay 28
to vim...@googlegroups.com
Hi Christian,

thanks for the explanation/history. - Some remarks below...

> On Do, 28 Mai 2026, Janis Papanagnou wrote:
>
> > I just cloned https://github.com/vim/vim.git
> >
> > I copied the various existing files bash.vim, sh.vim, zsh.vim
> > (which I'm interested in) below my local $HOME/.vim
> > into their respective directories syntax, indent, and ftplugin .
> >
> > Opening a local test file that contains (besides other things)
> >
> > if [[ "$1" = "$2" ]]
> > ...
> > x=$'\v'
>
> Traditionally, the vim sh filetype supports only the very narrow
> traditional (Bourne) syntax, not Bash, not ksh and not even POSIX
> syntax.
>
> However there were a few complaints over time, so a few heuristics have
> been built into the sh syntax script, which tries to guess what sh
> dialect is in use, e.g. by setting global variables or by inspecting the
> shebang line.
>
> I believe the above is not valid Bourne shell syntax, that's why you see
> those errors.

Yes, that is correct. (Actually Bourne shell 'sh' is meaningless nowadays.
The minimum expected standard is the POSIX Shell 'sh', based on ksh88.)

The [[...]] is syntax introduced by ksh(88) that is meanwhile supported
by all prominent shells, e.g. Ksh (88 and 93), Bash, and Zsh, and POSIX
at least "recognizes" that syntax.

The $'...' "ANSI strings" were introduced (I think) also by ksh (ksh93 in
this case) and is also supported by the prominent shells ksh, bash, zsh.

The points of my post were mainly that
* the double-set-syntax kludge showed an effect that was probably
an undesired side-effect that might need some attention; it could
be that these side effects are a key to make syntax highlighting
work also correctly for ksh (without that interactive kludge); I
mean, somehow it works then, so why not in the first place.
* correct highlighting of the syntax (invented by ksh and borrowed
by other shells) is provided but strangely not for Ksh (only for
Bash and Zsh it works; in zsh natively, and in bash by heuristics,
as I understand your explanations).
* given that there's syntax code/rules that are triggered for Bash
I'd have hoped that these could also be made to become effective
with Ksh.

Somehow it seems that the shell-heuristics detection seems to work for
bash but (strangely) isn't very good with ksh?

I'm lacking the Vim Syntax programming knowledge but maybe someone who
does may make something of above observations; to make ksh syntax work
as it already does with the mentioned kluge - which obviously doesn't
need any other heuristics than implicitly already present somewhere!
(It just doesn't get triggered initially. Why?)

> You can read about it a bit more at :h ft-sh-syntax

Thanks. I'll have another look to better understand the current state.

Janis

Charles Campbell

unread,
Jun 7, 2026, 9:12:13 AM (6 days ago) Jun 7
to vim...@googlegroups.com
Janis Papanagnou wrote:
> Hi Christian,
>
> thanks for the explanation/history. - Some remarks below...
>
>> On Do, 28 Mai 2026, Janis Papanagnou wrote:
>>
>>> I just cloned https://github.com/vim/vim.git
>>>
>>> I copied the various existing files bash.vim, sh.vim, zsh.vim
>>> (which I'm interested in) below my local $HOME/.vim
>>> into their respective directories syntax, indent, and ftplugin .
>>>
>>> Opening a local test file that contains (besides other things)
>>>
>>> if [[ "$1" = "$2" ]]
>>> ...
>>> x=$'\v'
>> Traditionally, the vim sh filetype supports only the very narrow
>> traditional (Bourne) syntax, not Bash, not ksh and not even POSIX
>> syntax.
>>
>> However there were a few complaints over time, so a few heuristics have
>> been built into the sh syntax script, which tries to guess what sh
>> dialect is in use, e.g. by setting global variables or by inspecting the
>> shebang line.
>>
>> I believe the above is not valid Bourne shell syntax, that's why you see
>> those errors.
> Yes, that is correct. (Actually Bourne shell 'sh' is meaningless nowadays.
> The minimum expected standard is the POSIX Shell 'sh', based on ksh88.)
>
The sh.vim syntax file was written to cover three types: ksh, bash, sh,
and was extended to handle dash. There are various ways that sh.vim
attempts to infer which shell the user actually wanted. However, this
process is likely to fail to be a good mind reader, and so things like:

let g:is_kornshell= 1
let g:is_bash= 1
let g:is_posix= 1

placed in your .vimrc will get sh.vim to do what you want. The reason
why it defaults to Bourne shell (sh) is to encourage users to specify
what they actually tend to use. In my experience, nearly half of the
users use bash and near half use kornshell/posix. There are plenty of
people that feel that their choice should be made the default; since
bash/posix/ksh share a lot of non-Bourne features about half will be
satisfied and the other half will be constantly issuing bug reports.

Chip Campbell

Janis Papanagnou

unread,
Jun 11, 2026, 5:27:09 AM (2 days ago) Jun 11
to vim...@googlegroups.com
Thanks for your reply.

If I understand correctly you are suggesting to hard-configure the "used shell" by, say,
let g:is_kornshell= 1

But this (as it appears to me) would not be an appropriate solution to the problem.
The setting should not be depending on a single shell type that a user has to specify.
It shall depend on the actual shell *necessary* to parse _the given source codes_.

I am actually using different shells; sh (POSIX), bash (from others), and ksh (preferred).
All files should be correctly highlighted. And ideally by an heuristic, not by forcing the
user to change the operated items. But the (existing) heuristics seems not be sufficient.

(And, BTW, it's not about "mind-reading". - It's about keywords and syntax-reading.)

What I seem to currently have to do is either to explicitly set the syntax (ex-command),
or to specify a shebang line on each file (whether mine or third party), or maybe add
(or change) the file extension. - Usually I don't want (or am not allowed) to change the
file contents or its name.

All that is of course aside from the actual inherent issues with shell highlighting in Vim
that have been detected.

Janis

________________________________________
Von: vim...@googlegroups.com <vim...@googlegroups.com> im Auftrag von Charles Campbell <astro...@gmail.com>
Gesendet: Sonntag, 7. Juni 2026 15:12
An: vim...@googlegroups.com
Betreff: Re: AW: Highlighting problem with ksh syntax

Chip Campbell

--
--
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+u...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/vim_dev/3d7aa4be-ee65-faf0-bd05-51daff280d37%40gmail.com.

Reply all
Reply to author
Forward
0 new messages