Vim keeps defaulting to soft-tabs when working with .haml files - vim

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

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

Am I writing Vim modelines incorrectly?

I've appended the following Vim modeline to the end of a .md file:
# vim: ts=4:sw=4:softtabstop=4:expandtab:autoindent
I've also tried this:
# vim: ts=4 sw=4 softtabstop=4 expandtab autoindent:
However, if I check a setting like this for either case, I see that the tabstop is still set to 2:
:set ts? " returns 2
Will Vim give greater precedence to any settings in my .vimrc or to ftplugin/markdown.vim? I would think that a modeline inside a file would have the greatest precedence.

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.

Vim autocompletion word with character _ , in python and latex

In vim, I managed to have autocompletion installing Supertab.
When I work on a .py file, it works ok: I can autocomplete xxx_yyy by typing xxTAB (and it pops up options if many xxx_yyy1 xxx_yyy2 exist).
But on a .tex file, if I have already the word xxx_yyy, when I type xxTAB I get the only match xxx.
How can I match xxx_yyy with xxTAB in a .tex file too?
This is my .vimrc :
filetype plugin indent on
syntax on
set backspace=indent,eol,start
autocmd Filetype python setlocal expandtab tabstop=4 shiftwidth=4
set ww=<,>,[,]
SuperTab uses the built-in insert mode completion (:help i_CTRL-N), and that is based on keywords. This setting is filetype-specific, controlled by the 'iskeyword' option. For Python, the _ is included, for Latex, it isn't (and based on #Konrad Rudolph's comment, for a reason).
You can certainly adapt this if it bothers you. In your ~/.vimrc:
autocmd Filetype tex setlocal iskeyword+=_

How to apply VIMRC to my python file?

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

Resources