partial syntax in vim

24 views
Skip to first unread message

Andreas Otto

unread,
Jun 17, 2024, 2:18:11 AM (9 days ago) Jun 17
to vim_use
Hi is it possible to change the syntax for a limit part of a file?
example: tcl file with bash syntax used with "pseudo code" vim: push … vim: pop

if {checkNotIgnore("DIFF")} {
  File_Exec_BASH {
    # vim: push syntax=bash
    set -ue

    [[ "$(yes_or_no.bash 'want to exit?')" == "y" ]] && exit

    declare -a files=($(find . -name .vimrc 2>/dev/null))

    git diff "${files[@]}"

    if [[ $(git status --porcelain "${files[@]}") ]] ; then
      if [[ "$(yes_or_no.bash 'commit changes?')" = "y" ]] ; then
        git commit -m lng_Shortcut.tcl "${files[@]}"
      fi
      :
    fi
    exit 0
    # vim: pop syntax
  } <@stdin >@stdout 2>@stderr
}

Lifepillar

unread,
Jun 23, 2024, 9:42:29 AM (3 days ago) Jun 23
to vim...@googlegroups.com
On 2024-06-17, Andreas Otto <aotto1...@gmail.com> wrote:
> Hi is it possible to change the syntax for a limit part of a file?

Yes. See :help syn-include and :help mysyntaxfile-add.

> example: tcl file with bash syntax used with "pseudo code" vim: push … vim:

I do not recommend using comments with `vim:` because they are
interpreted specially by Vim. Just use (for instance):

if {checkNotIgnore("DIFF")} {
File_Exec_BASH {
# syntax=bash
[...]
# pop syntax
} <@stdin >@stdout 2>@stderr
}

Create ~/.vim/after/syntax/tcl.vim with this content:

unlet b:current_syntax
syn include @Bash syntax/bash.vim
syn region tclBash matchgroup=Comment start=/^\s*# syntax=bash/ end=/^\s*# pop syntax/ contains=@Bash
let b:current_syntax = "tcl"

Hope this helps,
Life.

Reply all
Reply to author
Forward
0 new messages