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.
Related
I've installed a syntax file at ~.vim/after/syntax/antlr4.vim. And it works great when I manually open a file and then type in:
:set ft=antlr4
However, for whatever reason I cannot get it to recognize the file in my .vimrc. Here is what I currently have, but it seems to be doing nothing:
" Antlr4 highlighting
au BufRead,BufNewFile *.g4 set filetype=antlr4
Move this line:
au BufRead,BufNewFile *.g4 set filetype=antlr4
into that file:
~/.vim/ftdetect/antlr4.vim
See :help ftdetect.
I used to be able to use ctrl-v and Shift-I to do multi line insert. I think it MIGHT have been caused by installing gvim? I have spent hours trying to fix this. I have tried running without plugins and removing .vimrc
my .vimrc
execute pathogen#infect()
syntax on
filetype plugin indent on
set nocompatible
set tabstop=2 "Set size of tab to 2 spaces
set shiftwidth=2
set expandtab "Turns tab into spaces
set autoindent "Automatically indents to the same indent next line
set smarttab "return on a tabbed line returns to that tabbed position
set number
set showcmd "shows last command typed
set wildmenu "Autocomplete menu that pops up at bottom
set showmatch
""SEARCH""
set incsearch "search as characters are entered
set hlsearch "highlights all words
""PASTE MODE""""""""""""""""""""""""""""
nnoremap <F2> :set invpaste paste?<CR>
set pastetoggle=<F2>
set showmode
""SET COLORS"""""
colo solarized
set background=dark
augroup filetypedetect
au BufRead,BufNewFile *.hbs setfiletype html
" associate *.hbs with html filetype
augroup END
Global vimrc
runtime! debian.vim
if has("syntax")
syntax on
endif
if filereadable("/etc/vim/vimrc.local")
source /etc/vim/vimrc.local
endif
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
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
Vim is not autoindenting the C source files I am working on, although it claims both the autoindent and cindent options are enabled when I type the
:set
command.
Nothing is happening when I type in some code. For instance writing
int main()
{
return 0;
}
the "return 0;" statement stays on the left.
However if I type the "=G" command, my file gets indented.
Here is my config:
ubuntu 13.04
vim 7.3.547 + vim-scripts
vimrc is splitted into /etc/vim/vimrc and ~/.vimrc. The concatanated content is as follow:
runtime! debian.vim
if has("syntax")
syntax on
endif
set background=dark
" Uncomment the following to have Vim jump to the last position when
" reopening a file
if has("autocmd")
au BufReadPost * if line("'\"") > 1 && line("'\"") <= line("$") | exe "normal! g'\"" | endif
endif
if has("autocmd")
filetype plugin indent on
endif
set showcmd
set showmatch
if filereadable("/etc/vim/vimrc.local")
source /etc/vim/vimrc.local
endif
""""""" now this is ~/.vimrc """""
set runtimepath+=,/usr/share/vim-scripts
set autoindent
set noexpandtab
" create ~<file> when saving modifications to <file>
set backup
" preserve source's format when pasting
set paste
" disable mouse usage
set mouse=
" colors
set t_Co=256
colorscheme mustang
set hlsearch
set number
set cursorline
if has("statusline")
hi User1 ctermbg=red cterm=bold,reverse
hi User2 ctermbg=darkblue cterm=bold,reverse
hi User3 ctermbg=darkred cterm=bold,reverse
hi User4 ctermbg=brown cterm=bold,reverse
set laststatus=2
set statusline=%h%f\ %y\ %1*%r%*%1*%m%*%=[col:%2*%c%*]\ [line:%3*%.6l%*/%4*%.6L%*\ -\ %p%%]
endif
set spellsuggest=5
match Error /\s\+$/
Do you have any idea ?
Thank you very much for your help.
Pierre
You should have read :help paste before adding set paste to your ~/.vimrc:
When the 'paste' option is switched on (also when it was already on):
... skipped ...
- 'autoindent' is reset
... skipped ...
These options keep their value, but their effect is disabled:
... skipped ...
- 'cindent'
'paste' is very toxic and should never be added to one's ~/.vimrc. See :help pastetoggle and/or use p instead.
Some information:
autoindent does nothing more than copy the indentation from the previous line, when starting a new line. It can be useful for structured text files, or when you want to control most of the indentation manually, without Vim interfering.
autoindent does not interfere with other indentation settings, and some file type based indentation scripts even enable it automatically.
smartindent automatically inserts one extra level of indentation in some cases, and works for C-like files. cindent is more customizable, but also more strict when it comes to syntax.
smartindent and cindent might interfere with file type based indentation, and should never be used in conjunction with it.
When it comes to C and C++, file type based indentations automatically sets cindent, and for that reason, there is no need to set cindent manually for such files. In these cases, the cinwords, cinkeys and cinoptions options still apply.
Generally, smartindent or cindent should only be set manually if you're not satisfied with how file type based indentation works.
If you plan on using file type based indentation, don't set smartindent or cindent. You may still set autoindent, since it doesn't interfere.