vim tab indentation for tex document - vim

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

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

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.

Transitioning to vim. Having issues with indentation

I am recovering from a surgery, thusly, I am transitioning to VIM. As I starting point, I've opted to use the vim-sensible plugin as the foundation of my configuration. Additionally, I've installed the 'vim-rails' and 'vim-ruby' plugins. These are all loaded via Pathogen.
For whatever reason, the plugins are not adjusting indentation settings in accord with filetype. Tabs seem locked to 9 columns. I am able to set them manually, but obviously this is not ideal.
Here is my .vimrc. As you can see, it is very basic.
execute pathogen#infect()
syntax on
filetype plugin indent on
Vim provides the following buffer local options for managing indention: 'softtabstop', 'shiftwidth', 'tabstop', and 'expandtab'. The idea is to set these options for a specific filetype, e.g. ruby either by using an autocommand or using the after ftplugin directory (my preference).
After directory approach
Add the following to your ~/.vim/after/ftplugin/ruby.vim file:
setlocal softtabstop=2
setlocal shiftwidth=2
setlocal tabstop=2
Autocommand approach
Add the following to your ~/.vimrc file:
augroup MyIndentSettings
autocmd!
autocmd FileType ruby setlocal softtabstop=2 shiftwidth=2 tabstop=2
augroup END
Learn more
As you are just starting to vim it is best to learn how to query these options so you can track down future bugs. Querying an option is as simple as :verbose set {option}?, e.g. :verbose set expandtab?.
You may also be interested in Tim Pope's vim-sleuth which heuristically sets indent settings.
For more help see:
:h :set
:h 'softtabstop'
:h 'shiftwidth'
:h 'tabstop'
:h 'expandtab'
:h after-directory
In my vimrc I have the following:
set autoindent
set expandtabs
set shiftwidth=4
set tabstop=4
Actually I just have
set ai et sw=4 ts=4
You can change the specific settings using FileType or BufEnter:
autocmd BufEnter *.py ai et sw=4 ts=4

Vim mixes up indent style when editing different files

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.

How to prevent global defaults from overriding 'filetype=vim' specific settings in .vimrc when automatically sourcing .vimrc?

Context
I have my .vimrc automatically being sourced with:
autocmd! BufWritePost ~/.vimrc source ~/.vimrc
I also set defaults for spacing:
set tabstop=2 softtabstop=2 shiftwidth=2 expandtab
Later on, I use the FileType event to override the spacing default above with filetype-specific spacing:
" Syntax of these languages is fussy over tabs & spaces¬
autocmd FileType make setlocal ts=8 sts=8 sw=8 noexpandtab¬
autocmd FileType yaml setlocal ts=2 sts=2 sw=2 expandtab¬
" Customisations based on house-style (arbitrary)
autocmd FileType sh setlocal ts=4 sts=4 sw=4 expandtab
autocmd FileType vim setlocal ts=4 sts=4 sw=4 expandtab
Problem
The problem I am having is that sourcing my .vimrc will set the default values, but will not trigger the FileType event. This means that when I do a :write to my .vimrc while editting a file with filetype-specific spacing, the filetype-specific spacings get overridden by the defaults!
UPDATE: As pointed out by Ingo, the issue only arises in .vimrc and does not occur in any other files.
Example
As an example, when I am working on my .vimrc, initially the file-specific spacing (ts=4 sts=4 sw=4 expandtab) is enabled, but after I make some arbitrary change in my .vimrc, the default spacing (tabstop=2 softtabstop=2 shiftwidth=2 expandtab) is loaded but my file-specific spacing is NOT loaded (since the FileType event was not triggered by a write to my .vimrc) leaving me with the default spacing!
My question(s)
How can I override the default spacing with filetype specific spacings and preserve these even when auto-sourcing .vimrc?
If possible, I would like to accomplish this without getting rid of the autocmd's. Is that possible?
My hunch is that I should somehow manually trigger the FileType event when sourcing .vimrc Can this be done and is this the recommended approach?
Though you've very clearly described the problem, I doubt that this is happening.
As you're correctly using :setlocal for the filetype-specific settings, and :set in your ~/.vimrc, the sourcing of the latter will only affect the global defaults (and the current buffer, which is your .vimrc during the BufWritePost event). To affect other buffers, there would need to be a :bufdo command in your .vimrc.
You can check this with
:verbose set ts? sts? sw? et?
To avoid that your .vimrc settings are overridden with the global settings, you can append this:
if expand('%:t') ==# '.vimrc'
setlocal filetype=vim
endif
I ended up solving this issue by grouping the sourcing autocmd with another autocmd with the same event
" Reload changes to .vimrc automatically
autocmd BufWritePost ~/.vimrc source ~/.vimrc | setlocal filetype=vim
I had tried this earlier, but I was also using the AirlineRefresh command for vim-airline as such:
autocmd BufWritePost ~/.vimrc source ~/.vimrc | AirlineRefresh | setlocal filetype=vim
which was giving me this error:
E488: Trailing characters: AirlineRefresh | setlocal filetype=vim
Switching the order of AirlineRefresh and set local filetype=vim fixed the error and results in the desired behavior:
autocmd BufWritePost ~/.vimrc source ~/.vimrc | setlocal filetype=vim | AirlineRefresh
:bufdo e will do it but I couldn't just add it to my .vimrc. It will also remove syntax highlighting which is not so good.
:h bufdo execute the command in each buffer in the buffer list
:h :e re-edit the current file

Resources