2

I have a problem with indentation using vim.

Lets say I have a file:

class My
end // <-- cursor is here

When I press O vim will create new line in between class and end and it will be indented. When I add a method def to it so the file looks like this:

class My
  def my
  end
end

Vim inserts tab for the automatic indentation not spaces so in git diff it looks like this:

class My
    def my
    def
end

My .vimrc is very basic:

set nocompatible
filetype off
let mapleader = "\<space>"
set tabstop=2 shiftwidth=2 expandtab " setting up tab behaviour
set number " show line numbers
set relativenumber " make the line numbers relative

set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()

Plugin 'VundleVim/Vundle.vim'
Plugin 'tpope/vim-fugitive'
Plugin 'ctrlpvim/ctrlp.vim'
Plugin 'thoughtbot/vim-rspec'
Plugin 'tpope/vim-surround'
Plugin 'vim-ruby/vim-ruby'

call vundle#end()            " required
filetype plugin indent on    " required

map <Leader>t :call RunCurrentSpecFile()<CR>
map <Leader>s :call RunNearestSpec()<CR>
map <Leader>l :call RunLastSpec()<CR>
map <Leader>a :call RunAllSpecs()<CR>
nmap 0 ^
nmap <leader>vr :tabe $MYVIMRC<cr>
nmap <leader>so :source $MYVIMRC<cr>
nmap <leader>cs :tabe db/schema.rb<cr>
imap jk <esc>
imap kj <esc>

syntax on

Any idea on how to fix this so the automatic indention is two spaces? It drives my nuts! I have to spend so much time fixing indentation after every commit....

4

If you need to use two spaces instead of tab, try

set shiftwidth=2
set tabstop=2
set expandtab 

More on this here on wiki or in help

:h shiftwidth
:h tabstop
:h expandtab 
| improve this answer | |
  • Yes but it is there on line 4 of .vimrc - set tabstop=2 shiftwidth=2 expandtab – Kocur4d Sep 19 '15 at 12:06
  • I am sorry I missed it. – ryuichiro Sep 19 '15 at 12:08
  • np, it is wird. shiftwith should be responsible for autoindent and expandtab should convert tabs to spaces.. – Kocur4d Sep 19 '15 at 12:11
  • crap - it is working now.... I think I didn't source the .vimrc after adding expandtab – Kocur4d Sep 19 '15 at 12:14

Not the answer you're looking for? Browse other questions tagged or ask your own question.