Vim文件类型判断
vim中判断文件的类型也是非常重要的,打开vim后,使用命令::e $VIMRUNTIME/filetype.vim
例如如下片段:
1047 " Markdown 1048 au BufNewFile,BufRead *.markdown,*.mdown,*.mkd,*.mkdn,*.mdwn,*.md setf markdown 1049 1050 " Mason 1051 au BufNewFile,BufRead *.mason,*.mhtml,*.comp setf mason 1052 1053 " Mathematica, Matlab, Murphi, Objective C or Octave 1054 au BufNewFile,BufRead *.m call dist#ft#FTm() 1055
以1053行为例,其中有多种文件的后缀名都是一样的,就无法直接通过后缀名直接判断文件类型;
vim会继续执行代码,调用 ft.vim中的文件,使用命令 :e $VIMRUNTIME/autoload/dist/ft.vim 具体在:/usr/share/vim/vim82/autoload/dist/ft.vim里,下面是判断m后缀名的的具体属性函数
283 func dist#ft#FTm() 284 if exists("g:filetype_m") 285 exe "setf " . g:filetype_m 286 return 287 endif 288 289 " excluding end(for|function|if|switch|while) common to Murphi 290 let octave_block_terminators = '\<end\%(_try_catch\|classdef\|enumeration\|events\|methods\|parfor\|properties\)\>' 291 292 let objc_preprocessor = '^\s*#\s*\%(import\|include\|define\|if\|ifn\=def\|undef\|line\|error\|pragma\)\>' 293 294 let n = 1 295 let saw_comment = 0 " Whether we've seen a multiline comment leader. 296 while n < 100 297 let line = getline(n) 298 if line =~ '^\s*/\*' 299 " /* ... */ is a comment in Objective C and Murphi, so we can't conclude 300 " it's either of them yet, but track this as a hint in case we don't see 301 " anything more definitive. 302 let saw_comment = 1 303 endif 304 if line =~ '^\s*//' || line =~ '^\s*@import\>' || line =~ objc_preprocessor 305 setf objc 306 return 307 endif 308 if line =~ '^\s*\%(#\|%!\)' || line =~ '^\s*unwind_protect\>' || 309 \ line =~ '\%(^\|;\)\s*' .. octave_block_terminators 310 setf octave 311 return 312 endif 313 " TODO: could be Matlab or Octave 314 if line =~ '^\s*%' 315 setf matlab 316 return 317 endif 318 if line =~ '^\s*(\*' 319 setf mma 320 return 321 endif 322 if line =~ '^\c\s*\(\(type\|var\)\>\|--\)' 323 setf murphi 324 return 325 endif 326 let n = n + 1 327 endwhile 328 329 if saw_comment 330 " We didn't see anything definitive, but this looks like either Objective C 331 " or Murphi based on the comment leader. Assume the former as it is more 332 " common. 333 setf objc 334 else 335 " Default is Matlab 336 setf matlab 337 endif 338 endfunc
直至确认成功,还是不错的。
人就像是被蒙着眼推磨的驴子,生活就像一条鞭子;当鞭子抽到你背上时,你就只能一直往前走,虽然连你也不知道要走到什么时候为止,便一直这么坚持着。