Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adaptive tabbing in vim

I happen to work on code in which some modules use tabs for indentation while others use spaces. Many text editors such as Np++ has some sort of an adaptive tabbing feature, which use spaces for indentation if the previous line (or block of code) use spaces, or tabs as the case may be.

I haven't seen anything in vim like this. Is there any such plugin or setting for this?

like image 433
R.D. Avatar asked Aug 05 '11 00:08

R.D.


People also ask

How do I set tab to 4 spaces in vi?

Within Vim, type a colon and then "set tabstop=4" which will set the tabs to display as four spaces. Hit colon again and type "set expandtab" which will insert spaces for tabs.

How do I enable tabs in Vim?

Opening a tab There are a few ways to do this. Probably the easiest to remember is to run the :tabnew command while in normal mode. This will open a new tab with an empty buffer. If you want to edit a file in the new tab, you can run :tabnew filename and Vim will load the file in the new tab.

How do I use spaces instead of tabs in Vim?

To easily change a tab-based indent to use spaces instead when 'noexpandtab' is set, you can temporarily set 'expandtab' and use :retab with a range. For example, to convert only the current line to use spaces, use :. retab .

What is tabstop in Vim?

tabstop. This setting tells Vim how many columns a tab should be made up of in the editor view, it takes care only of how tabs will be rendered and has no effect on the actual text.


1 Answers

I perfer to set my enviroment up like the below example demonstrates. I make a general rule of replacing tabs with spaces and use augroup when I need to override that rule. Makefiles are a good example of when you may require TABS and a cpp file is when you may require spaces.

" A tab produces a 4-space indentation
:set softtabstop=4
:set shiftwidth=4
:set expandtab
" replace tabs with spaces unless noted otherwise

" <snip>

augroup CPPprog
   au!
   "-----------------------------------
   " GENERAL SETTINGS
   "-----------------------------------
   au BufRead,BufNewFile,BufEnter             *.cpp,*.c,*.h,*.hpp   set nolisp
   au BufRead,BufNewFile,BufEnter             *.cpp,*.c,*.h,*.hpp   set filetype=cpp
   au FileType                                *                     set nocindent smartindent
   au FileType                                *.c,*.cpp             set cindent
   au BufRead,BufNewFile,BufEnter             *.cpp                 let g:qt_syntax=1
   " turn on qt syntax highlighting (a plugin)
   au BufNewFile,BufRead,BufEnter             *.c,*.h,*.cpp,*.hpp   let c_space_errors=1
   " trailing white space and spaces before a <Tab>

   " <snip>

augroup END

" <snip>

augroup filetype
  au! BufRead,BufNewFile,BufEnter *Makefile*,*makefile*,*.mk set filetype=make
augroup END
" In Makefiles, don't expand tabs to spaces, since we need the actual tabs
autocmd FileType make set noexpandtab
like image 193
John Kaul Avatar answered Oct 10 '22 07:10

John Kaul