Vim: Autocommand for a specific file type that has no extension in the name?

Viewed 642
autocommand BufEnter,TextChanged,InsertLeave *.sh Neomake

I am trying to achieve the effect of the above's command for shellscripts with no extension.

2 Answers

You can create buffer-local autocommands for a buffer when it has its filetype set to one of the Shell filetypes (such as sh or bash.)

For example:

autocmd FileType sh,bash autocmd BufEnter,TextChanged,InsertLeave <buffer> Neomake

You can also use a filetype plug-in, adding the following to a file named ~/.vim/ftplugin/bash.vim (assuming your shell scripts have 'filetype' set to bash):

autocmd BufEnter,TextChanged,InsertLeave <buffer> Neomake
:help autocommand

displays a bunch of options, including autocmd-patterns.

:help autocmd-patterns

says this:

The pattern is interpreted like mostly used in file names:
* matches any sequence of characters; Unusual: includes path separators
? matches any single character
? matches a '?'
. matches a '.'
~ matches a '~'
, separates patterns
, matches a ','
{ } like ( ) in a |pattern|
, inside { }: like | in a |pattern|
} literal }
{ literal {
\{n,m} like {n,m} in a |pattern|
\ special meaning like in a |pattern|
[ch] matches 'c' or 'h'
[^ch] match any character but 'c' and 'h'
...
It is possible to use |pattern| items, but they may not work as expected, because of the translation done for the above.

Thus, if we want to match something that doesn't contain a .<some-suffix>, we can hack around with something like

autocommand BufEnter,TextChanged,InsertLeave *[^.]\\\{4\} Neomake

This would match any filename that is at least 4 (maybe 5, depending on if * matches the empty string) digits long and does not have a \..{0,3} suffix (e.g., .txt or the like). If you have suffixes that are longer, you would have to increase your number, which in turn will increase the minimum length of the non-suffixed filenames that it will match. This is an ugly hack, but I do not believe there is a better way.

Related