3

I want to set some commands for specific filetypes. For example an abbreviation:

:iabbr Sb SameBatch

The files have extension like .data, .pro. Their grammar, syntax-highlighting are defined in the framework. I am unaware of those things. Those files contain variables and their values for Perl scripts.

I tried

:au FileType data iabbr di ditto

But it doesn't work.

I thought that the extension is the file type of a file. Is it wrong and how to get it worked?

3
  • 2
    What's missing is a way for Vim to recognize the "data" filetype. You will have to add something like: au BufNewFile,BufRead *.data set filetype=data. See also this link
    – VanLaser
    Commented May 2, 2016 at 13:00
  • 1
    Run :set filetype. That will tell you what Vim thinks is the filetype.
    – muru
    Commented May 2, 2016 at 13:03
  • 1
    @muru : I gave that command and found the type to be conf. When I give :au FileType conf set command, it works. :)
    – SibiCoder
    Commented May 3, 2016 at 6:42

2 Answers 2

5

You could use the BufEnter autocommand event like this:

autocmd! BufEnter *.data :iabbr <buffer> di ditto

The abbreviation will be created when you enter a buffer which is named anything followed by .data and will exists only in this buffer thanks to <buffer>.

To answer more precisely the comment of @SibiCoder I'll detail the line:

  • BufEnter trigger the command when a the user enter a buffer.
  • *.data restrict the execution of the autocommand to files ending with .data this way when you enter a file named foo.c or bar.sh the autocommand is not trigger.
  • :iabbr <buffer> di ditto is the command to execute and because of the <buffer> argument the abbreviation will be defined only for the current buffer.

See :h autocommand-events and :h :abbreviate-<buffer>

4
  • 1
    I thought about this, but there may be an edge case when you define a special filetype for a file extension, i.e. with an autocommand autocmd! bufreadpost *.gs set filetype=javascript Here the extension is gs but the filetype is javascript
    – nobe4
    Commented May 2, 2016 at 13:03
  • 1
    //will exist only in this buffer // what does this mean? I thought this abbreviation will work in any buffer. Does this bufenter trigger every time when a file of this type is edited?
    – SibiCoder
    Commented May 2, 2016 at 13:04
  • 2
    @SibiCoder yes. This way, a buffer for a .sh file, for example, wouldn't have this abbreviation. Keeps your abbreviations and maps clean.
    – muru
    Commented May 2, 2016 at 13:06
  • @SibiCoder: muru is right, see the edit of my answer for a more detailed explanation.
    – statox
    Commented May 2, 2016 at 13:21
5

Use a function for this:

function! FTManage()
  let l:filetype = &filetype
  if l:filteype == 'data'
      " do stuff
  endif
endfunction

autocmd! FileType * call FTManage()

Here you pass through the function when the filetype change, so you can filter by filetype and call custom commands.

The main difference between statox's answer and this one is the type of filter. You can filter by file extension, but for some cases it's not relevant (because you can have an autocommand that change the filetype on read), so you can listen for the Filetype event and filter afterward.

2
  • 2
    The only "issue" of this answer is exactly the fact that Vim may not know anything about i.e. the *.data extension of a file, so the filetype may be not set up correctly, or at all.
    – VanLaser
    Commented May 2, 2016 at 13:42
  • 3
    Right, in which case, statox's answer is better :)
    – nobe4
    Commented May 2, 2016 at 13:43

Your Answer

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

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