Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Smart tabs in vim

Tags:

vim

Is there any way to make vim handle tab pressing by the next two ways?

  1. Insert usual tab for indent.
  2. Insert spaces for alignment.

Result of usage should looks as follows (---> it's tabs, _ it's spaces)

foo() {
--->int_a_____=_2;
--->int_count_=_12;
}
like image 637
Nelson Tatius Avatar asked Sep 28 '12 18:09

Nelson Tatius


People also ask

How do I set tabs in Vim?

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. You can put these settings in a . vimrc (or _vimrc on Windows) in your home directory, so you only have to type them once.

How do I visualize a tab in Vim?

A quick way to visualize whether there is a Tab character is by searching for it using Vim's search-commands : In NORMAL mode, type /\t and hit <Enter> . It will search for the Tab character ( \t ) and highlight the results.

How do I indent with tabs in Vim?

To indent the current line, or a visual block: ctrl-t, ctrl-d - indent current line forward, backwards (insert mode) visual > or < - indent block by sw (repeat with . ) then try hitting the F5 key while in insert mode (or just :set paste ).

How do you add a tab space in Vim?

With both configurations in place, Vim will be using spaces anytime you hit <Tab> so, if you need to insert a real Tab character you may: On INSERT mode press <Ctrl> + v + <Tab> .


1 Answers

There is the Smart Tabs plugin. I don't use it, but it looks like it does what you want.

Edit: Actually, I have written my own implementation of this, because I wanted some additional features (like still use Tabs after a comment prefix), and needed it to integrate with my own variant of the SuperTab plugin, which also overrides the key. I've just uploaded my IndentTab plugin, so that you have a choice.

like image 139
Ingo Karkat Avatar answered Oct 11 '22 18:10

Ingo Karkat