Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use only tab (not space) in vim

I prefer to use tab than white space(may be a little different from most of others)

But I found, when I hit Enter at the end of line, it will add some white spaces, but not tab. So, I have to delete them and press tab.

I want to know how to set vim as:

  1. use only tab to indent the lines
  2. a tab looks like 4-spaces, but actually is a tab
  3. when hit enter at the end of a line, the new line is started with only tabs

I've googled for this for a while, but not found a good answer. Thank you in advance


UPDATE

The answer @Alok has provided works well in most of cases. But I just found, sometimes, it depends on the file type. For example, if you are editing a haml file, and there is a haml.vim in your vimfiles/indent/, then all the tabs will be converted to space. So if you want it to be tab only, you should modify(or delete) the corresponding indent file.

like image 209
Freewind Avatar asked Sep 10 '10 06:09

Freewind


People also ask

How do I use tabs instead of spaces 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 .

How do I use tab instead of space?

So, at the end of the day, tabs versus spaces is truly a matter of preference, however the tab is still the character specifically designed for indentation, and using one tab character per indentation level instead of 2 or 4 spaces will use less disk space / memory / compiler resources and the like.

How do I insert a tab in Vim?

While in insert mode or command mode (the : prompt at the bottom of the editor), type CTRL + V then TAB . Using CTRL + V signals Vim that it should take the next character literally. Even in insert mode.

How do I indent 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 . )


1 Answers

The settings you are looking for are:

set autoindent set noexpandtab set tabstop=4 set shiftwidth=4 

As single line:

set autoindent noexpandtab tabstop=4 shiftwidth=4 

autoindent can be replaced with smartindent or cindent, depending upon your tastes. Also look at filetype plugin indent on.

http://vim.wikia.com/wiki/Indenting_source_code

like image 148
Alok Singhal Avatar answered Sep 20 '22 08:09

Alok Singhal