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
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
Im using the Janus Vim distribution as a starting point and I have the following in my .vimrc.after
set autoindent noexpandtab tabstop=2 shiftwidth=2
set guioptions
This has worked well as I want a tab the equivalent of 2 spaces to be used everywhere. Its important to note that I want a tab character and not two actual spaces. So far this has worked great in all the files I've used, with the exeception of .slim files. In this case two spaces are used. I'm using https://github.com/slim-template/vim-slim for slim syntax highlighting.
Here is my question: how do I override whatever settings are applied to slim files and force a two-space sized tab character everywhere?
Perhaps you need to put these in .vimrc instead of .vimrc.after? I have a similar issue that I want to expand tabs normally, but not expand them for makefiles since these must be tabs to work. I have in my main .vimrc
" In general
set expandtab
" Exception for makefiles
au BufNewFile,BufRead Makefile,makefile,nmakefile set noexpandtab
I don't have slim syntax installed, but I'm guessing
au BufNewFile,BufRead *.slim set tabstop=2 noexpandtab
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.
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.
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