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
Related
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.
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
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
I'm using Ubuntu13.10 My $HOME is /home/mario.
I execute vim in the terminal and used :e $MYVIMRC to set up my own preference.
I've test it inside that VIMRC file by typing :so $MYVIMRC, it works!
But later when I changed my directory to /home/mario/LPTH and use vim ex45.py to open my python exercises.
Then I found I can't apply it to my python file by typing :so $MYVIMRC or :source $MYVIMRC
I even restart my computer, still can't work.
What happend? How can I apply the VIMRC to my python docuemnt?
Thanks for your help.
---------------------------addtional information part 1---------------------------------
Yesterday when I raised up this question, I've already set several configurations. such as:
set number
set expandtab
set tabstop=8
set shiftwidth=4
set softtabstop=4
set autoindent
:syntax on
I can apply my configurations for this MYVIMRC file. which shows code numbers,for example. But when I open my python files in ~/LPTH. nothing happens. I checked this morning, still the same.
--------------------addtional information part2--------------------------
Obstacle hacked!!!!!
Previously: I followed tutor from http://vim.wikia.com/wiki/Vimrc,my execution is:
cd ~ ---> vim ---> :e $MYVIMRC ---> add my setting.
And it caused the problem.
This morning: I did the following executions.
cd ~ ---> vim .vimrc ---> add my setting.
And it works. Just that simple.
Thank you guys for your help
You don't (need to) apply your .vimrc to a file. The configuration and customization commands that you put in there are automatically executed by Vim on startup. You should only put general stuff like :set listchars=... backupdir=... in there, and any general :mappings.
Some settings are specific to a programming language like Python; Vim has buffer-local options (like :setlocal expandtab) and mappings; these can be set by filetype plugins. Some people define those via :autocmd FileType python setlocal ... directly in their ~/.vimrc, but it's better to just configure :filetype plugin on there and then place the specific options in ~/.vim/after/ftplugin/python.vim, for example.
As pointed out already you dont need to apply your .vimrc to a file. You just need to have your .vimrc file in your $HOME directory, it will automatically be executed by vim.
This is the .vimrc i would reccomend for Python
set nobackup
set nowritebackup
set noswapfile
set lines=40
set columns=80
set tabstop=4
set shiftwidth=4
set softtabstop=4
set autoindent
set smartindent
set smarttab
set retab
set autowrite
set autochdir
filetype indent on
filetype on
filetype plugin on
For some reason, Vim keeps using soft-tabs (tabs as spaces) everytime I'm working on Haml files. I prefer regular tabs and have the following in my vimrc:
set autoindent
set noexpandtab
set tabstop=4
set shiftwidth=4
Which seems to work great for every file type except for Haml. When editing Haml files Vim uses two spaces rather than a tab, any suggestions as to how this can be reverted back to my default settings (ie. regular tabs)?
It was because the indent script for haml sets expandtab for you automatically when VIM has detected the current file is in haml format. The script is located at $VIMRUNTIME/indent/haml.vim. It contains:
setlocal autoindent sw=2 et
To disable that, you can put this line in your ~/.vimrc to clear the expandtab setting again:
au! FileType haml set noet