0

VIM's Wikia documentation states that you can disable indentation for certain filestypes by inserting the line

let b:did_indent = 1

in the file ~/.vim/indent/<filetype>.vim where <filetype> is substituted by the filetype used.

What if I want this applied specifically to files without a suffix? What filetype does that constitute?

I have already tried using autocmd FileType let b:did_indent = 1 in my vimrc file, which doesn't work, as expected, since the blank space doesn't constitute no suffix files.

Note.

By indentation, I mean the behaviour that you get by having set autoindent inside your vimrc. I want this disabled for text-editing; in my case, files without a suffix.

4
  • What do you mean exactly by indent? Files with no filetype set will have whatever the global default settings are, usually set in your vimrc
    – Mass
    Mar 30, 2018 at 14:42
  • I mean automatic indentation which is set by set autoindent.
    – easytarget
    Mar 30, 2018 at 14:45
  • autoindent is by default off, so you must be setting it somewhere
    – Mass
    Mar 30, 2018 at 14:46
  • What do you mean? If I have the line set autoindent in my vimrc --- which I intend to keep ---; this will enable automatic indentation by default indeed. But I want to disable it for filenames without a suffix. Problem: I don't know which filetype this constitutes.
    – easytarget
    Mar 30, 2018 at 14:49

1 Answer 1

1

Files without a recognized filetype do not trigger the FileType autocmd at all. Sometimes, but not always, this means files without an extension in their name. Filetypes and suffixes are different things. To be sure, you can do :set filetype? to see what the filetype is set to. If this is an empty string, then no FileType autocmd can be used.

It seems to me what you want is the following:

  1. Files with a filetype get set autoindent

  2. Files without a filetype (i.e., plain text) get set noautoindent

Thus in your vimrc, you can use:

set noautoindent
augroup vimrc_indent
    au!
    autocmd FileType * set autoindent
augroup END

The first set sets the global option value, which applies to all buffers unless otherwise stated. The second set sets the option for files with filetype.

See :help autocmd and :help augroup for the meaning of augroup and au!. Briefly, this pattern of wrapping an autocmd with augroup (vimrc_indent is a completely arbitrary string) prevents multiple instances of the same autocmd from being installed when your vimrc is reloaded.

2
  • Could you elaborate on the use of augroup and au!? Also, what kind of keyword is vimrc_indent?
    – easytarget
    Mar 30, 2018 at 15:12
  • @MusséRedi It prevents those autocommands from being defined multiple times (and thus being run multiple times!) if you reload your vimrc. Steve Losh explains it pretty well here.
    – ZeroKnight
    Mar 31, 2018 at 3:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.