Please forgive me if this has already been answered, but I am wanting to do the following within vim
set tabstop=2 expandtab
However, I only want this to apply to .c and .h files (since I also use makefiles and they need the hard tab ).
My old computer broke that I had this already in the vimrc, and any help would be appreciated
Edit: Changing Vim indentation behavior by file type seems to have answered a similar question, but I cannot find a way to make this work for the .h files unless I am being dumb
I just didn't scroll down enough on this question: Changing Vim indentation behavior by file type
The one that worked for me was
autocmd BufRead,BufNewFile *.c,*.h set ts=2 expandtab
Through the pointers in I got in the comments, I modified my solution to have the autocmd in a group and then to clear out the autocmd before running anything. leaving it as this:
:augroup cAndHeaderFiles
: autocmd!
: autocmd BufRead,BufNewFile *.c,*.h set sts=2 sw=2 expandtab cindent
:augroup END
Im using the Janus Vim distribution as a starting point and I have the following in my .vimrc.after
set autoindent noexpandtab tabstop=2 shiftwidth=2
set guioptions
This has worked well as I want a tab the equivalent of 2 spaces to be used everywhere. Its important to note that I want a tab character and not two actual spaces. So far this has worked great in all the files I've used, with the exeception of .slim files. In this case two spaces are used. I'm using https://github.com/slim-template/vim-slim for slim syntax highlighting.
Here is my question: how do I override whatever settings are applied to slim files and force a two-space sized tab character everywhere?
Perhaps you need to put these in .vimrc instead of .vimrc.after? I have a similar issue that I want to expand tabs normally, but not expand them for makefiles since these must be tabs to work. I have in my main .vimrc
" In general
set expandtab
" Exception for makefiles
au BufNewFile,BufRead Makefile,makefile,nmakefile set noexpandtab
I don't have slim syntax installed, but I'm guessing
au BufNewFile,BufRead *.slim set tabstop=2 noexpandtab
I'm using spf13-vim The Ultimate Vim Distribution. One of the my c files' colorscheme is not working. Now whole file is displaying like a commented block. I could not figure out which of my actions caused this problem. So if anybody has any idea, please share.
set filetype and set syntax is not showing anything.
:set filetype
filetype=
:set syntax
syntax=
I try to get autoindent to work in VIM with html files, but unfortunately it won't work. Autoindent works perfectly with other languages, but it just won't work with html. I've already tried setting the filetype to html, as suggested in this question, but unfortunately it doesn't work either. Also, when I select everything and press = it doesn't indent as well, even though vim reports XX lines indented.
My .vimrc file looks like this:
set autoindent
set cindent
set tabstop=4
set shiftwidth=2
set expandtab
Any suggestions?
You have to add the following lines to your .vimrc to make sure vim uses file-specific indentions:
filetype on
filetype plugin on
filetype indent on
This way it will indent html as html when the filetype is set to html.
I prefer to use tab than white space(may be a little different from most of others)
But I found, when I hit Enter at the end of line, it will add some white spaces, but not tab. So, I have to delete them and press tab.
I want to know how to set vim as:
use only tab to indent the lines
a tab looks like 4-spaces, but actually is a tab
when hit enter at the end of a line, the new line is started with only tabs
I've googled for this for a while, but not found a good answer. Thank you in advance
UPDATE
The answer #Alok has provided works well in most of cases. But I just found, sometimes, it depends on the file type. For example, if you are editing a haml file, and there is a haml.vim in your vimfiles/indent/, then all the tabs will be converted to space. So if you want it to be tab only, you should modify(or delete) the corresponding indent file.
The settings you are looking for are:
set autoindent
set noexpandtab
set tabstop=4
set shiftwidth=4
As single line:
set autoindent noexpandtab tabstop=4 shiftwidth=4
autoindent can be replaced with smartindent or cindent, depending upon your tastes. Also look at filetype plugin indent on.
http://vim.wikia.com/wiki/Indenting_source_code
Late to the discussion, just for anyone who struggled to force the use of tabs. With the current version of vim (>8) it was required to set:
:set noet ci pi sts=0 sw=4 ts=4
which expands to:
:set noexpandtab
:set copyindent
:set preserveindent
:set softtabstop=0
:set shiftwidth=4
:set tabstop=4
We can also use this setup with only specific file types, taking advantage of setlocal:
autocmd FileType c,cpp,java,python setlocal noet ci pi sts=0 sw=4 ts=4
For further reference, see: https://vim.fandom.com/wiki/Indent_with_tabs,_align_with_spaces
In my case set indentexpr= did the trick.
I have found the config files which set the annoying behavior with fd python.vim /. In /usr/share/vim/vim90/indent/python.vim there is the line setlocal indentexpr=python#GetIndent(v:lnum). The function python#GetIndent is defined in /usr/share/vim/vim90/autoload/python.vim. This function returned a weird value which was not a multiple of shiftwidth and tabstop. Therefore vim inserted a tab followed by some spaces.
Now, that I have set indentexpr to an empty value, I think vim falls back to autoindent. smartindent and cindent are turned off. (https://vimhelp.org/indent.txt.html)
Of course set noexpandtab is required, too. I am not sure if any other settings are relevant here.
EDIT: autoindent is not perfect, it does nothing more than indent the new line as much as the previous line.
set smartindent
set cinwords=if,elif,else,try,except,finally,with,for,while,class,def
does not work as expected and cindent does not seem suitable for python, so using indentexpr is the way to go after all.
Luckily the indentation function can be configured, see help ft-python-indent or look at the code – that was more informative regarding the disable_parentheses_indenting feature. It seems this is the solution for me:
if !exists('g:python_indent')
let g:python_indent = {}
endif
let g:python_indent.disable_parentheses_indenting = 1
(https://vi.stackexchange.com/a/38824/43863)