What exactly happens when you change the file type in vim?

If I have an apache file in /etc/apache2/sites-available/www.example.com and I set its file type this way

 :set filetype=apache 

What does it do? Is this a file change at all? Is this just reflected in the vim example? Vim session? I can manually set the file type, but then vim warns me that I am in read-only mode ( /etc/apache2 needs root access). If I open vim as root, I won’t get a warning, but if I leave and open it again (like regular or root), the file type will disappear. How to make it more persistent, at least when calling from the same session file

+4
source share
2 answers

set filetype changes the way a vim file is processed by invoking all FileType autocommands. This is not saved. If you want to always open this file with filetype=apache , try adding it to your .vimrc :

 au BufRead,BufNewFile /etc/apache2/sites-available/www.example.com set filetype=apache 

You can learn more about this in:

 :help 'filetype' :help filetypes :help :autocmd :help .vimrc 

EDIT: as found in my /usr/share/vim/vim73/filetype.vim :

 au BufNewFile,BufRead access.conf*,apache.conf*,apache2.conf*,httpd.conf*,srm.conf* call s:StarSetf('apache') au BufNewFile,BufRead */etc/apache2/*.conf*,*/etc/apache2/conf.*/*,*/etc/apache2/mods-*/*,*/etc/apache2/sites-*/*,*/etc/httpd/conf.d/*.conf* call s:StarSetf('apache') 

s:StarSetf will be setfiletype before apache if the file type does not match the ignored template. On my system :echo g:ft_ignore_pat will only show archive file extensions as ignored. setfiletype does set filetype , but only once.

So, at least on my system the template */etc/apache2/sites-*/* will catch your file name and make it apache .

+3
source

The file type basically allows Vim to change the settings for "file types". How this is done is an automatic autopilot command for the FileType category when changing the file type. This can potentially change your file if the automatic command for FileType is applicable to your file (but, as a rule, plug-in developers use it for changes like r / o, which affect the selection and not the contents of the file).

If you are concerned that the file type setting produces a file, you can see which FileType autocommands exist by issuing the following command:

 :au FileType 

To configure apache files as apache file types, you can put something like the following in your ~ / .vimrc:

 :au BufRead /etc/apache2/sites-available/* set ft=apache 
0
source

Source: https://habr.com/ru/post/1390234/


All Articles