Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 705
Reman Avatar asked Mar 30 '11 13:03

Reman


3 Answers

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
like image 187
Curt Nelson Avatar answered Oct 23 '22 19:10

Curt Nelson


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?

like image 44
Paul Ruane Avatar answered Oct 23 '22 18:10

Paul Ruane


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.

like image 2
Nick Knowlson Avatar answered Oct 23 '22 19:10

Nick Knowlson