Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Autoindenting not working in vim?

I am currently using vim and wish to autoindent every time I need to (such as after a brace in javascript or after a colon in Python). For some reason, I tried autoindent and smartindent, but every new line gave me 2 tabs instead of one. Why is it doing this? I thought it would only insert one tab.

My current ~/.vimrc file has:

    set ts=4
    set autoindent
    set smartindent
    filetype plugin indent on
like image 323
therealblee Avatar asked Mar 10 '14 15:03

therealblee


1 Answers

you need to set up as well:

:set shiftwidth=4 softtabstop=4

tabstop is only for the number of columns a real "\t" tab takes:

:he shiftwidth

    Number of spaces to use for each step of (auto)indent.  Used for
    |'cindent'|, |>>|, |<<|, etc.
    When zero the 'ts' value will be used.

:he softtabstop

    Number of spaces that a <Tab> counts for while performing editing
    operations, like inserting a <Tab> or using <BS>.  It "feels" like
    <Tab>s are being inserted, while in fact a mix of spaces and <Tab>s is
    used.

whereas tabstop:

 :he tabstop

    Number of spaces that a <Tab> in the file counts for.  Also see
    |:retab| command, and 'softtabstop' option.

As a bonus, here's a few mappings I've set up for that, when I need to work on projects that do not use my favorite default of expand tabs with 4 spaces indent:

nmap <Leader>t2 :set expandtab tabstop=2 shiftwidth=2 softtabstop=2<CR>
nmap <Leader>t4 :set expandtab tabstop=4 shiftwidth=4 softtabstop=4<CR>
nmap <Leader>t8 :set expandtab tabstop=8 shiftwidth=8 softtabstop=4<CR>
nmap <Leader>T2 :set noexpandtab tabstop=2 softtabstop=2 shiftwidth=2<CR>
nmap <Leader>T4 :set noexpandtab tabstop=4 softtabstop=4 shiftwidth=4<CR>
nmap <Leader>T8 :set noexpandtab tabstop=8 softtabstop=8 shiftwidth=8<CR>
like image 78
zmo Avatar answered Oct 12 '22 23:10

zmo