How to set vim use tab instead of 4 whitespace? - vim

"(1)use 4 whitespace instead of tab
set cindent
set smartindent
set autoindent
"set softtabstop=4 shiftwidth=4 expandtab smarttab
"(2)use tab instead of 4 whitespace
set cindent
set smartindent
set autoindent
set tabstop=4 shiftwidth=4 noexpandtab
The first settings is 4 whitespace.
Now my team use tab, the second setting work correctly in *.cpp *.h files, but still show 4 whitespace in *.py file.
I don't know why...?

Check this blog entry: http://henry.precheur.org/vim/python.html
Creating a proper file in ftplugin folder will recognize the options for any python file.

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

Neovim Indent Bug?

I have the following options in my .config/nvim/init.vim:
set tabstop=4
set shiftwidth=4
set expandtab
set ai
set smartindent
filetype on
filetype plugin on
filetype indent on
So, the indentation engine should work on all files, but it does not.
When I edit my config file for example, :set ai returns: autoindent and :set si returns: smartindent.
But when I run the same commands on a php file they return:
noautoindent
nosmartindent
EDIT:
The file that was overriding my config is:
/usr/share/nvim/runtime/indent/php.vim
It's the default php indent file.

vim can not set shiftwidth and tabstop with ~/.vim/after/ftplugin/coffeescript.vim

I want to set shiftwidth and tabstop with ~/.vim/after/ftplugin/coffeescript.vim as follow:
setlocal shiftwidth=2
setlocal tabstop=2
but this does not work, I have test with ~/.vim/after/ftplugin/javascript.vim, this does work.
By any chance, did you copy paste the path to your settings file? I noticed that there's a typo in "coffescirpt.vim", is that how the file is spelled?

How do I set my shift width to indent two spaces when scripting for python in MacVim?

Here is my ~/.vimrc file. My indentation works for vim, but does not work for MacVim. Every time I open MacVim and try scripting in python I have to click Edit --> File Settings --> Shiftwidth --> 2
What corrections do I need to make to my ~/.vimrc file? I even considered making an exact replica of my ~/.vimrc file and called it ~/.mvimrc.
1 set nocompatible
2
3 execute pathogen#infect()
4 syntax on
5 filetype plugin indent on
6 set noai ai
7 set nosi si
8 set nosta sta
9 set shiftwidth=2
10 set softtabstop=2
11 set ts=2
12 set expandtab
13 set number
14
15 set statusline+=%#warningmsg#
16 set statusline+=%{SyntasticStatuslineFlag()}
17 set statusline+=%*
18
19 let g:syntastic_always_populate_loc_list = 1
20 let g:syntastic_auto_loc_list = 1
21 let g:syntastic_check_on_open = 1
22 let g:syntastic_check_on_wq = 0
23
"~/.vimrc" [readonly] 31L, 589C
In the past, the Python ftplugin only took care of "generic" stuff but a semi-recent update added indentation settings in an effort to enforce PEP8 compliance:
" As suggested by PEP8.
setlocal expandtab shiftwidth=4 softtabstop=4 tabstop=8
A clean way to override those settings would be to add the lines below to ~/.vim/after/ftplugin/python.vim:
setlocal shiftwidth=2
setlocal softtabstop=2
A few comments about your vimrc:
set nocompatible generally useless, remove this line
set noai ai needlessly complex and ambiguous, use set autoindent
set nosi si smartindent is useless, remove this line
set nosta sta needlessly complex and ambiguous, use set smarttab
set ts=2 useless, 'tabstop' should be left at its default value
Do your plugins and other settings load in macvim, or is it having issues detecting your .vimrc completely? If it's completely not finding it, you might want to check this question and see if you can fix it. If it's only happening to python, is it possible you have a plugin that is changing shiftwidth? If so you may want to look into using after to do it.
If your vimrc is loading, you might want to try doing this to set the shiftwidth to 2 for python files only...
You can use Autocommand to detect that you are editing a .py filetype and change shiftwidth.
autocmd Filetype py setl shiftwidth=2
Above that line, you may need to put this:
filetype on
Put both these in your vimrc to make it work.
Pretty self explanatory, but just to spell out what it does, when it detects a .py file, it sets the shift width to 2. Setl is shorthand for setlocal (to the buffer).

Why is c.vim overriding .vimrc for HTML?

My ~/.vimrc contains the following (default in Ubuntu 14.04, just added the last line):
set smartindent
set tabstop=4
set shiftwidth=4
filetype plugin indent on
In ~/.vim/after/ftplugin/c.vim:
set tabstop=8 softtabstop=8 shiftwidth=8 noexpandtab
I was expecting for only .c and .h files to use 8-column tabs. However, other files are also being afected such as .html files.
How can I make c.vim affect only C-related files?
Change this:
set tabstop=8 softtabstop=8 shiftwidth=8 noexpandtab
to this:
setlocal tabstop=8 softtabstop=8 shiftwidth=8 noexpandtab

Resources