I'm using the following .vimrc. Which contains (but not limited to) the following:
set encoding=utf-8
set tabstop=4
set autoindent
set list lcs=tab:·\ ,trail:•,eol:¬
This should give me four-space hard tabs, with the first character being displayed as a small mid-line dot, and the remaining three characters as spaces. But what I get is two-space hard tabs. Something else in my vimrc is throwing off the tab spacing.
Clearly I'm doing something wrong, and giving myself two-space hard tabs. I've researched listchars, vim compile flags and trying to input non-breaking space into my vimrc. Any other ideas what I'm doing wrong??
Just looked at your _vimrc; the confusion is in the following lines
augroup htmldjango
set tabstop=2
set softtabstop=2
set shiftwidth=2
augroup END
You're using set command and rewriting your previous tabstop, softtabstop and shiftwidth settings regardless of filetype. Therefore, you're effectively setting your tab to 2.
It would be better maybe (and my guess is this is what you were trying to do) were you to use
augroup htmldjango
autocmd!
autocmd FileType html setlocal tabstop=2
autocmd FileType html setlocal softtabstop=2
autocmd FileType html setlocal shiftwidth=2
augroup END
therefore setting different tabstop, softtabstop, etc. only when HTML filetype is used.
Notice: I have no idea what is a htmldjango filetype so you might want to modify that part for what suits you best.
Related
At my work, I am required to follow the house style for indentation, which goes as follows:
2 spaces when coding html and ruby
tabs when coding javascript, with tabwidth=4 recommended
What is the best way to specify different whitespace preferences per filetype?
there are many ways, but here's a simple, easy to understand way. add these lines to your ~/.vimrc:
autocmd FileType html setlocal ts=2 sts=2 sw=2
autocmd FileType ruby setlocal ts=2 sts=2 sw=2
autocmd FileType javascript setlocal ts=4 sts=4 sw=4
Peter's answer is straightforward enough, but unfortunately the options aren't right. You need to use the following options instead:
autocmd Filetype html setlocal ts=2 sw=2 expandtab
autocmd Filetype ruby setlocal ts=2 sw=2 expandtab
autocmd Filetype javascript setlocal ts=4 sw=4 sts=0 noexpandtab
Also note:
You can make vim show tab characters by using :set list.
Once you have the tab/space options set correctly, you can make vim repair the file (replace spaces with tabs or vice versa) using the :retab! command.
+1 to Peter's answer, but Vim provides another solution as well. If you want to do something more complicated than a single setlocal, like setting up a whole bunch of options, commands, and mappings at once, then vim's filetype plugin feature comes to the rescue.
You need to have filetype plugin on or filetype plugin indent on in your .vimrc, and then to create a plugin for e.g. ruby you can create ~/.vim/ftplugin/ruby.vim. Technically you can use any commands you like in here, to be run when a Ruby file is loaded, but the recommended ones include setlocal, map <buffer>, command -buffer, and defining functions. Lots more information is in the User Guide; if you're pretty familiar with scripting vim then jump to :help 41.11, otherwise read :help usr_40 and :help usr_41.
There's also a nice vim script: DetectIndent which tries to detect the indentation of a file that you open.
It's very handy if you work with many files with different coding style.
I use an autocommand in my .vimrc:
:autocmd BufReadPost * :DetectIndent
To insert space characters whenever the tab key is pressed, set the 'expandtab' option:
:set expandtab
Next step is to control the number of space characters that will be inserted when the tab key is pressed, set the 'tabstop' option. For example, to insert 2 space for a tab, use:
:set tabstop=2
ref: http://vim.wikia.com/wiki/Converting_tabs_to_spaces
I'm trying to learn to use Vim. I like indenting by 4 spaces but use 2 spaces for certain languages, such as Nim and MoonScript. I tried adding this to my .vimrc:
autocmd BufNewFile,BufRead,BufEnter *.nim :setlocal tabstop=2 shiftwidth=2 softtabstop=2
Problem? It doesn't do anything! All that happens is that the tab does nothing when I press it! What am I doing wrong?
autocmd BufNewFile,BufRead *.nim setlocal tabstop=2 shiftwidth=2 softtabstop=2
This should work (after restarting Vim / re-editing an existing file via :e!) fine, but it mixes filetype detection with filetype settings. Vim has an intermediate abstraction called filetype, which you should use. With it, you map file globs like *.nim to a filetype nim, and then define settings either via an :autocmd FileType, or a filetype plugin in ~/.vim/ftplugin/nim.vim (for the latter, you need :filetype plugin on in your ~/.vimrc).
Steps
So, create a filetype detection in ~/.vim/ftdetect/nim.vim:
autocmd BufRead,BufNewFile *.nim setfiletype nim
Then, create a filetype plugin in ~/.vim/ftplugin/nim.vim:
setlocal tabstop=2 shiftwidth=2 softtabstop=2
You can check that these are loaded correctly via the :scriptnames output. After a restart of Vim, this should work, and you can add additional settings to the latter file. If your filetype is derived from another, you can also add :runtime! ftplugin/javascript.vim (for example) in there to get those settings, too.
Could someone explain to me in simple terms the easiest way to change the indentation behavior of Vim based on the file type? For instance, if I open a Python file it should indent with 2 spaces, but if I open a Powershell script it should use 4 spaces.
You can add .vim files to be executed whenever vim switches to a particular filetype.
For example, I have a file ~/.vim/after/ftplugin/html.vim with this contents:
setlocal shiftwidth=2
setlocal tabstop=2
Which causes vim to use tabs with a width of 2 characters for indenting (the noexpandtab option is set globally elsewhere in my configuration).
This is described here: http://vimdoc.sourceforge.net/htmldoc/usr_05.html#05.4, scroll down to the section on filetype plugins.
Use ftplugins or autocommands to set options.
ftplugin
In ~/.vim/ftplugin/python.vim:
setlocal shiftwidth=2 softtabstop=2 expandtab
And don't forget to turn them on in ~/.vimrc:
filetype plugin indent on
(:h ftplugin for more information)
autocommand
In ~/.vimrc:
autocmd FileType python setlocal shiftwidth=2 softtabstop=2 expandtab
I would also suggest learning the difference between tabstop and softtabstop. A lot of people don't know about softtabstop.
edit your ~/.vimrc, and add different file types for different indents,e.g. I want html/rb indent for 2 spaces, and js/coffee files indent for 4 spaces:
" by default, the indent is 2 spaces.
set shiftwidth=2
set softtabstop=2
set tabstop=2
" for html/rb files, 2 spaces
autocmd Filetype html setlocal ts=2 sw=2 expandtab
autocmd Filetype ruby setlocal ts=2 sw=2 expandtab
" for js/coffee/jade files, 4 spaces
autocmd Filetype javascript setlocal ts=4 sw=4 sts=0 expandtab
autocmd Filetype coffeescript setlocal ts=4 sw=4 sts=0 expandtab
autocmd Filetype jade setlocal ts=4 sw=4 sts=0 expandtab
refer to: Setting Vim whitespace preferences by filetype
Put autocmd commands based on the file suffix in your ~/.vimrc
autocmd BufRead,BufNewFile *.c,*.h,*.java set noic cin noexpandtab
autocmd BufRead,BufNewFile *.pl syntax on
The commands you're looking for are probably ts= and sw=
I usually work with expandtab set, but that's bad for makefiles. I recently added:
:autocmd FileType make set noexpandtab
to the end of my .vimrc file and it recognizes Makefile, makefile, and *.mk as makefiles and does not expand tabs. Presumably, you can extend this.
Personally, I use these settings in .vimrc:
autocmd FileType python set tabstop=8|set shiftwidth=2|set expandtab
autocmd FileType ruby set tabstop=8|set shiftwidth=2|set expandtab
This might be known by most of us, but anyway (I was puzzled my first time):
Doing :set et (:set expandtabs) does not change the tabs already existing in the file, one has to do :retab.
For example:
:set et
:retab
and the tabs in the file are replaced by enough spaces. To have tabs back simply do:
:set noet
:retab
For those using autocmd, it is a best practice to group those together. If a grouping is related to file-type detection, you might have something like this:
augroup filetype_c
autocmd!
:autocmd FileType c setlocal tabstop=2 shiftwidth=2 softtabstop=2 expandtab
:autocmd FileType c nnoremap <buffer> <localleader>c I/*<space><esc><s-a><space>*/<esc>
augroup end
Groupings help keep the .vimrc organized especially once a filetype has multiple rules associated with it. In the above example, a comment shortcut specific to .c files is defined.
The initial call to autocmd! tells vim to delete any previously defined autocommands in said grouping. This will prevent duplicate definition if .vimrc is sourced again. See the :help augroup for more info.
Today, you could try editorconfig, there is also a vim plugin for it. With this, you are able not only change indentation size in vim, but in many other editors, keep consistent coding styles.
Below is a simple editorconfig, as you can see, the python files will have 4 spaces for indentation, and pug template files will only have 2.
# 4 space indentation for python files
[*.py]
indent_style = space
indent_size = 4
# 2 space indentation for pug templates
[*.pug]
indent_size = 2
While you can configure Vim's indentation just fine using the indent plugin or manually using the settings, I recommend using a python script called Vindect that automatically sets the relevant settings for you when you open a python file. Use this tip to make using Vindect even more effective. When I first started editing python files created by others with various indentation styles (tab vs space and number of spaces), it was incredibly frustrating. But Vindect along with this indent file
Also recommend:
pythonhelper
python_match
python_ifold
In Lua (for Neovim users) you can use RUNTIMEPATH/ftplugin/*yourfiletype*.lua with options like:
vim.opt_local.shiftwidth = 2
vim.opt_local.tabstop = 2
Just be sure to use string values in quotes. For example:
vim.opt_local.foldmethod = 'marker'
I use a utility that I wrote in C called autotab. It analyzes the first few thousand lines of a file which you load and determines values for the Vim parameters shiftwidth, tabstop and expandtab.
This is compiled using, for instance, gcc -O autotab.c -o autotab. Instructions for integrating with Vim are in the comment header at the top.
Autotab is fairly clever, but can get confused from time to time, in particular by that have been inconsistently maintained using different indentation styles.
If a file evidently uses tabs, or a combination of tabs and spaces, for indentation, Autotab will figure out what tab size is being used by considering factors like alignment of internal elements across successive lines, such as comments.
It works for a variety of programming languages, and is forgiving for "out of band" elements which do not obey indentation increments, such as C preprocessing directives, C statement labels, not to mention the obvious blank lines.
This is not a very simple question.
I found that the "shiftwidth" for python files are set to 4, while I have these at the bottom of my .vimrc:
set tabstop=2
set shiftwidth=2
set softtabstop=2
set expandtab
The shiftwidth are 2 for all other filetypes; but it's 4 only for python.
I want to know if it's possible to see where a variable (in my case, the shiftwidth) has been changed; if this is not possible when what are the possible locations that this variable is changed..? And is there any way go make the shiftwidth=2 universally... even for the python files? (Although it's also recommended of using 4 spaces for python, but I personally prefer 2)
:verbose set shiftwidth
tells you what's the current value and where it was defined. See :help :verbose.
Filetype plugins are sourced after the corresponding FileType event was triggered so they will override the options in your vimrc.
The dirty way to add filetype-specific settings to your config is to add something like this to your vimrc:
augroup Python
autocmd!
autocmd fileType python setlocal tabstop=2 shiftwidth=2 softtabstop=2 expandtab
augroup END
See :help :autocmd.
The clean way is to put your desired settings into a custom ftplugin.
The file:
~/.vim/after/ftplugin/python.vim
Its content:
setlocal tabstop=2
setlocal shiftwidth=2
setlocal softtabstop=2
setlocal expandtab
Note the use of setlocal instead of set because you don't want those settings to be applied everywhere.
This is probably being set by the built-in ftplugin file for Python. If you
:e $VIMRUNTIME/ftplugin/python.vim
and search for shiftwidth, you’ll probably find it set there to 4.
I'd like to default to soft tabs, indented two spaces (but hard tabs displayed as two spaces for Makefiles), and for vim to reindent appropriately on save.
See this question. For your case, you would want this in your .vimrc:
set tabstop=2
set softtabstop=2
set expandtab
And this in ~/.vim/after/ftplugin/make.vim:
setlocal noexpandtab
You can use autocmd for this, so it pretty much becomes a oneliner in your.vimrc
autocmd Filetype jade setlocal ts=2 sw=2 expandtab
autocmd Filetype yaml setlocal ts=2 sw=2 expandtab
Everything else will still use the global defaults.