乐闻世界logo
搜索文章和话题

How to use Vim's autocommand feature?

2月21日 15:12

Vim's autocommand feature allows automatically executing commands when specific events occur. Defining autocommands: :autocmd [event] [pattern] [command]. Common events: BufRead for reading buffer, BufWrite for writing buffer, BufEnter for entering buffer, BufLeave for leaving buffer, FileType for file type detection, VimEnter for Vim startup, VimLeave for Vim exit, CursorHold for cursor hold. Autocommand groups: :augroup [group] to start group, :autocmd! [group] to clear commands in group, :augroup END to end group. Examples: :autocmd FileType python setlocal tabstop=4 to set indentation for Python files, :autocmd BufWritePost *.py !python % to run Python file after saving. The autocommand feature can implement file-type specific configuration, automatic formatting, automatic compilation, etc., and is an important tool for Vim automation.

标签:Vim