📄 autocmd.txt
字号:
"<afile>". *BufHidden*BufHidden Just after a buffer has become hidden. That is, when there are no longer windows that show the buffer, but the buffer is not unloaded or deleted. NOTE: When this autocommand is executed, the current buffer "%" may be different from the buffer being unloaded "<afile>". *BufCreate*BufCreate Just after creating a new buffer. Also used just after a buffer has been renamed. NOTE: When this autocommand is executed, the current buffer "%" may be different from the buffer being deleted "<afile>". *BufDelete*BufDelete Before deleting a buffer from the buffer list. The BufUnload may be called first (if the buffer was loaded). Also used just before a buffer is renamed. NOTE: When this autocommand is executed, the current buffer "%" may be different from the buffer being deleted "<afile>". *WinEnter*WinEnter After entering another window. Not done for the first window, when Vim has just started. Useful for setting the window height. If the window is for another buffer, Vim executes the BufEnter autocommands after the WinEnter autocommands. *WinLeave*WinLeave Before leaving a window. If the window to be entered next is for a different buffer, Vim executes the BufLeave autocommands before the WinLeave autocommands (but not for ":new"). Not used for ":qa" or ":q" when exiting Vim. *GUIEnter*GUIEnter After starting the GUI succesfully, and after opening the window. It is triggered before VimEnter when using gvim. Can be used to position the window from a .gvimrc file:> :autocommand GUIEnter * winpos 100 50 *VimEnter*VimEnter After doing all the startup stuff, including loading .vimrc files, executing the "-c cmd" arguments, creating all windows and loading the buffers in them. *VimLeavePre*VimLeavePre Before exiting Vim, just before writing the .viminfo file. This is executed only once, if there is a match with the name of what happens to be the current buffer when exiting. Mostly useful with a "*" pattern.> :autocmd VimLeavePre * call CleanupStuff() *VimLeave*VimLeave Before exiting Vim, just after writing the .viminfo file. Executed only once, like VimLeavePre. *FileEncoding*FileEncoding Fires off when you change the file encoding with ':set fileencoding'. Allows you to set up fonts or other language sensitive settings. *TermChanged*TermChanged After the value of 'term' has changed. Useful for re-loading the syntax file to update the colors, fonts and other terminal-dependent settings. Executed for all loaded buffers. *User*User Never executed automatically. To be used for autocommands that are only executed with ":doautocmd".For READING FILES there are three possible pairs of events. Vim uses only onepair at a time:BufNewFile starting to edit a non-existent fileBufReadPre BufReadPost starting to edit an existing fileFilterReadPre FilterReadPost read the temp file with filter outputFileReadPre FileReadPost any other file readNote that the autocommands for the *ReadPre events and all the Filter eventsare not allowed to change the current buffer (you will get an error message ifthis happens). This is to prevent the file to be read into the wrong buffer.Note that the 'modified' flag is reset AFTER executing the BufReadPostand BufNewFile autocommands. But when the 'modified' option was set by theautocommands, this doesn't happen.You can use the 'eventignore' option to ignore a number of events or allevents.==============================================================================6. Patterns *autocmd-patterns*The file pattern {pat} is tested for a match against the file name in one oftwo ways:1. When there is no '/' in the pattern, Vim checks for a match against only the tail part of the file name (without its leading directory path).2. When there is a '/' in the pattern, Vim checks for a match against the both short file name (as you typed it) and the full file name (after expanding it to a full path and resolving symbolic links).Examples:> :autocmd BufRead *.txt set etSet the 'et' option for all text files.> :autocmd BufRead /vim/src/*.c set cindentSet the 'cindent' option for C files in the /vim/src directory.> :autocmd BufRead /tmp/*.c set ts=5If you have a link from "/tmp/test.c" to "/home/nobody/vim/src/test.c", andyou start editing "/tmp/test.c", this autocommand will match.Note: To match part of a path, but not from the root directory, use a '*' asthe first character. Example:> :autocmd BufRead */doc/*.txt set tw=78This autocommand will for example be executed for "/tmp/doc/xx.txt" and"/usr/home/piet/doc/yy.txt". The number of directories does not matter here.Environment variables can be used in a pattern:> :autocmd BufRead $VIMRUNTIME/doc/*.txt set expandtabAnd ~ can be used for the home directory (if $HOME is defined):> :autocmd BufWritePost ~/.vimrc so ~/.vimrc> :autocmd BufRead ~archive/* set readonlyThe environment variable is expanded when the autocommand is defined, not whenthe autocommand is executed. This is different from the command!The pattern is interpreted like mostly used in file names: * matches any sequence of characters ? matches any single character \? matches a '?' . matches a '.' ~ matches a '~' , separates patterns \, matches a ',' { } like \( \) in a |pattern| , inside { }: like \| in a |pattern| \ special meaning like in a |pattern| [ch] matches 'c' or 'h'Note that for all systems the '/' character is used for path separator (evenMS-DOS and OS/2). This was done because the backslash is difficult to usein a pattern and to make the autocommands portable across different systems.==============================================================================7. Filetypes *autocmd-filetypes*Vim can detect the type of file that is edited. This is done by checking thefile name and sometimes by inspecting the contents of the file for specifictext. *:filetype* *:filet*To enable file type detection, use this command in your vimrc:> :filetype onEach time a new or existing file is edited, Vim will try to recognize the typeof the file and set the 'filetype' option. This will trigger the FileTypeevent, which can be used to set the syntax highlighting, set options, etc.Detail: The ":filetype on" command will load the file $VIMRUNTIME/filetype.vim, which defines autocommands for the BufNewFile and BufRead events. If the file type is not found by the name, the file $VIMRUNTIME/scripts.vim is used to detect it from the contents of the file.To add your own file types, see |new-filetype| below.If the file type is not detected automatically, or it finds the wrong type,you can either set the 'filetype' option manually, or add a modeline to yourfile. Example, for in an IDL file use the command:> set filetype=idlor add this |modeline| to the file:> /* vim: set filetype=idl : */You can use the detected file type to set options and install mappings. Forexample, to set the 'tabstop' option for C files to 4:> :autocmd FileType c set tabstop=4Example to install a mapping for Java:> :autocmd FileType java map <F4> /import<CR>NOTE: Although a mapping is installed for a specific file type, it will beused for all buffers. There are no mappings local to a buffer yet.If you have several commands to be executed, it is more convenient to put themin a function. This is faster and more easy to maintain. Example:> autocmd FileType cpp call FT_cpp()> function FT_cpp()> map <F4> a#include ""<Esc>i> set cindent shiftwidth=4 softtabstop=4> endfunctionThe file types are also used for syntax highlighting. If the ":syntax on"command is used, the file type detection is installed too. There is no needto do ":filetype on" after ":syntax on".To disable file type detection, use this command:> :filetype offTo disable one of the file types, add a line in the myfiletypefile, see|remove-filetype|. *new-filetype*If a file type that you want to use is not detected yet, there are two ways toadd it. It's better not modify the $VIMRUNTIME/filetype.vim file. It will beoverwritten when installing a new version of Vim. *myfiletypefile*1. If your file type can be detected by the file name. Create a file that contains autocommands to detect the file type. Example:> " myfiletypefile> augroup filetype> au! BufRead,BufNewFile *.mine set filetype=mine> au! BufRead,BufNewFile *.xyz set filetype=drawing> augroup END Then add a line in your vimrc file to set the "myfiletypefile" variable to the name of this file. Example:> let myfiletypefile = "~/vim/myfiletypes.vim" Your file will then be sourced after the default FileType autocommands have been installed. This allows you to overrule any of the defaults, by using ":au!" to remove any existing FileType autocommands for the same pattern. Only the autocommand to source the scripts.vim file is given later. This makes sure that your autocommands in "myfiletypefile" are used before checking the contents of the file. NOTE: Make sure that you set "myfiletypefile" before switching on file type detection. Thus is must be before any ":filetype on" or ":syntax on" command. *myscriptsfile*2. If your file type can only be detected by inspecting the contents of the file, create a vim script file for doing this. Example:> if getline(1) =~ '^#!.*\<mine\>'> set filetype= mine> endif See $VIMRUNTIME/scripts.vim for more examples. Let's assume you write this file in "~/vim/myscripts.vim". Then set the "myscriptsfile" variable to this file name. Example:> let myscriptsfile = "~/vim/myscripts.vim" The "myscriptsfile" is loaded in $VIMRUNTIME/scripts.vim before the default checks for file types, which means that your rules override the default rules in $VIMRUNTIME/scripts.vim. *remove-filetype*If a file type is detected that is wrong for you, you can remove it bydeleting the autocommand that detects the file type. This is best done byadding a line in your |myfiletypefile|:> augroup filetype> au! BufNewFile,BufRead {pattern}> augroup ENDWhere {pattern} is the matched pattern that you want to be ignored.If the file type is actually detected by a script, you need to avoid that$VIMRUNTIME/scripts.vim does its work. That can be done by setting 'filetype'to an unrecognized name, for example "ignored":> augroup filetype> au! BufNewFile,BufRead {pattern} set ft=ignored> augroup ENDIf you are setting up a system with many users, and you don't want each userto add/remove the same filetypes, consider creating a vimrc file that is usedfor everybody. See |system-vimrc|. *autocmd-osfiletypes*On operating systems which support storing a file type with the file, you canspecify that an autocommand should only be executed if the file is of acertain type.The actual type checking depends on which platform you are running Vimon; see your system's documentation for details.To use osfiletype checking in an autocommand you should put a list of types tomatch in angle brackets in place of a pattern, like this:> :au BufRead *.html,<&faf;HTML> so $VIMRUNTIME/syntax/html.vimThis will match:- Any file whose name ends in `.html'
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -