Vim permanent syntax highlighting - vim

I'm using :set syntax=javascript whenever I work on TypeScript files for example. I want to set the syntax automatically based on my file type. Now here's the thing, I've done it before by adding:
au BufNewFile,BufRead,BufReadPost *.ts set syntax=javascript
to my ~/.vimrc file, however, it doesn't seem to be working anymore.
Here is the contents of my .vimrc file. Is there a reason why this wouldn't work?
colorscheme atom-dark-256
set number
set autoindent
set guifont=monaco:h12
set expandtab
set tabstop=4 shiftwidth=4 expandtab
au BufNewFile,BufRead,BufReadPost *.ts set syntax=javascript
au BufNewFile,BufRead,BufReadPost *.handlebars set syntax=HTML
au BufNewFile,BufRead,BufReadPost *.twig set syntax=HTML
au BufNewFile,BufRead,BufReadPost *.theme set syntax=PHP
I'm sure it used to work just fine, but it just stopped for some reason. Any ideas? I'm using MacVim if that helps?

Let us set the filetype of *.ts as javascript so that it applies javascript syntax using
au BufEnter,BufRead *.ts set filetype=javascript

Related

My VIM configuration doesn't work with autocmd plugin

set tabstop=4
set softtabstop=4
set shiftwidth=4
set noexpandtab
set nu
set autoindent
set cindent
set encoding=utf-8
set fileencodings=utf-8,ucs-bom,gb18030,gbk,gb2312,cp936
set termencoding=utf-8
filetype plugin indent on
autocmd Filetype python setlocal expandtab tabstop=4 shiftwidth=4
I added the last two lines into .vimrc but received this error message:
-bash: filetype: command not found
-bash: autocmd: command not found
I haven't used this plugin before and what's the proper way to enable it? My purpose is to avoid the error message when I edit a file with vim on a Linux machine remotely:
TabError: inconsistent use of tabs and spaces in indentation
-bash: filetype: command not found
-bash: autocmd: command not found
These errors seem to be printed by bash,
did you run something like source .vimrc?

don't apply current options to new opened file vim

I frequntly open many files in vim. The problem is some files have their own options. For example:
settings for make files
set noexpandtab tabstop=4 shiftwidth=4 softtabstop=4
set list listchars=eol:¬,tab:→⠀,trail:~,extends:>,precedes:<
hi SpecialKey ctermfg=243
hi NonText ctermfg=243
settings for C files
set colorcolumn=100 tabstop=4 shiftwidth=4 softtabstop=4
So, if I open makefile first and then :tabe main.c I can see invisble character that were turned on from makefile.
I understand that I can :set nolist but imagine if I have many other options inherit from many other files. How can I tell vim not to inherit them when I :tabe anotherFile?
Well there is setlocal to set this things for a buffer only.
But that does of course not work for a .vimrc. There you have two possibilities, autocmds or filetype plugins:
autocmd BufRead,BufNewFile *.{c} setlocal colorcolumnt=100
This does set the settings local to any oppened buffer of a file ending with .c
Or you can add the settings to a filetype-plugin. See :h filetype-plugin for more information on that. Basically just add the settings to a specific file. In your case most likely under .vim/ftplugin/c.vim

Neovim Indent Bug?

I have the following options in my .config/nvim/init.vim:
set tabstop=4
set shiftwidth=4
set expandtab
set ai
set smartindent
filetype on
filetype plugin on
filetype indent on
So, the indentation engine should work on all files, but it does not.
When I edit my config file for example, :set ai returns: autoindent and :set si returns: smartindent.
But when I run the same commands on a php file they return:
noautoindent
nosmartindent
EDIT:
The file that was overriding my config is:
/usr/share/nvim/runtime/indent/php.vim
It's the default php indent file.

vimrc - Syntax highlighting not working

so this is what my .vimrc contains, why is my syntax highlighting not working?
set nocompatible " must be the first line
filetype plugin indent on
syntax on
set laststatus=2
set statusline=%<%f\%h%m%r%=%-20.(line=%l\ \ col=%c%V\ \ totlin=%L%)\ \ \%h%m%r%=%-40(bytval=0x%B,%n%Y%)\%P
set nu
set shortmess=I
set nowrap
set tabstop=2
set backspace=indent,eol,start
set shiftwidth=2
set shiftround
set ignorecase
set smarttab
set hlsearch
set incsearch
set undolevels=1000
set pastetoggle=<F2>
set t_Co=256
colorscheme monokai
let g:user_emmet_leader_key = '<c-y>'
execute pathogen#infect()
autocmd StdinReadPre * let s:std_in=1
autocmd VimEnter * if argc() == 0 && !exists("s:std_in") | NERDTree | endif
autocmd vimenter * NERDTree
My color theme works, but the colors are not showing up properly as syntax highlighting.
Any suggestions?
First, check the output of:
:setlocal syntax?
The correct output (for php files) is:
syntax=php
You can check the php syntax code doing a:
:syntax list
You must see all syntax code with the command above. If you don't get the syntax=php value or don't see any syntax code on :syntax list, probably you don't have a php.vim file in your ~/.vim/ftplugin folder. When you set filetype plugin on and open an php file, what Vim does is to look in your ftplugin folder for a php.vim file and execute it.
php.vim is a system file, and it comes with the default installation. Try to do a :scriptname to see all scripts that are opening with vim. If you can't see a php.vim file, a solution could be install StanAngeloff/php.vim plugin.
If, and only if, you have a php.vim file and see its syntax with :syntax list, then something external is affecting Vim's syntax highlight, and it's probably your terminal.

VIM: Indentation changes when opening Python files

when I open Python files my indentation changes. I have the following vimrc settings:
set number
colorscheme wombat256mod
set guifont=Consolas\ 11
set linespace=2
filetype on
filetype plugin on
au BufRead,BufNewFile *py,*pyw,*.c,*.h set tabstop=4
au BufRead,BufNewFile *.py,*pyw set shiftwidth=4
au BufRead,BufNewFile *.py,*.pyw set expandtab
fu Select_c_style()
if search('^\t', 'n', 150)
set shiftwidth=8
set noexpandtab
el
set shiftwidth=4
set expandtab
en
endf
au BufRead,BufNewFile *.c,*.h call Select_c_style()
au BufRead,BufNewFile Makefile* set noexpandtab
highlight BadWhitespace ctermbg=red guibg=red
au BufRead,BufNewFile *.py,*.pyw match BadWhitespace /^\t\+/
au BufRead,BufNewFile *.py,*.pyw,*.c,*.h match BadWhitespace /\s\+$/
au BufRead,BufNewFile *.py,*.pyw,*.c,*.h set textwidth=79
au BufRead,BufNewFile *.c,*.h set formatoptions-=c formatoptions-=o formatoptions-=r
au BufNewFile *.py,*.pyw,*.c,*.h set fileformat=unix
let python_highlight_all=1
syntax on
filetype indent on
set autoindent
Anybody see something that can cause it?
For example I open an *.html file the indentation is a tab. But once I opened any Python file and go back to the html file the indentation switches to python preferences.
I have an addition to #dash-tom-bang's answer: both shiftwidth and expandtab options are local to buffer. No need to add any autocommands, just change all set statements with setlocal, so it will not alter the defaults.
The au lines with *.py in them are what are changing your settings. Notably,
au BufRead,BufNewFile *.py,*pyw set shiftwidth=4
au BufRead,BufNewFile *.py,*.pyw set expandtab
You need to "undo" these settings when opening HTML files. Just add a few more autocmds that work on *.html to set noexpandtab and set your shiftwidth back to 8.
I would suggest saving http://svn.python.org/projects/python/trunk/Misc/Vim/vimrc at some place and sourcing it from .vimrc. That should save hassles.
EDIT: Oh, well apparently, that's already something you're doing!

Resources