ChatGPT解决这个技术问题 Extra ChatGPT

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:

use only tab to indent the lines a tab looks like 4-spaces, but actually is a tab 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.

Instead of modifying installed files, try putting your overriding settings in $HOME/vimfiles/after/indent/haml.vim. See :help after-directory for more info.
It is common to have different tab/space preferences for different filetypes. Check out my screencast over at Vimcasts.org, which shows how to set whitespace preferences for different file types. This solution uses your vimrc, but if you want to set up lots of different filetypes, you might prefer to use the after-directory, as suggested by Nefrubyr.
The /after/indent trick also works to get rid of those pesky spaces in python files, the offending file to look at is ftplugin/python.vim, just copy the line with setlocal and copy it to a blank file as suggested by @Nefrubyr, edit it and restore sanity.

o
oddRaven

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


As @freewind said, it does no work well when working with .haml files. In my case I use vim janus and I just add the suggested single line but it does not work for haml files. Any idea?
Add softtabstop=-1, too.
If you are using neovim have a look on :help option because at least "autoindent and noexpandtab" are default options.
Is there some quick way for alternating back and forth between using tabs or spaces? Should I open a new SO quest?
w
wiredolphin

Late to the discussion, just for anyone who struggled to force the use of tabs. With the current version of vim (>8) it was required to set:

:set noet ci pi sts=0 sw=4 ts=4

which expands to:

:set noexpandtab
:set copyindent
:set preserveindent
:set softtabstop=0
:set shiftwidth=4
:set tabstop=4

We can also use this setup with only specific file types, taking advantage of setlocal:

autocmd FileType c,cpp,java,python setlocal noet ci pi sts=0 sw=4 ts=4

For further reference, see: https://vim.fandom.com/wiki/Indent_with_tabs,_align_with_spaces