My current setting assumes 8 spaces; how could I redefine it?
It depends on what you mean. Do you mean:
you want tab characters in your file to appear 4 character cells wide?
you want the tab key to generate an indent consisting of 4 space characters
Depending on which behavior you need, one of the following sets of settings should work:
If you want tab characters in your file to appear 4 character cells wide:
set tabstop=4
If your code requires use of actual tab characters these settings prevent unintentional insertion of spaces (these are the defaults, but you may want to set them defensively):
set softtabstop=0 noexpandtab
If you also want to use tabs for indentation, you should also set shiftwidth to be the same as tabstop:
set shiftwidth=4
To make any of these settings permanent add them to your vimrc.
If you want pressing the tab key to indent with 4 space characters:
First, tell vim to use 4-space indents, and to intelligently use the tab key for indentation instead of for inserting tab characters (when at the beginning of a line):
set shiftwidth=4 smarttab
If you'd also like vim to only use space caharacters, never tab characters:
set expandtab
Finally, I also recommend setting tab stops to be different from the indentation width, in order to reduce the chance of tab characters masquerading as proper indents:
set tabstop=8 softtabstop=0
To make any of these settings permanent add them to your vimrc.
More Details
In case you need to make adjustments, or would simply like to understand what these options all mean, here's a breakdown of what each option means:
tabstop
The width of a hard tabstop measured in "spaces" -- effectively the (maximum) width of an actual tab character.
shiftwidth
The size of an "indent". It's also measured in spaces, so if your code base indents with tab characters then you want shiftwidth to equal the number of tab characters times tabstop. This is also used by things like the =, > and < commands.
softtabstop
Setting this to a non-zero value other than tabstop will make the tab key (in insert mode)
insert a combination of spaces (and possibly tabs) to simulate tab stops at this width.
expandtab
Enabling this will make the tab key (in insert mode) insert spaces instead of
tab characters. This also affects the behavior of the retab command.
smarttab
Enabling this will make the tab key (in insert mode) insert spaces or tabs to
go to the next indent
of the next tabstop when the cursor is at the beginning of a line (i.e. the
only preceding characters are whitespace).
For further details on any of these see :help 'optionname' in vim (e.g. :help 'tabstop')
To define this on a permanent basis for the current user, create (or edit) the .vimrc file:
$ vim ~/.vimrc
Then, paste the configuration below into the file. Once vim is restarted, the tab settings will apply.
set tabstop=4 " The width of a TAB is set to 4.
" Still it is a \t. It is just that
" Vim will interpret it to be having
" a width of 4.
set shiftwidth=4 " Indents will have a width of 4
set softtabstop=4 " Sets the number of columns for a TAB
set expandtab " Expand TABs to spaces
or shorthand for vim modeline:
vim :set ts=4 sw=4 sts=4 et :
There are few settings which define whether to use spaces or tabs.
So here are handy functions which can be defined in your ~/.vimrc file:
function! UseTabs()
set tabstop=4 " Size of a hard tabstop (ts).
set shiftwidth=4 " Size of an indentation (sw).
set noexpandtab " Always uses tabs instead of space characters (noet).
set autoindent " Copy indent from current line when starting a new line (ai).
endfunction
function! UseSpaces()
set tabstop=2 " Size of a hard tabstop (ts).
set shiftwidth=2 " Size of an indentation (sw).
set expandtab " Always uses spaces instead of tab characters (et).
set softtabstop=0 " Number of spaces a <Tab> counts for. When 0, featuer is off (sts).
set autoindent " Copy indent from current line when starting a new line.
set smarttab " Inserts blanks on a <Tab> key (as per sw, ts and sts).
endfunction
Usage:
:call UseTabs()
:call UseSpaces()
To use it per file extensions, the following syntax can be used (added to .vimrc):
au! BufWrite,FileWritePre *.module,*.install call UseSpaces()
See also: Converting tabs to spaces.
Here is another snippet from Wikia which can be used to toggle between tabs and spaces:
" virtual tabstops using spaces
set shiftwidth=4
set softtabstop=4
set expandtab
" allow toggling between local and default mode
function TabToggle()
if &expandtab
set shiftwidth=8
set softtabstop=0
set noexpandtab
else
set shiftwidth=4
set softtabstop=4
set expandtab
endif
endfunction
nmap <F9> mz:execute TabToggle()<CR>'z
It enables using 4 spaces for every tab and a mapping to F9 to toggle the settings.
I copied and pasted this into my .vimrc file:
" size of a hard tabstop
set tabstop=4
" always uses spaces instead of tab characters
set expandtab
" size of an "indent"
set shiftwidth=4
The first 2 settings mean that when I press Tab I get 4 spaces.
The third setting means that when I do V> (i.e. visual and indent) I also get 4 spaces.
Not as comprehensive as the accepted answer but it might help people who just want something to copy and paste.
One more thing, use
:retab
to convert existing tab to spaces
http://vim.wikia.com/wiki/Converting_tabs_to_spaces
Put your desired settings in the ~/.vimrc file -- See below for some guidelines and best practices.
There are four main ways to use tabs in Vim:
Always keep 'tabstop' at 8, set 'softtabstop' and 'shiftwidth' to 4 (or 3 or whatever you prefer) and use 'noexpandtab'. Then Vim will use a mix of tabs and spaces, but typing and will behave like a tab appears every 4 (or 3) characters.
Note: Setting 'tabstop' to any other value than 8 can make your file appear wrong in many places (e.g., when printing it).
Set 'tabstop' and 'shiftwidth' to whatever you prefer and use 'expandtab'. This way you will always insert spaces. The formatting will never be messed up when 'tabstop' is changed.
Set 'tabstop' and 'shiftwidth' to whatever you prefer and use a |modeline| to set these values when editing the file again. Only works when using Vim to edit the file.
Always set 'tabstop' and 'shiftwidth' to the same value, and 'noexpandtab'. This should then work (for initial indents only) for any tabstop setting that people use. It might be nice to have tabs after the first non-blank inserted as spaces if you do this though. Otherwise aligned comments will be wrong when 'tabstop' ischanged.
Source:
vimdoc.sourceforge.net/htmldoc/options.html#'tabstop'
:help tabstop
Add line
set ts=4
in
~/.vimrc file for per user
or
/etc/vimrc file for system wide
:set sw=4
See Mastering the VI editor
My basic ~/.vimrc with comment:
set number " show line number
set tabstop=2 " set display width of tab; 1 tab = x space with
set expandtab " transform tab to x space (x is tabstop)
set autoindent " auto indent; new line with number of space at the beginning same as previous
set shiftwidth=2 " number of space append to lines when type >>
Permanent for all users (when you alone on server):
# echo "set tabstop=4" >> /etc/vim/vimrc
Appends the setting in the config file.
Normally on new server apt-get purge nano mc and all other to save your time. Otherwise, you will redefine editor in git, crontab etc.
Make sure vartabstop is unset
set vartabstop=
Set tabstop to 4
set tabstop=4
For permanent change, create the file ~/.vim/plugin/tab_expander.vim with the content
set tabstop=4 softtabstop=4 expandtab shiftwidth=4 smarttab
To prevent touching the ~/.vimrc, thus keeping other default settings untouched.
Related
A great answer I read as to all the different type of tab-related settings in vim is in the answer here: Redefine tab as 4 spaces. However, is there a way to actually see how those settings apply and affect a file? I have used something like:
:set list
But it doesn't really show me anything more than a bunch of $ (linebreaks), and I don't see anything for spaces or tabs in my file. Is there a way to view all the space-like characters in vim so that I can play around with, for example, how tabstop might work and how softtabstop might work, and how they may all work together, etc.
There are four things in vim related to tabs:
tabstop
expandtab
shiftwidth
softtabstop
Refer to useful reference of tab stops in vim
tabstop Set tabstop to tell vim how many columns a tab counts for. Linux kernel code expects each tab to be eight columns wide. Visual
Studio expects each tab to be four columns wide. This is the only
command here that will affect how existing text displays.
expandtab When expandtab is set, hitting Tab in insert mode will produce the appropriate number of spaces.
shiftwidth Set shiftwidth to control how many columns text is indented with the reindent operations (<< and >>) and automatic
C-style indentation.
softtabstop Set softtabstop to control how many columns vim uses when you hit Tab in insert mode. If softtabstop is less than tabstop
and expandtab is not set, vim will use a combination of tabs and
spaces to make up the desired spacing. If softtabstop equals tabstop
and expandtab is not set, vim will always use tabs. When expandtab is
set, vim will always use the appropriate number of spaces.
You can see current settings in vim by using below commands, in command mode:
:set tabstop?
:set expandtab?
:set shiftwidth?
:set softtabstop?
You can set values for current session by using commands below, in command mode:
:set tabstop=4
:set expandtab #if you want to remove expand tab, set noexpandtab
:set shiftwidth=4
:set softtabstop=4
Alternatively, if you want to make the changes permanent, you can make changes to vimrc file:
It is located in the below path:
~/.vimrc on Unix;
$HOME/_vimrc on Windows
For gvim, you can go to Edit -> startup settings to see current vimrc
settings and edit them.
You need to add a line to vimrc like given below:
set tabstop=4 softtabstop=4 shiftwidth=4 expandtab
This should work:
:set lcs=space:_,tab:>~ list
I just used vim to edit a javascript file. My indentation in vim is set like so:
(source: take.ms)
When I edit a portion of the file, in vim it looks as though the indentation is correct:
(source: take.ms)
However, in SourceTree and in Sublime Text, the indentation is incorrect:
SourceTree:
(source: take.ms)
Sublime Text:
Can anyone explain to me why this is happening and how I might fix it? I'm also curious which, really, is the "true" representation of the file's state.
There are three related settings: tabstop, softtabstop, and shiftwidth. They are not the same.
tabstop sets the size of a tab character.
softtabstop sets the number of spaces inserted when the <Tab> key is pressed.
shiftwidth sets the number of spaces inserted when autoindentation is used (e.g. after typing if (foo) {<Cr>).
If expandtab isn't set, then spaces will be automatically replaced by a Tab character (0x09) as soon as the number of spaces is a multitude of tabstop. If expandtab is set, then spaces are never "expanded" to a tab character.
In your case, you only set the shiftwidth, which doesn't control the actual size of the tab characters. You either want to set tabstop to the same value as Sublime text, or you want to use space indentation by setting expandtab. If you use set list you can see if your file is using tab characters or spaces (use set nolist to disable this).
Bonus tip
In my personal opinion, you generally want to set all three settings to the same value. I use this command to quickly set all three with :TS 4:
command! -nargs=1 TS setlocal ts=<args> sts=<args> sw=<args>
The solution (thanks to #ceejayoz) was to set the expandtab option in my .vimrc settings, so this:
autocmd FileType javascript setlocal shiftwidth=2 tabstop=2 expandtab
autocmd FileType jsx setlocal shiftwidth=2 tabstop=2 expandtab
did the job.
Every time I add a selector in CSS and I press Enter to define the properties it ends up like this:
#selector {
property: value;
}
(8-space tabs)
How can I configure Vim to make it like this:
#selector {
property: value;
}
(4-space tabs)
:set tabstop=4
:set shiftwidth=4
:set expandtab
This will insert four spaces instead of a tab character. Spaces are a bit more “stable”, meaning that text indented with spaces will show up the same in the browser and any other application.
To make the change for one session, use this command:
:set tabstop=4
To make the change permanent, add it to ~/.vimrc or ~/.vim/vimrc:
set tabstop=4
This will affect all files, not just css. To only affect css files:
autocmd Filetype css setlocal tabstop=4
as stated in Michał's answer.
Expanding on zoul's answer:
If you want to setup Vim to use specific settings when editing a particular filetype, you'll want to use autocommands:
autocmd Filetype css setlocal tabstop=4
This will make it so that tabs are displayed as 4 spaces. Setting expandtab will cause Vim to actually insert spaces (the number of them being controlled by tabstop) when you press tab; you might want to use softtabstop to make backspace work properly (that is, reduce indentation when that's what would happen should tabs be used, rather than always delete one char at a time).
To make a fully educated decision as to how to set things up, you'll need to read Vim docs on tabstop, shiftwidth, softtabstop and expandtab. The most interesting bit is found under expandtab (:help 'expandtab):
There are four main ways to use tabs in Vim:
Always keep 'tabstop' at 8, set 'softtabstop' and 'shiftwidth' to 4 (or 3 or whatever you prefer) and use 'noexpandtab'. Then Vim will use a mix of tabs and spaces, but typing and will behave like a tab appears every 4 (or 3) characters.
Set 'tabstop' and 'shiftwidth' to whatever you prefer and use 'expandtab'. This way you will always insert spaces. The formatting will never be messed up when 'tabstop' is changed.
Set 'tabstop' and 'shiftwidth' to whatever you prefer and use a |modeline| to set these values when editing the file again. Only works when using Vim to edit the file.
Always set 'tabstop' and 'shiftwidth' to the same value, and 'noexpandtab'. This should then work (for initial indents only) for any tabstop setting that people use. It might be nice to have tabs after the first non-blank inserted as spaces if you do this though. Otherwise aligned comments will be wrong when 'tabstop' is changed.
As a one-liner into vim:
:set tabstop=4 shiftwidth=4
For permanent setup, add these lines to ~/.vimrc:
set tabstop=4
set shiftwidth=4
set expandtab <-- (optional) 4-spaces instead of Tab indentation
Several of the answers on this page are 'single use' fixes to the described problem. Meaning, the next time you open a document with vim, the previous tab settings will return.
If anyone is interested in permanently changing the tab settings:
find/open your .vimrc - instructions here
add the following lines: (more info here)
set tabstop=4
set shiftwidth=4
set expandtab
then save file and test
UPDATE
If you are working in a particular project I highly recommend using editorconfig.
It lets you define an .editorconfig file at the root of your repository defining the indentation you want to use for each file type across your repository.
For example:
root = true
[*.css]
charset = utf-8
indent_style = space
indent_size = 4
[*.js]
charset = utf-8
indent_style = space
indent_size = 2
There is a vim plugin that automatically configures vim according to the config file for file you open.
On top of that the .editorconfig file is automatically supported on many other IDEs and editors so it is the best option for collaborating between users with different environments.
ORIGINAL ANSWER
If you need to change sizes often and you don't want to bind this to a specific file type you can have predefined commands on your .vimrc file to quickly switch preferences:
nmap <leader>t :set expandtab tabstop=4 shiftwidth=4 softtabstop=4<CR>
nmap <leader>m :set expandtab tabstop=2 shiftwidth=2 softtabstop=2<CR>
This maps two different sets of sizes to keys \t and \m. You can rebind this to whatever keys you want.
in my .vim/vimrc (vim 8.0 under ubuntu bionic), I have
if has("autocmd")
filetype plugin indent on
endif
so added line like :
autocmd Filetype css setlocal tabstop=2
doesn't work.
I created .vim/indent folder and added in :
css.vim with
set tabstop=2
set shiftwidth=2
set softtabstop=2
and it works !
I tried to on another computer with ubuntu focal and vim 8.1, it doesn't work !-(
in vim command mode, write:
:set ts=X
where X is your new desired space length
Vim is very accommodating when it comes to tab Vs. space preferences. As I understand it, the tabstop setting indicates the width of a tab character. The shiftwidth setting specifies how many columns to increment/decrement when using the << and >> commands, whereas the softtabstop setting influences the amount of whitespace to be inserted when you press the Tab key in insert mode. If expandtab is on, the tab key inserts softtabstop number of space characters. Whereas with expandtab switched off, pressing the Tab key inserts a the smallest possible number of tab+space characters that matches softtabstop. (Please correct me if I'm wrong.)
This final point makes me wonder: is there a practical case where you wouldn't want shiftwidth == tabstop && tabstop == softtabstop? I can't think of one. As far as I am concerned, it would be most convenient if I could set all 3 of these to the same value, in one single assignment. e.g. calling:
:set stab=4
which would be equivalent to running:
:set tabstop=4 softtabstop=4 shiftwidth=4
Can anyone suggest how this could be done?
UPDATE
Thanks for the replies so far from too much php, hobbs and kaiser.se. Rather than reply to each individually, I'm updating the question here.
Softtabstop with expandtab switched off
I said above that with expandtab switched off, pressing the Tab key inserts a the smallest possible number of tab+space characters that matches softtabstop. I stand by that, but I think I need to explain what I meant. I shall attempt to do so by way of a few examples. To follow along, run :set list so that you can see tab characters.
tabstop=4 softtabstop=2 shiftwidth=4 noexpandtab
In insert mode, pressing the tab key inserts 2 space characters. Press the tab key a second time, and instead of inserting two more space characters (for a total of 4 space characters) it replaces the previous 2 spaces with a single tab character. Tabstop is set to 4, so a single tab character has the same width as 4 spaces.
tabstop=4 softtabstop=6 shiftwidth=4 noexpandtab
In insert mode, pressing the tab key inserts 1 tab character plus 2 spaces. The tab character has a width of 4, so the total width is 6, and this is achieved using 3 characters. Pressing the tab key a second time inserts two tab characters, and removes the two spaces that were inserted previously. The total width is 12, and this is achieved using 3 characters.
In both of these examples, Vim inserts the minimum possible number of tab+space characters that matches softtabstop.
If I am working with expandtab switched off, I can't see myself wanting the extra granular control that can be achieved by setting softtabstop to a different value from tabstop. It would still be useful for me to be able to set tabstop, softtabstop and shiftwidth to the same value with a single command.
Does expandtab make softtabstop redundant?
tabstop=4 softtabstop=0 shiftwidth=4 expandtab
In insert mode, pressing the tab key inserts 4 spaces. Pressing the delete key deletes a single space - so you have to backspace 4 times if you hit the tab key by accident.
tabstop=4 softtabstop=4 shiftwidth=4 expandtab
In insert mode, pressing the tab key inserts 4 spaces. Pressing the backspace key deletes 4 spaces.
If I am working with expandtab switched on, I would prefer the delete key to remove the same amount of whitespace as the tab key inserts. So in this case, too, I feel that it would be useful to be able to assign the same value to tabstop, softtabstop and shiftwidth simultaneously.
A shortcut would still be useful
It's great that Vim provides so much flexibility, but I can't see myself needing it. I just want to be able to choose the width of a tab, and whether it is a 'hard' tab (using a tab character) or a 'soft' tab (made up of spaces). Toggling between hard and soft tabs is easy enough (:set expandtab!), but I wish it was more straightforward to set the width of tab, without having to fiddle with 3 different parameters.
So my proposed suggestion for something like :set stab=4 still sounds good to me.
Creating a stab option in Vim itself would not be easy, but I've whipped up this command/function that you can drop in your .vimrc (or a plugin file if you're super-organized). Use :Stab and you will be prompted for an indent level and whether or not to use expandtab. If you hit enter without giving it a new indent level, it will just print the current settings.
" put all this in your .vimrc or a plugin file
command! -nargs=* Stab call Stab()
function! Stab()
let l:tabstop = 1 * input('set shiftwidth=')
if l:tabstop > 0
" do we want expandtab as well?
let l:expandtab = confirm('set expandtab?', "&Yes\n&No\n&Cancel")
if l:expandtab == 3
" abort?
return
endif
let &l:sts = l:tabstop
let &l:ts = l:tabstop
let &l:sw = l:tabstop
if l:expandtab == 1
setlocal expandtab
else
setlocal noexpandtab
endif
endif
" show the selected options
try
echohl ModeMsg
echon 'set tabstop='
echohl Question
echon &l:ts
echohl ModeMsg
echon ' shiftwidth='
echohl Question
echon &l:sw
echohl ModeMsg
echon ' sts='
echohl Question
echon &l:sts . ' ' . (&l:et ? ' ' : 'no')
echohl ModeMsg
echon 'expandtab'
finally
echohl None
endtry
endfunction
This is my first attempt at writing VimScript, but here goes:
function! Stab(value)
let &shiftwidth = a:value
let &softtabstop = a:value
let &tabstop = a:value
endfunc
If I put this in my .vimrc file, I can call it by running :call Stab(X), where X is the desired tab width. This is an adequate solution for now, but if anyone can suggest a way of making it easier to call I would be grateful.
I've also created a function that quickly summarizes the current settings, which I have mapped to ctrl-Tab:
nmap <C-Tab> :call TabParams()<CR>
function! TabParams()
echo "tabstop: ".&tabstop
echo "shiftwidth: ".&shiftwidth
echo "softtabstop: ".&softtabstop
endfunc
Well, I put up a 100 point bounty for this answer, and now I've half solved it myself. Not sure if I can accept my own answer...
You can in edit mode also use Ctrl-T to indent and Ctrl-D to deindent to the next indentation level as set by shiftwidth, regardless of the tabstop, softtabstop or expandtab settings. Vim will automatically add/remove spaces or tabs to bring you to the right column.
If you use these commands to control indentation instead of Tab/Backspace you don't have to worry about all these tab settings fitting together and always get to the correct indentation level.
If expandtab is set then (as too much php points out), softtabstop becomes redundant. The only reason you might set shiftwidth differently from tabstop would be to cater to an odd habit; for instance, you use four-space indents but you prefer tab to insert eight spaces.
If expandtab is unset then things get fuzzier. If you want your code to look the same in with cat and non-vim editors as it does in vim, then tabstop should always be set at 8; in this case you would set softtabstop and shiftwidth both to your preferred indent level. If you instead prefer that every "physical tab" in the file represents one indent level, you would set tabstop and shiftwidth to your preferred indent level and leave softtabstop at zero (setting it equal to tabstop is equivalent except that if you change tabstop it will get out of sync, while zero just means "ignore this please").
Your understanding of softtabstop and expandtab is wrong - so the stab option you suggest wouldn't be very useful.
expandtab is for when you want to use spaces instead of tabs for everything. If you set expandtab, then Vim ignores the softtabstop option and uses tabstop and shiftwidth to work out how many spaces to insert.
softtabstop is only for when you would like to use a mix of tabs and spaces, allowing you to indent with fine control (2 or 4 spaces), while keeping tab width at a higher value (usually 8) so that text appears in the other applications. Setting softtabstop=tabstop doesn't accomplish anything because Vim will always use tabs for indenting.
Update: As kaizer.se has pointed out, if you are using expandtab, then you still need to set softtabstop if you want Vim to backspace multiple spaces as though they are a tab.
Are you changing your white space settings so often you really need a function to manage that? If you are messing with tabstop a lot and also setting expandtab, you are probably going to have a mess over time as you change files with different values passed to stab. Today I use :call stab (4), tomorrow it's :call stab (2) and last week it was :call stab (8). Sounds like even if you write it, you'll soon stop using it.
If you plan to always pass the same value to stab, why not just edit your global settings? In vim:
:e $MYVIMRC
and add the following:
set tabstop=4
set shiftwidth=4 "tabs are 4 spaces wide (default = 8)
set expandtab "Convert tabs to spaces
This is how my .vimrc is setup.
One useful option is softtabstop=-1 which will set it to the value of shiftwidth.
You can also set shiftwidth to 0, in which case the tabstop value will be used.
How do I make vi-Vim never use tabs (converting spaces to tabs, bad!), makes the tab key == 4 spaces, and automatically indent code after curly brace blocks like Emacs does?
Also, how do I save these settings so I never have to input them again?
I've seen other questions related to this, but it always seems to be a little off from what I want.
As has been pointed out in a couple of other answers, the preferred method now is NOT to use smartindent, but instead use the following (in your .vimrc):
filetype plugin indent on
" show existing tab with 4 spaces width
set tabstop=4
" when indenting with '>', use 4 spaces width
set shiftwidth=4
" On pressing tab, insert 4 spaces
set expandtab
In your [.vimrc:][1] file:
set smartindent
set tabstop=4
set shiftwidth=4
set expandtab
The help files take a bit of time to get used to, but the more you read, the better Vim gets:
:help smartindent
Even better, you can embed these settings in your source for portability:
:help auto-setting
To see your current settings:
:set all
As graywh points out in the comments, smartindent has been replaced by cindent which "Works more cleverly", although still mainly for languages with C-like syntax:
:help C-indenting
Related, if you open a file that uses both tabs and spaces, assuming you've got
set expandtab ts=4 sw=4 ai
You can replace all the tabs with spaces in the entire file with
:%retab
The best way to get filetype-specific indentation is to use filetype plugin indent on in your vimrc. Then you can specify things like set sw=4 sts=4 et in .vim/ftplugin/c.vim, for example, without having to make those global for all files being edited and other non-C type syntaxes will get indented correctly, too (even lisps).
To have 4-space tabs in most files, real 8-wide tab char in Makefiles, and automatic indenting in various files including C/C++, put this in your ~/.vimrc file:
" Only do this part when compiled with support for autocommands.
if has("autocmd")
" Use filetype detection and file-based automatic indenting.
filetype plugin indent on
" Use actual tab chars in Makefiles.
autocmd FileType make set tabstop=8 shiftwidth=8 softtabstop=0 noexpandtab
endif
" For everything else, use a tab width of 4 space chars.
set tabstop=4 " The width of a TAB is set to 4.
" Still it is a \t. It is just that
" Vim will interpret it to be having
" a width of 4.
set shiftwidth=4 " Indents will have a width of 4.
set softtabstop=4 " Sets the number of columns for a TAB.
set expandtab " Expand TABs to spaces.
On many Linux systems, like Ubuntu, the .vimrc file doesn't exist by default, so it is recommended that you create it first.
Don't use the .viminfo file that exist in the home directory. It is used for a different purpose.
Step 1: Go to your home directory
cd ~
Step 2: Create the file
vim .vimrc
Step 3: Add the configuration stated above
filetype plugin indent on
set tabstop=4
set shiftwidth=4
set expandtab
Step 3: Save file, by pressing Shift + ZZ.
The recommended way is to use filetype based indentation and only use smartindent and cindent if that doesn't suffice.
Add the following to your .vimrc
set expandtab
set shiftwidth=2
set softtabstop=2
filetype plugin indent on
Hope it helps as being a different answer.
From the VIM wiki:
:set tabstop=4
:set shiftwidth=4
:set expandtab
edit your ~/.vimrc
$ vim ~/.vimrc
add following lines :
set tabstop=4
set shiftwidth=4
set softtabstop=4
set expandtab
The auto-indent is based on the current syntax mode. I know that if you are editing Foo.java, then entering a { and hitting Enter indents the following line.
As for tabs, there are two settings. 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.
Firstly, do not use the Tab key in Vim for manual indentation. Vim has a pair of commands in insert mode for manually increasing or decreasing the indentation amount. Those commands are Ctrl-T and Ctrl-D. These commands observe the values of tabstop, shiftwidth and expandtab, and maintain the correct mixture of spaces and tabs (maximum number of tabs followed by any necessary number of spaces).
Secondly, these manual indenting keys don't have to be used very much anyway if you use automatic indentation.
If Ctrl-T instead of Tab bothers you, you can remap it:
:imap <Tab> ^T
You can also remap Shift-Tab to do the Ctrl-D deindent:
:imap <S-Tab> ^D
Here ^T and ^D are literal control characters that can be inserted as Ctrl-VCtrl-T.
With this mapping in place, you can still type literal Tab into the buffer using Ctrl-VTab. Note that if you do this, even if :set expandtab is on, you get an unexpanded tab character.
A similar effect to the <Tab> map is achieved using :set smarttab, which also causes backspace at the front of a line to behave smart.
In smarttab mode, when Tab is used not at the start of a line, it has no special meaning. That's different from my above mapping of Tab to Ctrl-T, because a Ctrl-T used anywhere in a line (in insert mode) will increase that line's indentation.
Other useful mappings may be:
:map <Tab> >
:map <S-Tab> <
Now we can do things like select some lines, and hit Tab to indent them over. Or hit Tab twice on a line (in command mode) to increase its indentation.
If you use the proper indentation management commands, then everything is controlled by the three parameters: shiftwidth, tabstop and expandtab.
The shiftwidth parameter controls your indentation size; if you want four space indents, use :set shiftwidth=4, or the abbreviation :set sw=4.
If only this is done, then indentation will be created using a mixture of spaces and tabs, because noexpandtab is the default. Use :set expandtab. This causes tab characters which you type into the buffer to expand into spaces, and for Vim-managed indentation to use only spaces.
When expandtab is on, and if you manage your indentation through all the proper Vim mechanisms, the value of tabstop becomes irrelevant. It controls how tabs appear if they happen to occur in the file. If you have set tabstop=8 expandtab and then sneak a hard tab into the file using Ctrl-VTab, it will produce an alignment to the next 8-column-based tab position, as usual.
Afterall, you could edit the .vimrc,then add the conf
set tabstop=4
Or exec the command
Simplest one will be n vim file
set tabstop=4