[Vim learning series article 13.1 -- Automatic command autocmd sets vim parameters according to file type]

autocmd configures vim parameters according to file type

In Vim, you can use autocmd (autocommands) to automatically execute specific functions based on the file type. First, you need to define these functions and then call them using autocmd with file type pattern matching.

The following is an example of a basic Vim configuration that defines two functions in the ~/.vimrc file, one for C type files and one for Python type files. Then it uses autocmd to call the corresponding function based on the file extension.

" 在 ~/.vimrc 文件中定义你的函数

" 函数用于 C 文件的配置 
function! SetupCEnvironment() " C 相关的配置 
	set tabstop=4 
	set shiftwidth=4 
	set softtabstop=4 
	set expandtab 
	" 其他 C 文件类型特定的设置... 
endfunction

" 函数用于 Python 文件的配置
function! SetupPythonEnvironment()
	" Python 相关的配置 
	set tabstop=4 
	set shiftwidth=4 
	set softtabstop=4 
	set noexpandtab 
	" 其他 Python 文件类型特定的设置...
endfunction

" 设置文件类型特定的自动命令 
" 注意:这里的模式 '*.c' 和 '*.py' 与文件后缀名匹配

augroup filetype_c 
	autocmd! 
	autocmd FileType c call SetupCEnvironment() 
augroup END

augroup filetype_python 
	autocmd! 
	autocmd FileType python call SetupPythonEnvironment() 
augroup END

In the above configuration, we first define two functions SetupCEnvironment and SetupPythonEnvironment. These functions contain file type specific settings. Then we define two augroup blocks to wrap the related autocmd. The autocmd! command clears previously defined autocommands from the same group to avoid duplication. FileType event is used to trigger when Vim detects a specific file type.

Note that Vim's file type detection mechanism is usually determined based on the content and extension of the file. Make sure your Vim has enabled file type detection. You can enable it by including the following command in .vimrc:

filetype plugin indent on 

Multiple patterns can be separated by commas to match multiple types of files:

function! SetupCEnv()
        set tabstop=8
        set shiftwidth=8
        set softtabstop=8
        set expandtab

endfunction

function! SetupUsrEnv()
        set tabstop=4
        set shiftwidth=4
        set softtabstop=4
        set expandtab
endfunction

augroup filetype_c
        autocmd!
        autocmd FileType *.c,*.h,*.cpp,*.cc call SetupCEnv()
augroup END

augroup filetype_python
        autocmd!
        autocmd FileType *.py,*.sh,*.csh,*.tcl,*.v call SetupUsrEnv()
augroup END

If the above configuration does not take effect, you can configure it separately, as follows:

augroup filetype_usr
        autocmd!
        autocmd FileType python call SetupUsrEnv()
        autocmd FileType sh call SetupUsrEnv()
        autocmd FileType verilog call SetupUsrEnv()
        autocmd FileType csh call SetupUsrEnv()
        autocmd FileType tcl make call SetupUsrEnv()
        autocmd FileType make call SetupUsrEnv()
augroup END

vim text type

Vim uses filetypes to determine how to enable specific plug-ins, syntax highlighting, and indentation rules for different types of files. Vim supports many file types. Here are some examples of common file types:

  • c: C language source file
  • cpp: C++ language source file
  • java:Java language source file
  • python:Python script
  • sh:Shell script
  • javascript: JavaScript file
  • html:HTML file
  • css: CSS file
  • xml:XML file
  • php:PHP script
  • ruby:Ruby script
  • perl:Perl script
  • json:JSON file
  • yaml: YAML file
  • sql:SQL script
  • markdown or md: Markdown sentence item
  • vim:Vim script
  • tex:LaTeX file
  • make:Makefile
  • conf:Configuration file

To see all the file types supported by Vim, you can consult Vim's documentation or check Vim's filetype.vim file, which defines the mapping of filename patterns to file types. Usually, you can find plug-ins related to these file types in the ftplugin directory of the Vim installation directory.

In Vim, you can use the following command to view the file type of the currently open file:

:set filetype? 

Or use:

:echo &filetype 

Guess you like

Origin blog.csdn.net/sinat_32960911/article/details/135012383