7

For example, I have the following autocommand definition:

augroup cursorline
    autocmd!
    autocmd WinLeave,BufLeave * set nocursorline
    autocmd WinEnter,BufEnter * set cursorline
augroup end

I want to define something that excludes *.tex files:

autocmd WinEnter,BufEnter * "except *.tex" set cursorline

What is the syntax for that? I'm not sure what keywords to search the help docs.

6

In the same spirit as @Sundar answer but with a little more flexible syntax you can do:

let ftToIgnore = ['latex', 'plaintex']
autocmd BufWritePre * if index(ftToIgnore, &ft) < 0 | set your options

I tend to prefer this syntax because:

  • You can easily add new filtetypes if you want.
  • The condition length stay the same no matter how many filetypes you're checking.
  • Neat ! Where did you find that you could run a condition/function inside the autocommand ? – nobe4 Aug 24 '15 at 13:00
  • 2
    Well its the same principe as the @sundar answer excepted the condition isn't on the value of a variable but on the return value of a function. – statox Aug 24 '15 at 13:03
2

How about:

autocmd WinEnter,BufEnter * if &filetype != "latex" && &filetype != "plaintex" | set cursorline | endif

better formatted as

autocmd WinEnter,BufEnter * if &filetype != "latex" && &filetype != "plaintex" 
                            \ | set cursorline 
                            \ | endif

This depends upon the filetype as detected or set by Vim, and so doesn't have any case-sensitivity issues. (You might want to check what your tex files are being detected as, by running set filetype? in Vim's commandline).

1

I didn't find anything that seems to support what you want, but you can do quite the same with a little bit of scripting :

function! s:ToggleCursorLine(open) abort
  let l:extension = expand('%:e')
  if l:extension !=? 'tex'
    if a:open
      set cursorline
    else
      set nocursorline
    endif
  endif
endfunction

augroup cursorline
  autocmd!
  autocmd WinLeave,BufLeave * call s:ToggleCursorLine(1)
  autocmd WinEnter,BufEnter * call s:ToggleCursorLine(0)
augroup end

Reference : :h expand()

  • 1
    You might want to use !=# instead of != on UNIX, and !=? on Windows. Otherwise the result depends on the value of ignorecase. – Sato Katsura Aug 24 '15 at 5:35
  • Could you point me to an explaination of the difference between !=# and !=? ? Because on :h expr4 it doesn't mention Windows/Unix. – nobe4 Aug 24 '15 at 5:42
  • 1
    UNIX filenames are (usually) case-sensitive, Windows ones are case-insensitive. – Sato Katsura Aug 24 '15 at 9:56

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.