2

I don't like how vim-polyglot does indentation for my .html files. One apparent solution I found from researching online is to create ~/.vim/indent/html.vim and put the following line in it:

let b:did_indent = 1 

This works! However, I'd much prefer it, if I could do without this one extra config file and just have this setting in my ~/.vim/vimrc with all the other settings. From reading vim-polyglot docs and looking at its source code, there is both

if exists("b:did_indent") "{{{ finish endif 

as well as:

if polyglot#init#is_disabled(expand('<sfile>:p'), 'html5', 'indent/html.vim') finish endif 

So I thought I could maybe simply set

let g:polyglot_disabled = ['html5'] 

to disable the plugin for .html files, as described on its GitHub repo website. But it seems to have no effect at all.

I'm new to Vim and vimscript and I'd appreciate any pointers. Either how to fix the vim-polyglot settings - or to move the did_indent workaround line into my vimrc.


To isolate the issue and to make it reproducible, I have completely deleted my ~/.vim folder and started fresh:

Once I add an extra file ~/.vim/indent/html.vim with let b:did_indent = 1, the issue disappears.

Furthermore, I have verified that :set filetype? after opening the file indeed has output filetype=html, so the file is recognized as html. Also, I have compared the output from :scriptnames

Here's a diff (with indent/html.vim on the left, without on the right):

48,51c48,57 < 48: ~/.vim/indent/html.vim < 49: ~/.vim/pack/plugins/opt/vim-polyglot/indent/html.vim < 50: /usr/share/vim/vim82/indent/html.vim < 51: /usr/share/vim/vim82/scripts.vim --- > 48: ~/.vim/pack/plugins/opt/vim-polyglot/indent/html.vim > 49: ~/.vim/pack/plugins/opt/vim-polyglot/indent/javascript.vim > 50: /usr/share/vim/vim82/indent/javascript.vim > 51: ~/.vim/pack/plugins/opt/vim-polyglot/after/indent/javascript.vim > 52: ~/.vim/pack/plugins/opt/vim-polyglot/after/indent/javascript-1.vim > 53: ~/.vim/pack/plugins/opt/vim-polyglot/after/indent/jsx.vim > 54: ~/.vim/pack/plugins/opt/vim-polyglot/after/indent/javascript-2.vim > 55: ~/.vim/pack/plugins/opt/vim-polyglot/indent/graphql.vim > 56: /usr/share/vim/vim82/indent/html.vim > 57: /usr/share/vim/vim82/scripts.vim 

But I don't see any relevant difference. Maybe someone else does.

The command :set cindent? smartindent? indentexpr? autoindent? currently says:

nocindent nosmartindent indentexpr=HtmlIndent() autoindent 

Putting let g:polyglot_disabled = ['sensible', 'html5'] which is supposed to disable vim-polyglot for .html, doesn't change anything (or overwrites HtmlIndent with the same name):

nocindent nosmartindent indentexpr=HtmlIndent() autoindent 

However, with putting the extra ~/.vim/indent/html.vim, I get:

nocindent nosmartindent indentexpr= autoindent 

My vim version is updated, most recent one for current Debian LTS release 11 (bullseye). The 3 topmost lines of output from :version are:

VIM - Vi IMproved 8.2 (2019 Dec 12, compiled Oct 01 2021 01:51:08) Included patches: 1-2434 Extra patches: 8.2.3402, 8.2.3403, 8.2.3409, 8.2.3428 
1
  • 1
    Looks much better after the edit. Thank you. For the future, deleting your ~/.vim dir is a bit extreme, you can use vim --clean or vim -N -u NONE instead. Just a side note. Commented Oct 1, 2024 at 11:37

3 Answers 3

1

Here is a quick hack, if you don't want to use a separate indent/html.vim (which I would still recommend).

Use a filetype autocommand to undo the effect of the indent script using the special buffer-local variable undo_indent which all indent plugins should set:

augroup CustomHTML au! au Filetype html :if exists('b:undo_indent') | exe b:undo_indent | endif augroup END 
3
  • Thank you very much for your answer. Unfortunately, it doesn't seem to have any effect at all. I have tried to put it before packadd! as well as afterwards. My vimrc is reduced to 3 lines to isolate the issue. (See updated question text above.) Commented Sep 30, 2024 at 22:50
  • Strange, I tested it with the default html indent script and it worked. perhaps it triggers too early for packadd. Can you try this one instead? au Filetype html let b:did_indent=1 Commented Oct 1, 2024 at 11:32
  • Sorry, no, unfortunately seems to have no effect at all. And I tried both before and after packadd. It's like the autocmd doesn't run at all. I have noticed as much with one of the other answers. Manually setting :set indentexpr= works, but in an autocmd it doesn't. Commented Oct 1, 2024 at 12:17
1

Here are the possibilities I see:

Restore the indentation settings after polyglot

If HtmlIndent() expression is not needed:

augroup HtmlIndent autocmd! autocmd FileType html setlocal indentexpr= nocindent nosmartindent autoindent augroup END 

If HtmlIndent() expression is needed:

function! RestoreIndent() setlocal indentexpr=HtmlIndent() nocindent nosmartindent autoindent let b:did_indent = 0 let g:force_reload_html = 1 execute 'source $VIMRUNTIME' . '/indent/html.vim' let g:force_reload_html = 0 endfunction augroup HtmlIndent autocmd! autocmd FileType html call RestoreIndent() augroup END 

Remark: vim-polyglot overrides the HtmlIndent method. The code needs to restore it from the Vim runtime.

Disable polyglot for html

To follow your suggestion (but this seems to disable polyglot completely for html files) you can add before the packadd! vim-polyglot command:

let g:polyglot_disabled = ['html5'] 

Remark: I have tested the autocmd approach but polyglot uses the ftplugin approach that is triggered before the autocmd which makes the approach failing.

5
  • Thanks for your feedback. That is odd. Did you tried at the start of the vimrc? Commented Sep 30, 2024 at 15:55
  • I have modified the suggestion to override the autocmd set by vim-polyglot. Commented Sep 30, 2024 at 16:27
  • I have installed polyglot and I can reproduce your problem. My approach is indeed not working (because the ftplugin scripts are triggered before the autocmd). Commented Sep 30, 2024 at 18:27
  • Thanks for the feedback! autocmd FileType html set indentexpr= should also work only for html does it? Commented Oct 1, 2024 at 15:26
  • Thanks for the feedback. It is very strange to me :-). I'll try to reproduce your behavior on Vim 8.2 (I'm using Vim 9.1 where the behavior seems different). I'm happy you have found a solution to your problem :-) Commented Oct 1, 2024 at 15:55
0

With the help of the other answers and comments, I was able to find out that the culprit of what I simply described as "don't like how vim-polyglot does indentation for .html", is the setting of indentexpr.

This setting is set to an internal HtmlIndent function by default, but it seems like the plugin overwrites this value with its own function - and this function doesn't work as well as the function for indentexpr of other file types, which is why I didn't like its behavior for .html files. For example, autoindent seems to be ignored, the indentation of the previous line doesn't play a role at all, but indentation of a new line always follows its supposed indentation according to the nesting hierarchy indentation level.

So my solution, which is only partially an answer to the original question, is to completely disable this type of re-indenting for all file types with:

autocmd BufEnter * setlocal indentexpr= 

Now autoindent takes effect, which is good enough for me.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.