For files of a certain filetype, specifying "file-type-specific" mappings is simple.

  • If Vim opens a fill and return filetype=python after issuing command :set filetype? through command-line (native to Vim), then python-specific mappings could be defined directly through ~/ftplugin/python.vim;

  • However, where shall I specify the mappings for files that return filetype= after issuing command :set filetype?. This applies to cases where one does not give a file any extension.

4 Answers 4


As Luc said, you would have to use an autocmd:

augroup NoFileType
  autocmd BufNewFile,BufRead * if &filetype ==# '' | setlocal filetype=noft | endif
  autocmd FileType noft echom 'No file type!'
augroup END

Or just add this to ftdetect/noft.vim:

autocmd BufNewFile,BufRead * if &filetype ==# '' | set filetype=noft | endif 

Then you can add mappings in ftplugin/noft.vim.

Also see this question about detecting an empty file at startup.

  • Instead of adding the one-liner to ftdetect/noft.vim, I dropped it directly to a *.vim file that is sourced when my Vim starts. Then, as you have described, dropping arbitrary settings into the ftplugin/noft.vim shall work for "headless" files.
    – llinfeng
    Aug 20, 2017 at 13:14
  • You might want wrap your auto commands with in at least one augroup: executing autocmd! clears the current group, otherwise a new autocmd copy is created each time the file is sourced. You can inspect a group with :verbose au GroupName. See this question and :h 40.3 for more details. Using ftdetect/*.vim takes care of the autocmd namespacing.
    – LEI
    Aug 20, 2017 at 13:40

An autocmd is not necessarly needed. Using <expr>, a check on filetype can do it too:

nnoremap <expr> <Leader>M &filetype == '' ? ":echo 'Example'<CR>" : ""

With :h <expr>, the mapping is set to the result of the expression on the right-hand side.

Here, if &filetype is empty, it will be set to :echo 'Example'<cr>. Else, it will be set to nothing.


I would define a autocommand for all buffers that'd test for the filetype. I'm afraid there is no other way.

  • OH, is it a function that may return a binary outcome variable after comparing the return value of :set filetype? against an empty string? I am afraid I don't know Vim script well enough to construct such function/autocommand on top of my head.
    – llinfeng
    Mar 21, 2017 at 12:57
  • In the autocommand you could indeed write something like if empty (&ft) | runtime ftplugin/nofiletype/*.vim | endif for instance. Mar 21, 2017 at 19:15

Depending on your use case for such feature, if you just don't want to have that mapping in files with a type, then you can set normal mapping and autocommand for any filetype that will remove that mapping:

nnoremap <Leader>M :echo "Example"<CR>

autocmd FileType * :nnoremap <buffer> <Leader>M <NOP>

This can have side effects if you would like to reuse that mapping in a file with a type - I'm not sure if it will work then (race condintion)

  • That will actually change whatever filetype was set to notype - rather not something that one would like to do.
    – grodzik
    Apr 20, 2017 at 18:41

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.