I wrote the following in my .vimrc file in order to automate getting an HTML file set up.

:imap html<tab>

and so on. However, when I use this in an .html file the tabs are very off, seemingly because tabs in HTML files are treated like sequences of spaces rather than single characters. I'm open to any kind of suggestions, either about how to deal with this issue or more broadly if there's a better way to be accomplishing what I'm trying to do.


I think this has to do with the way indenting is handled. If this is true expandtab should be set in your HTML buffer (you can check with :set expandtab?). If you :set noexpandtab and then use your command, the tabs should be in the file and you should get the expected behavior.

Many people actually prefer to use spaces instead of tabs. This option allows them to use the tab key to insert the correct number of spaces (derived from shiftwidth). You do not seem to think this desirable, so you can safely set noexpandtab.

  • Interesting, I'll look into what expandtab does, since I don't fully get it right now. I'm on no side of the religious war about tabs and spaces except that I want the minimum number of keystrokes. However, when I check set: expandtab? it responds expandtab. When I set it to set: noexpandtab and re-try the command, the indents are still all off. So this doesn't seem to have been the source of the issue as far as I can tell. – Addem Jan 17 '17 at 16:47
  • I should perhaps comment that, apparently, the number of spaces that result when I use this command is about 8, twice as many as my tab command is set to produce. I know it's not supposed to work this way, but I can help wondering if the tabs in my remapping code is getting turned into tab commands when the remap happens. – Addem Jan 17 '17 at 16:52
  • I think I'm on to something: In HTML, unlike most of my other document types, the tabstop is set to 8 rather than 4, even though in the .vimrc file I set it to 4. I would have thought this would force it to be 4 in every document type but apparently not. In any case I think what's happening is that <Bs> still only deletes 4 spaces even though tab produces 8. – Addem Jan 17 '17 at 17:09
  • Well, here's as far as I made it: I found that my ftplugin path is located at /usr/share/vim/vim74/ftplugin where I have files html.vim and htmldjango.vim. I inspected them to the best of my ability but don't see where (or if) they set spacing. I'll look at some of the suggestions in the comments to see if that leads to anything, but I think I'm at an impasse here. – Addem Jan 17 '17 at 17:21

So it turns out that, for whatever reason, the settings I used caused the delete command to only delete four spaces at a time, while the file-type-specific settings cause the tab command to produce eight--thus, a mis-match in how things get tabbed and un-tabbed. The resolution I now use that seems to be working is, in the vimrc file, to include the file-specific setting

autocmd FileType html setlocal shiftwidth=4 tabstop=4

in order to override the default HTML settings. I'm fine with this solution but it might be better (or better practice?) to find the file that controls all file-specific settings and edit that file directly ... but that'll be a project for another day.

  • I prefer to put setlocal shiftwidth=4 tabstop=4 in ~/.vim/ftplugin/html.vim. See :help ftplugin-overrule, #3. Either way, it's definitely not better to edit the vim-installed ftplugin, as you suggest in your final paragraph. – Rich Jan 24 '17 at 15:53

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.