3

For example, my file looks like:

#! /bin/bash
verbose()
{
    [ "$VERBOSE_FLAG" == 'yes' ] && 
    {
        [ $# == 0 ] || echo $@
        return 0
    } || return 1
}

verbose display in verbose mode

# make verbose highlight as a keyword
# vim: syntax keyword Keyword verbose

Is it possible use syn in a modeline or is there any alternative way?

Conner
  • 30,144
  • 8
  • 52
  • 73
wener
  • 7,191
  • 6
  • 54
  • 78

2 Answers2

4

You can't do that directly; only (buffer-local) options can be set in modeline.

option 1

If this additional keyword is not special to a single file, but applies to a wide range of files, I'd define a custom filetype (e.g. mybash), and have that additional keyword added in ~/.vim/syntax/mybash.vim:

" mybash is a kind of shell syntax
runtime! syntax/sh.vim syntax/sh/*.vim

" Add keyword.
syntax keyword mybashKeyword verbose

To use that custom syntax, you either augment the default filetype detection rules, or include ft=mybash in the modeline.

option 2

If this is just a minor tweak for one or few files, I'd use a local vimrc plugin to add the keyword.

There are several such plugins on vim.org; I can recommend the localrc plugin, which even allows local filetype-specific configuration.

Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324
  • This keyword is special to a single file. Seems I can use `set exrc` then add a `.vimrc/.exrc/.gvimrc` to current dir to replace __local imrc__. – wener Jan 22 '14 at 10:08
  • That's right; a `.exrc` would be a lightweight alternative of a localrc plugin; see also [this answer](http://stackoverflow.com/a/18933951/813602). – Ingo Karkat Jan 22 '14 at 10:14
0

Vim modelines are used to set various vim options (see :help modeline and :help options.txt). Since syntax highlighting is a command and not an option, it cannot be specified via a modeline. However, you can set syntax highlighting for a particular filename, file type, directory, etc. Simply use an autocommand (see :help au) to specify the file pattern you want to match and the command you want to execute.

Conner
  • 30,144
  • 8
  • 52
  • 73