0

I have a large script and I have extracted a few lines into two files, why1.ksh and why2.ksh, to show highlighting issue I am seeing that I can't explain.

Here are the two lines of why1.ksh:

output_string "LOG" "[`date`][${RULE_NAME}][${SOURCE_NAME}][${DEST_NAME[${DEST_INDX}]}] DEST_FLAG_FILE[${DEST_INDX}]:       ${DEST_FLAG_FILE[${DEST_INDX}]:}"
output_string "LOG" "[`date`][${RULE_NAME}][${SOURCE_NAME}][${DEST_NAME[${DEST_INDX}]}] DEST_FLAG_FILE[${DEST_INDX}]:       ${DEST_FLAG_FILE[${DEST_INDX}]:}"

Here's the one line of why2.ksh:

SUCCESS_EMAIL="${SUCCESS_EMAIL//,/\\n}"

Can any of you reproduce these problems and then tell me what is the problem?

I am using vim version 8.2.0486-1. My syntax/vim.sh version is 189, last change October 16, 2019.

Images follow...

enter image description here enter image description here

Here's a version of why2.ksh that shows it works for me as coded:

enter image description here

3
  • What is the highlighting issue you're experiencing? Note that Vim shipped a buggy syntax/sh.vim for a while, I think the bug was related to highlighting closing quotes... You might want to try the latest Vim (or just upgrade syntax/sh.vim) to see if your issue is solved there.
    – filbranden
    Jan 5 at 22:20
  • Also, the syntax may not know to use ksh (sometimes you have to set this), and getting shell syntax right for vim is actually quite difficult
    – D. Ben Knoble
    Jan 6 at 13:58
  • You can post screenshots by attaching images here. See instructions here: meta.stackexchange.com/questions/317576/… (In short, you can drag and drop images into the edit box and they'll get attached here.)
    – filbranden
    Jan 7 at 2:56
1

These are actually highlighting actual issues you have with your files.

In why1.ksh, the line ends with:

${DEST_FLAG_FILE[${DEST_INDX}]:}

So you have a ${parameter} followed by a single :, as in ${parameter:}, but that is not actually valid syntax in ksh or any Bourne shell. The valid operators are :- or := or :+ or :?. Bash additionally supports : with an offset and optional length, but you don't have an offset here either, and this is ksh. But a : on its own just before the } seems to be invalid on every shell.

See here in the syntax/sh.vim for the recognized formats for each shell flavor:

" sh ksh bash : ${parameter:-word}    word is default value
" sh ksh bash : ${parameter:=word}    assign word as default value
" sh ksh bash : ${parameter:?word}    display word if parameter is null
" sh ksh bash : ${parameter:+word}    use word if parameter is not null, otherwise nothing

In why2.ksh, you're trying to use the ${parameter//pattern/string} syntax. This is a valid syntax, but only for bash and not for ksh. (The syntax highlighting script for filetype sh detects the flavor, based on the file extension, the shebang line or some global variable settings, so it's detecting ksh in your case.)

See here for the comment about this particular syntax format, in particular the comment indicating it's only recognized for the "bash" flavor.

 " bash : ${parameter//pattern/string}
 " bash : ${parameter//pattern}
4
  • 1
    One can also force a certain syntax by e.g. defining some global variables :let g:is_bash=1 and a like, see :h sh.vim and scroll down a bit Jan 7 at 19:04
  • On why1.ksh, good point! That one is an easy fix! Jan 7 at 23:30
  • On why2.ksh, it appears on my version of ksh that syntax is valid. I have added an image of the working code above. Thoughts? Jan 7 at 23:43
  • @JoelBreazeale I guess the author of syntax/sh.vim was being strict with ksh settings, since there are actually quite a few flavors of ksh and some of them support more or fewer features... You might want to consider using let g:is_bash = 1 in your vimrc as suggested by Christian above, this would make syntax/sh.vim always consider bash settings. This would assume that bash-like syntax would be OK, which I'm not sure it's really the case... Anyways, I'm not really a ksh user, so not sure what would be best here.
    – filbranden
    Jan 7 at 23:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.