VIM: set filetype=txt for every new file [No Name]

倖福魔咒の 提交于 2019-12-09 17:21:28

问题


I tried all possible things to let vim set filetype to 'txt' to all new files I create (in a new tab) but it doesn't work.

This is p.e. what I've read on the web a few times:
au BufRead,BufNewFile *.txt setlocal ft=txt
(to put in _vimrc)
However it doesn't work.

Can anyone help me?


回答1:


The following line, added to your .vimrc, will set the filetype to text if it is not already set.

autocmd BufEnter * if &filetype == "" | setlocal ft=text | endif



回答2:


All files are considered plain text unless you have file-type detection turned on or explicitly set the file-type. However, Vim lets you set the file-type to any old text, so are you absolutely sure it is not working?

:set filetype=banana
:set filetype?
  filetype=banana

Setting the filetype is not going to have any noticable effect unless there is a corresponding file in the ftplugin Vim directory and Vim does not ship with a txt.vim file-type file. You could, of couse, add a txt.vim here but I am not sure what this will gain you over default settings — what special behaviour would you want for text files that you would not want for the default behaviour?

(If you want syntax highlighting (whatever that may mean for text file!) then you will also have to create a txt.vim file in the syntax Vim directory.)

What effect are you trying to achieve?




回答3:


It's actually way simpler than all this. Just put this as one of the first lines in your .vimrc.

set ft=txt

Whenever opening a new file, the filetype will be set to txt. But if you open a file with an known existing type it will still be overridden no problem.



来源:https://stackoverflow.com/questions/5487338/vim-set-filetype-txt-for-every-new-file-no-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!