Vim mixes up indent style when editing different files - vim

I am using the nerdtree with some other plugins.
When I edit files with different indent settings (e.g. Makefile which requires tabs and *.js which should have spaces) then vim mixes up both and applies tabs to *.js.
The file type stuff is defined in these lines:
" use real tabs for these languages
autocmd FileType go,make
\ set noexpandtab
" use two spaces for these languages
autocmd FileType ruby,html,json,jade,javascript
\ set tabstop=2 |
\ set shiftwidth=2
" use four spaces for these languages
autocmd FileType css
\ set tabstop=4 |
\ set shiftwidth=4
Any idea how to make vim to reapply autocmd Filetype when opening another tab in nerdtree?
Bodo

You should use setlocal instead of set to prevent your settings from leaking to other buffers.

Related

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

vim tab indentation for tex document

I am trying to configure my vimrc so that tabs are 2 spaces when editing tex documents.
In my vimrc file, I have
au BufNewFile,BufRead *.py:
\ set tabstop=4
\ set softtabstop=4
\ set shiftwidth=4
\ set textwidth=79
\ set expandtab
\ set autoindent
\ set fileformat=unix
au BufNewFile,BufRead *.js, *.html, *.css, *.tex:
\ set tabstop=2
\ set softtabstop=2
\ set shiftwidth=2
However, when I edit a document in vim, it ignores the tabstop. Tabs are much longer than 2 spaces. Vim does not throw out any errors. I do not understand why it ignores the tabstop=2 line when I am editing a tex document. I have to run :set tabstop=2 while editing.
Can anyone see how my configuration file is wrong?
In case this is relevant, I am using the vimtex plugin.
Thank you.
The file globs need to be separated by commas without whitespace in between; cp. :help autocmd-patterns.
Drop the : colon after the pattern.
Instead of :set, better use :setlocal, otherwise the indent settings will be inherited to any new buffer that is created from within one of those.
You can combine the arguments of all :set command into one.
au BufNewFile,BufRead *.js,*.html,*.css,*.tex
\ setlocal tabstop=2 softtabstop=2 shiftwidth=2
Instead of the :autocmd, I would prefer to put this into ~/.vim/after/ftplugin/{filetype}.vim. (This requires that you have :filetype plugin on; use of the after directory allows you to override any default filetype settings done by $VIMRUNTIME/ftplugin/{filetype}.vim.) The small downside is that you have to duplicate the :setlocal commands for every filetype, but to me, the fact that you would like to have the same indent settings for those languages is accidental, and there's no connection between those configurations.
Alternatively, you could define an :autocmd FileType {filetype} ... directly in your ~/.vimrc. With this, you at least wouldn't duplicate the built-in filetype detection and the file glob patterns used by it:
au FileType javascript,html,css,tex
\ setlocal tabstop=2 softtabstop=2 shiftwidth=2

VIM ignores my tab-related settings in ~/.vimrc (and paste is not set)

Here's my ~/.vimrc on some machine:
set softtabstop=0 noexpandtab nosmarttab
set shiftwidth=4
set tabstop=4
colorscheme murphy
... but when I edit files, tabs show up as 8 spaces, and typing in a tab produces for spaces. I looked around and found this question:
set expandtab in .vimrc not taking effect
but I don't have set paste in my ~/.vimrc, so that can't be the problem. What's causing this? And how can I enforce my tab preferences?
Edit: It seems my settings are only ignored for some filetypes, not for simple text files. How can I force my settings to apply to all filetypes? Or at least to specific types?
Solution for a specific file type (rather than for all file types):
For file type foo Add the following to your ~/.vimrc:
augroup foo
autocmd!
autocmd FileType foo setlocal softtabstop=0 noexpandtab nosmarttab shiftwidth=4 tabstop=4
augroup END
and replace foo with the file type in both occurrences.
Note: foo is not necessarily a file extension. For example, the Python language (.py typically), file type is python.

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.

Autocommands for Matlab in vim?

I use several different programming languages every day, and I'd like to have different tab widths (in spaces) for each. For example: I use the "standard" 2 spaces for Ruby, but all our existing Matlab code uses 4 spaces.
I have this from my personal ~/.vimrc:
augroup lang_perl
au!
set tabstop=4 " tabstop length N in spaces
set shiftwidth=4 " make >> and friends (<<, ^T, ^D) shift N, not the default 8
set expandtab " Use spaces instead of tabs
augroup END
augroup lang_ruby
au!
set tabstop=2 " tabstop length N in spaces
set shiftwidth=2 " make >> and friends (<<, ^T, ^D) shift N, not the default 8
set expandtab " Use spaces instead of tabs
augroup END
Those work, but the following doesn't:
augroup lang_matlab
au!
set tabstop=4 " tabstop length N in spaces
set shiftwidth=4 " make >> and friends (<<, ^T, ^D) shift N, not the default 8
set expandtab " Use spaces instead of tabs
augroup END
I really don't understand how augroup lang_ruby figures out that I'm editing a Ruby file. (My searches brought up ftdetect, but the solution wasn't obvious.) It doesn't seem like vim knows that I'm editing Matlab using augroup lang_matlab. What do I change to make this work?
If you want to have a large number of settings for {filetype}, you should put them into ~/.vim/ftplugin/{filetype}.vim or into file that matches ~/.vim/ftplugin/{filetype}/**/*.vim (examples: ~/.vim/ftplugin/ruby/foo.vim, ~/.vim/ftplugin/ruby/foo/bar.vim). In this case you don't need any autocommands at all. If you still want to use autocommands, use the following:
augroup lang_matlab
autocmd!
autocmd FileType matlab setlocal ts=4 sw=4 et
augroup END
. Note two things: FileType event (it is there and it is not BufRead,BufNewFile) and setlocal instead of plain set. First is intended to be used for filetype settings, second is how buffer-specific options must be set.
About why perl and ruby settings work and why matlab settings does not: your example code is just the same as
augroup lang_perl
autocmd!
augroup END
augroup lang_ruby
autocmd!
augroup END
set tabstop=4 " tabstop length N in spaces
set shiftwidth=4 " make >> and friends (<<, ^T, ^D) shift N, not the default 8
set expandtab " Use spaces instead of tabs
set tabstop=2 " tabstop length N in spaces
set shiftwidth=2 " make >> and friends (<<, ^T, ^D) shift N, not the default 8
set expandtab " Use spaces instead of tabs
So, you effectively set ts=2 sw=2 et. But the $VIMRUNTIME/ftplugin/perl.vim contains the following code:
setlocal tabstop=4
setlocal shiftwidth=4
so, ts=4 sw=4 for perl is set into ftplugin/perl.vim, not in your vimrc (if you have installed perl-support plugin). You can check it by replacing tabstop=4 with tabstop=8 in vimrc.
I cannot tell you how it's determined that lang_perl or lang_ruby shall be read. But looking at the autocommand documentation brings up an example for gzipped files (:he gzip-example):
augroup gzip
autocmd!
autocmd BufReadPre,FileReadPre *.gz set bin
autocmd BufReadPost,FileReadPost *.gz '[,']!gunzip
autocmd BufReadPost,FileReadPost *.gz set nobin
autocmd BufReadPost,FileReadPost *.gz execute ":doautocmd BufReadPost " . expand("%:r")
autocmd BufWritePost,FileWritePost *.gz !mv <afile> <afile>:r
autocmd BufWritePost,FileWritePost *.gz !gzip <afile>:r
autocmd FileAppendPre *.gz !gunzip <afile>
autocmd FileAppendPre *.gz !mv <afile>:r <afile>
autocmd FileAppendPost *.gz !mv <afile> <afile>:r
autocmd FileAppendPost *.gz !gzip <afile>:r
augroup END
See it? Every autocmd that gets specified carries an Event (BufReadPre, FileReadPre) and the extension for which it shall be executed (*.gz). This is backing up my hitherto opinion that you can use any name for your augroup but have to specify an extension that the augroup shall be responsible for in order to get it working correctly.
Another quote of :he autocmd-groups:
Normally, when executing autocommands automatically, Vim uses the autocommands
for all groups. The group only matters when executing autocommands with
":doautocmd" or ":doautoall", or when defining or deleting autocommands.
So, in order to make an augroup for matlab files, this one should do the trick:
augroup lang_matlab
autocmd!
autocmd BufRead *.m set ts=4 sw=4 expandtab
augroup END
And make sure that these settings don't get overwritten by any augroups that do something unconditional (i.e. modify the other augroup definitions too).
Use ftplugins, configuring vim will be much more easier to achieve.
See gVim and multiple programming languages

Resources