1

When editing Typescript neovim automatically sets indentexpr=GetTypescriptIndent(). I don't like the way it aligns to an open brace instead of inserting a (soft) tab or two, but it doesn't seem to be configurable. I tried adding:

au FileType javascript,typescript setlocal cindent indentexpr&

to my init.vim but it hasn't made any difference; indentexpr is still set to GetTypescriptIndent() when I edit typescript files. How can I get my autocmd to override whatever typescript.vim does?

3

To override settings, you need to use the after directory.

In this case, we need to know whether to use after/indent or after/ftplugin—use :verbose set <options we care about>? to find where they were last set, and pay attention to whether it was in the ftplugin or indent directory. We expect it to be indent in this case, but sometimes people do strange things.

Assuming we need to override an indent file, we place

setlocal indentexpr=
setlocal cindent

In ~/.vim/after/indent/javascript.vim (ditto for typescript).

Don’t forget to read about b:undo_ftplugin and b:undo_indent—they will need to be set properly.

To quote from :h undo_indent:

When the user does ":setfiletype xyz" the effect of the previous filetype
should be undone.  Set the b:undo_ftplugin variable to the commands that will
undo the settings in your filetype plugin.
...
For undoing the effect of an indent script, the b:undo_indent variable should
be set accordingly.

In this case, we'll need to append

'setlocal indentexpr< cindent<'

to b:undo_indent:

let b:undo_indent = get(b:, 'undo_indent', '')
if !empty(b:undo_indent)
  let b:undo_indent .= '|'
endif
let b:undo_indent .= 'setlocal indentexpr< cindent<'
| improve this answer | |
  • It should be indent, right? I mean, that's the point of filetype indent on, you want to be able to enable/disable indentation plug-ins on their own... (Maybe you could/should assume that in your answer?) – filbranden Aug 29 '19 at 14:07
  • @filbranden yes, but sometimes people do wacky things (I wouldnt expect it from the core vimruntime, but if its a plugin or something you never really know). Ill make a note though – D. Ben Knoble Aug 29 '19 at 14:08
  • In typescript's particular case, yeah, an indent file, as expected: github.com/leafgarland/typescript-vim/blob/master/indent/… – filbranden Aug 29 '19 at 14:10
  • Thanks, that seems to have done the trick. I'm not sure how I should use b:undo_indent, but I don't think it will matter for me anyway. – realh Aug 30 '19 at 12:56
  • @realh does my edit make undo_indent clearer? – D. Ben Knoble Aug 30 '19 at 13:24

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.