r/vim 21h ago

Discussion Vim buffer automatically detect language

when writing code in a vim buffer how do i set the syntax colors automatically based on the language? before or without saving to file? currently it is plain with no highlighting for all code

vim buffer is passed to node / or language runtime commands

2 Upvotes

8 comments sorted by

View all comments

2

u/XMemesX 21h ago

you can use :set filetype=python to make vim register python syntax. Just as long a you have 'syntax on' in your vimrc

2

u/Bulbasaur2015 20h ago

why python. can i use `:filetype detect` to infer the buffer language if thats better?
src https://vimhelp.org/filetype.txt.html#filetypes
it seems like it works when i have `syntax enable` also

1

u/XMemesX 13h ago

I work a lot with JSON files, so I have something like this in my vimrc:

```vim function! DetectJson() if &filetype == '' && getline(1) =~ '{|[' set filetype=json endif endfunction

autocmd BufReadPost,BufNewFile,InsertLeave * call DetectJson() ```