Last Updated: February 25, 2016
·
428
· yitsushi

Maintain other's code? Space-Tab hell? Not a problem, I use Vim

Sometimes I need to work on other's codebase. Ok, it happens a little more than sometimes. Some developers use tabs to indent, some others use 3 or 4 spaces. I use 2 spaces.

When I open their codebase I need to change my settings. But I can deal with it. This lines sit at the end of my .vimrc:

" Maintain other's code (shit happening sometimes)
fun MaintainOthersCode(tab, isSoft)
  if a:isSoft
    set expandtab
  else
    set noexpandtab
  endif
  let &tabstop=a:tab
  let &shiftwidth=a:tab
  let &softtabstop=a:tab
endfun

command! -nargs=* MaintainOthersCode call MaintainOthersCode(<f-args>)

What it does for me? When I need to change indentation to tab then I just type :MaintainOthersCode 2 0 that means I want to change my indentation to 2 space length but with tab character. The first parameter defines the length of the indentation and the second one is a boolean like "Do you want to use soft-tabs?"

A few example:

" this is my default: 2 spaces
:MaintainOthersCode 2 1
" 3 spaces
:MaintainOthersCode 3 1
" 4 spaces
:MaintainOthersCode 4 1
" 2 characters length tab
:MaintainOthersCode 2 0
" 3 characters length tab
:MaintainOthersCode 3 0
" 4 characters length tab
:MaintainOthersCode 4 0

These changes are not permanent. When I close vim or open a new one then I get back my preferences.