Skip to main content
Update the answer to take into account the usage of `syn iskeyword` in the shipped `syntax/sh.vim` (thanks @ChristianBrabandt for pointing it out!) (comment edited Dec 16, 2020 at 16:51)
Source Link
filbranden
  • 29.9k
  • 5
  • 28
  • 73

This behavior is happening because your 'iskeyword' did not include "_" as a valid keyword character when the syntax was first loaded.

The syntax for sh (among many other languages) use the :syn keyword command to define syntax based on keywords, and where the keywords are recognized (regarding the delimiters around them) depends on the 'iskeyword' setting by default.

To track down where "_" was removed from 'iskeyword', you can find where it was last set with the following command:

:verbose set iskeyword?

This should show you the current setting (where you'll notice it's missing _ in the list) and also the file and line number of the script where it was last set (which hopefully will be the place where _ was removed.)

If you fix it back with :set iskeyword+=_, you'll still have to reload the syntax (easiest is by using :set ft=sh) for it to highlight the keywords correctly.


There is also a :syn iskeyword command to override the global 'iskeyword' settings, but I imagine many or most when interpreting syntax definitions, including the onekeyword elements.

The syntax file for filetype=sh, will assume that does set a specific :syn iskeyword (thanks @ChristianBrabandt for pointing it out!) But it uses the globalvalue of the 'iskeyword' will be sanesetting in its definition. (It uses the :syn iskeyword command to add - and include delimiters such as _: to a keyword definition for syntax purposes.)

You can useoverride the :syn keyword command to fix this issuedefinition by creating a new syntax/sh.vim file in your local Vim after/ directory (~/.vim/after/syntax/sh.vim, or ~/config/nvim/after/syntax/sh.vim for NeoVim), with the following line in its contents:

syntax iskeyword @,48-57,_,192-255,-,:

This hardcodes the default 'iskeyword' settings, which include the "_" as a keyword character, asand also includes the one to use when parsingmodifications of the shipped syntax keywords for the sh filetype.

You may also decide to add more characters that should be considered as keyword characters forwhich adds sh- syntaxand (such as "-") in your local definition:.

This behavior is happening because your 'iskeyword' did not include "_" as a valid keyword character when the syntax was first loaded.

The syntax for sh (among many other languages) use the :syn keyword command to define syntax based on keywords, and where the keywords are recognized (regarding the delimiters around them) depends on the 'iskeyword' setting by default.

To track down where "_" was removed from 'iskeyword', you can find where it was last set with the following command:

:verbose set iskeyword?

This should show you the current setting (where you'll notice it's missing _ in the list) and also the file and line number of the script where it was last set (which hopefully will be the place where _ was removed.)

If you fix it back with :set iskeyword+=_, you'll still have to reload the syntax (easiest is by using :set ft=sh) for it to highlight the keywords correctly.


There is also a :syn iskeyword command to override the global 'iskeyword' settings, but I imagine many or most syntax definitions, including the one for filetype=sh, will assume that the global 'iskeyword' will be sane and include delimiters such as _.

You can use the :syn keyword command to fix this issue by creating a new syntax/sh.vim file in your local Vim directory (~/.vim/syntax/sh.vim, or ~/config/nvim/syntax/sh.vim for NeoVim), with the following line in its contents:

syntax iskeyword @,48-57,_,192-255

This hardcodes the default 'iskeyword' settings, which include the "_" as a keyword character, as the one to use when parsing syntax keywords for the sh filetype.

You may also decide to add more characters that should be considered as keyword characters for sh syntax (such as "-") in your local definition.

This behavior is happening because your 'iskeyword' did not include "_" as a valid keyword character when the syntax was first loaded.

The syntax for sh (among many other languages) use the :syn keyword command to define syntax based on keywords, and where the keywords are recognized (regarding the delimiters around them) depends on the 'iskeyword' setting by default.

To track down where "_" was removed from 'iskeyword', you can find where it was last set with the following command:

:verbose set iskeyword?

This should show you the current setting (where you'll notice it's missing _ in the list) and also the file and line number of the script where it was last set (which hopefully will be the place where _ was removed.)

If you fix it back with :set iskeyword+=_, you'll still have to reload the syntax (easiest is by using :set ft=sh) for it to highlight the keywords correctly.


There is also a :syn iskeyword command to override the global 'iskeyword' settings when interpreting syntax keyword elements.

The syntax file for filetype=sh does set a specific :syn iskeyword (thanks @ChristianBrabandt for pointing it out!) But it uses the value of the 'iskeyword' setting in its definition. (It uses the :syn iskeyword command to add - and : to a keyword definition for syntax purposes.)

You can override the :syn keyword definition by creating a new syntax/sh.vim file in your local Vim after/ directory (~/.vim/after/syntax/sh.vim, or ~/config/nvim/after/syntax/sh.vim for NeoVim), with the following line in its contents:

syntax iskeyword @,48-57,_,192-255,-,:

This hardcodes the default 'iskeyword' settings, which include the "_" as a keyword character, and also includes the modifications of the shipped syntax for sh which adds - and :.

Incorporate fleshed out suggestion to use `syn iskeyword` in a local `syntax/sh.vim` file.
Source Link
filbranden
  • 29.9k
  • 5
  • 28
  • 73

This behavior is happening because your 'iskeyword' did not include "_" as a valid keyword character when the syntax was first loaded.

The syntax for sh (among many other languages) use the :syn keyword command to define syntax based on keywords, and where the keywords are recognized (regarding the delimiters around them) depends on the 'iskeyword' setting by default.

(There is also a :syn iskeyword command to override the global 'iskeyword' settings, but I imagine many or most syntax definitions, including the one for filetype=sh, will assume that the global 'iskeyword' will be sane and include delimiters such as _.)

To track down where "_" was removed from 'iskeyword', you can find where it was last set with the following command:

:verbose set iskeyword?

This should show you the current setting (where you'll notice it's missing _ in the list) and also the file and line number of the script where it was last set (which hopefully will be the place where _ was removed.)

If you fix it back with :set iskeyword+=_, you'll still have to reload the syntax (easiest is by using :set ft=sh) for it to highlight the keywords correctly.


There is also a :syn iskeyword command to override the global 'iskeyword' settings, but I imagine many or most syntax definitions, including the one for filetype=sh, will assume that the global 'iskeyword' will be sane and include delimiters such as _.

You can use the :syn keyword command to fix this issue by creating a new syntax/sh.vim file in your local Vim directory (~/.vim/syntax/sh.vim, or ~/config/nvim/syntax/sh.vim for NeoVim), with the following line in its contents:

syntax iskeyword @,48-57,_,192-255

This hardcodes the default 'iskeyword' settings, which include the "_" as a keyword character, as the one to use when parsing syntax keywords for the sh filetype.

You may also decide to add more characters that should be considered as keyword characters for sh syntax (such as "-") in your local definition.

This behavior is happening because your 'iskeyword' did not include "_" as a valid keyword character when the syntax was first loaded.

The syntax for sh (among many other languages) use the :syn keyword command to define syntax based on keywords, and where the keywords are recognized (regarding the delimiters around them) depends on the 'iskeyword' setting by default.

(There is also a :syn iskeyword command to override the global 'iskeyword' settings, but I imagine many or most syntax definitions, including the one for filetype=sh, will assume that the global 'iskeyword' will be sane and include delimiters such as _.)

To track down where "_" was removed from 'iskeyword', you can find where it was last set with the following command:

:verbose set iskeyword?

This should show you the current setting (where you'll notice it's missing _ in the list) and also the file and line number of the script where it was last set (which hopefully will be the place where _ was removed.)

If you fix it back with :set iskeyword+=_, you'll still have to reload the syntax (easiest is by using :set ft=sh) for it to highlight the keywords correctly.

This behavior is happening because your 'iskeyword' did not include "_" as a valid keyword character when the syntax was first loaded.

The syntax for sh (among many other languages) use the :syn keyword command to define syntax based on keywords, and where the keywords are recognized (regarding the delimiters around them) depends on the 'iskeyword' setting by default.

To track down where "_" was removed from 'iskeyword', you can find where it was last set with the following command:

:verbose set iskeyword?

This should show you the current setting (where you'll notice it's missing _ in the list) and also the file and line number of the script where it was last set (which hopefully will be the place where _ was removed.)

If you fix it back with :set iskeyword+=_, you'll still have to reload the syntax (easiest is by using :set ft=sh) for it to highlight the keywords correctly.


There is also a :syn iskeyword command to override the global 'iskeyword' settings, but I imagine many or most syntax definitions, including the one for filetype=sh, will assume that the global 'iskeyword' will be sane and include delimiters such as _.

You can use the :syn keyword command to fix this issue by creating a new syntax/sh.vim file in your local Vim directory (~/.vim/syntax/sh.vim, or ~/config/nvim/syntax/sh.vim for NeoVim), with the following line in its contents:

syntax iskeyword @,48-57,_,192-255

This hardcodes the default 'iskeyword' settings, which include the "_" as a keyword character, as the one to use when parsing syntax keywords for the sh filetype.

You may also decide to add more characters that should be considered as keyword characters for sh syntax (such as "-") in your local definition.

Source Link
filbranden
  • 29.9k
  • 5
  • 28
  • 73

This behavior is happening because your 'iskeyword' did not include "_" as a valid keyword character when the syntax was first loaded.

The syntax for sh (among many other languages) use the :syn keyword command to define syntax based on keywords, and where the keywords are recognized (regarding the delimiters around them) depends on the 'iskeyword' setting by default.

(There is also a :syn iskeyword command to override the global 'iskeyword' settings, but I imagine many or most syntax definitions, including the one for filetype=sh, will assume that the global 'iskeyword' will be sane and include delimiters such as _.)

To track down where "_" was removed from 'iskeyword', you can find where it was last set with the following command:

:verbose set iskeyword?

This should show you the current setting (where you'll notice it's missing _ in the list) and also the file and line number of the script where it was last set (which hopefully will be the place where _ was removed.)

If you fix it back with :set iskeyword+=_, you'll still have to reload the syntax (easiest is by using :set ft=sh) for it to highlight the keywords correctly.