In Vim, are there shortcuts to set tab widths to 2 and 4?

Tab widths may be different for different languages. Even with a common language, the tab width settings can be different for different projects.

In Vim, are there shortcuts to set tab widths to 2 and 4? To quickly change the tab widths when changing projects.

asked Aug 29, 2016 by anonymous

1 Answer

I am afraid no. But you create shortcuts for setting tab widths in Vim.

Add these lines to your ~/.vimrc:

" set tab width
function SetTab2()
  set shiftwidth=2
  set tabstop=2
  set softtabstop=2
  echo "Set tab to 2 spaces"
endfunction

function SetTab4()
  set shiftwidth=4
  set tabstop=4
  set softtabstop=4
  echo "Set tab to 4 spaces"
endfunction

map t4 :call SetTab4() <CR>
map t2 :call SetTab2() <CR>

Reference: https://github.com/zma/vim-config/blob/master/.vimrc#L331

Then, in your Vim, you can use t2 and t4 to set table to 2 and 4 spaces.

answered Oct 10, 2016 by Eric Z Ma (43,500 points)

Please log in or register to answer this question.

Copyright © SysTutorials. User contributions licensed under cc-wiki with attribution required.
Hosted on Dreamhost

...