therealblee
therealblee

Reputation: 461

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

Upvotes: 7

Views: 797

Answers (2)

sarath
sarath

Reputation: 543

I use like this in my .vimrc

filetype plugin indent on
set smartindent
set autoindent
set shiftwidth=4
set expandtab
set tabstop=4
set softtabstop=4

The expandtab will replace the tabs with spaces, I think this will be perfect for coding.

Upvotes: 0

zmo
zmo

Reputation: 24812

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>

Upvotes: 6

Related Questions