1

I edit a lot of different file types, like .c, .cc, .S, .py, .rs, .ld, plus maybe a dozen more. And I'm constantly editing new file types, so I'm adding to this list. I have settings I want to apply equivalently to all of them, like shift width.

But, I don't want to do this to all files, since I also edit a lot of non-programming files.

It doesn't make sense to have to create a new config every time I come across a new file type, even if I have a common base file. The autocmd FileType is no better since I still need to add more configuration to .vimrc for every file type.

Is there some equivalent of an emacs prog-mode-hook where I can configure settings once and have them apply to all programming files?

12
  • Doesn't autocmd FileType work for you? – jdhao Dec 21 '20 at 2:10
  • 2
    How does the emacs mechanism know a new file type is associated with a "programming file" or not? – B Layer Dec 21 '20 at 2:17
  • 1
    Make a "super auto command" preconfigured with every supported programming language filetype...in the event you start using any of those in the future...? ;) Or create a single base "after" file and symlink it for every supported file type. (Easily scritable.) – B Layer Dec 21 '20 at 2:20
  • 1
    I'm not very familiar with Emacs, but from what I can gather the mode ("filetype" in Emacs jargon) for e.g. C (c-mode) inherits from prog-mode. It looks like you can actually inherit from multiple modes – it seems pretty neat. The closest you can get with Vim is by using dot-separated multiple filetypes (i.e set ft=prog.c). – Martin Tournoij Dec 21 '20 at 4:17
  • 1
    Couldn't it be that something like vim-sleuth will do? – Matt Dec 21 '20 at 6:52
1

I'm using a workaround on this topic. I have a function in my library plugin where I register all the filetypes I've encountered that relates to text, IOW they are non-programming filetypes.

" Function: lh#ft#is_text(...) {{{3
function! lh#ft#is_text(...)
  let ft = a:0 == 0 ? &ft : (a:1)
  return ft =~ '^$\|text\|latex\|tex\|html\|docbk\|help\|mail\|man\|xhtml\|markdown\|rst\|gitcommit'
endfunction

The downside is that this list is far from being complete. Occasionally I have to register a new filetype. Fortunately my DRY policy centered around my library-plugin permits to avoid redundancy.

So, you could register an autocommand at the same (priority) level that checks whether your filetype doesn't belong to text related filetypes.

0

In my case, I use function with autocommand all in one .vimrc file. See :help function or learn_vimscript.

Function structure:

function FunctionName()
    code
endfunction

Managing multiple file types

  1. I use one autocmd for each type of file. Each autocmd call a specific function that will get all the needed configs.

For instance if it's a python file:

autcmd FileType python call Python()

Or if it's an exotic file extention like ".pl" for Prolog:

autocmd BufReadPre *.s call Swipl()

Like that, the all the settings/keybinding/function-call/commands for a file type will be triggered only if this file type is read by vim. (don't forget to use key bindings with <buffer> option if you want to edit multiple file types in one session)

  1. To define a function (The first letter of the function name must take an uppercase) for example:
function Python()
    set wildmenu
    call CustomFunction()
    ...
endfunction

Managing options for a specific group of file type

Now we can create a specific function that will contain all the settings we need to code. For instance:

function ProgrammingFile()
    set cindent_shiftwidth=4
    ...
endfunction

To add those settings to a file type settings, I just have to put this in his function settings.

call ProgrammingFile()

You can define how many function you want to make some specific groups and call them where you want.

I Hope it will help you.

2
  • Those are prolog files, not swipl (though some of them are instead perl!). Also, you seem to re-inventing the FileType and ftplugin systems, which is really not necessary. Just drop the relevant code in (eg) ~/.vim/after/ftplugin/python.vim and use setlocal/etc – D. Ben Knoble Dec 26 '20 at 23:02
  • @D.Ben Knoble, that's interesting. I didn't know about the ftplugin systems. I still have lot to learn. Thanks – Fabrice Hategekimana Dec 27 '20 at 14:32

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.