Am I writing Vim modelines incorrectly? - vim

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.

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 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.

Why doesn't setting ts=4 in .vimrc work?

I have a stupid question but I am stuck.
I do set set ts=4 in my .vimrc file, but it looks not work. Open a new file, tab is still extended as 3 spaces. And under command mode, "set ts" got a "tabstop=3". Is there a final file like .vimrc that overwrite my ts?
Even I type :set ts=4, in vim, when I type tab, it only has 3 spaces in length.
Here is my suspicious options that cause this annoying result in my .vimrc
set autoindent
set shiftwidth=4
set softtabstop=4
set backspace=2
Thanks.
Try this:
:verbose set tabstop?
in vim, it will tell you where the tabstop option value is coming from.

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

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

Vim editor indent problem when the first character of the line is a sharp # character

This haven been bugging me since the first day using Vim for 3 years. Whenever I try to indent a line via Shift + > when the FIRST CHARACTER of the line starts with a "#", it doesn't work at all, regardless of the file types (.php, .txt, etc.). Because # is used for comment in PHP and I also use it for decoration for text files something like:
# This is a comment
### 1. Instruction one
# ------------ this is an sample --------------
I use Vim 7.2 in Ubuntu with the following .vimrc settings
syntax on
set t_Co=256
set incsearch
set hlsearch
set number
set nowrap
set nowrapscan
set ignorecase
set et
set sw=4
set smarttab
set smartindent
set autoindent
set textwidth=0
set noequalalways
set formatoptions=1
set lbr
set vb
set foldmethod=marker
Thanks!
Insert the following in your .vimrc:
set nosmartindent
It is smartindent that causes lines beginning with # to not be indented as you want. You can read more about it by typing :help smartindent. If you use an indenting file for python scripts (or any other syntax), include the following too.
filetype indent on
You can use:
inoremap # X^H#
I think this behaviour is not totally wrong for C/C++, therefore I just change it in python/php.
autocmd FileType python,php inoremap # X^H#
:help smartindent says:
When typing # as the first character in a new line, the indent for
that line is removed, the # is put in the first column. The indent
is restored for the next line.
If you don't want this, use this mapping: :inoremap # X^H#, where ^H is entered with CTRL-V CTRL-H.
When using the >> command, lines starting with # are not shifted right.

Resources