142 votes
Accepted

How to replace tabs with spaces?

You can use the :retab command. From :help :retab Replace all sequences of white-space containing a <Tab> with new strings of white-space using the new tabstop value given. If you do not ...
  • 2,321
85 votes
Accepted

What is the difference between `filetype plugin indent on` and `filetype indent on`?

filetype plugin indent on is like a combination of these commands: filetype on filetype plugin on filetype indent on It turns on "detection", "plugin" and "indent" at once. You can check for ...
  • 1,992
74 votes

How to indent as spaces instead of tab?

You want expandtab. But there are usually a couple of options you want to set at the same time. If you add the following to your ~/.vimrc file " tabstop: Width of tab character " ...
  • 6,160
67 votes
Accepted

How can I change the indent size?

Several settings controls the behavior: First set expandtab allows to replace the tabs by white spaces characters :h 'expandtab'. Then set shiftwidth=4 makes the tabulations be 4 white spaces :h '...
  • 48.1k
62 votes
Accepted

How to indent as spaces instead of tab?

You are looking for the expandtab option. When this option is set, spaces are always used. You can put set expandtab in your vimrc to always have this option set when vim starts. If you want to ...
55 votes
Accepted

How to add indentation guides/lines?

Unified answer: If you are using tabs I'd try using listchars: :set listchars=tab:\|\ :set list note: there is a space after the last \ above. This method is good because it uses Vim's built-in ...
  • 8,282
55 votes

How to add indentation guides/lines?

Another option I use, which isn't exactly what you asked for, but accomplishes the same purpose of making it easier to see what is in the same column, is to set the following in your vimrc: set ...
54 votes
Accepted

Re-indenting badly indented code

I do this one of two ways. Indent adjusted paste First, if the code in the buffer is formatted, but at a different level of indentation, I use ]p instead of p, which pastes the code as is, but with ...
  • 8,282
46 votes
Accepted

How do I make Vim adapt to the current indenting style of the file I'm editing?

Install this plugin: tpope/vim-sleuth
  • 5,330
45 votes
Accepted

What is the difference between autoindent and smartindent in vimrc?

autoindent essentially tells vim to apply the indentation of the current line to the next (created by pressing enter in insert mode or with O or o in normal mode. smartindent reacts to the syntax/...
  • 1,215
32 votes
Accepted

How can I change the default indentation based on filetype?

The best method is to put those settings in ~/.vim/after/ftplugin/<filetype>.vim. For HTML (assuming you want 4 characters-wide tabs): ~/.vim/after/ftplugin/html.vim setlocal shiftwidth=4 ...
  • 36.2k
30 votes

How to replace tabs with spaces?

You can use :retab, as stated, however, this will change all tabs to spaces, not only tabs at the start of the line So this (where ⇥ is a tab character): if :; do ⇥echo "⇥hello" end gets changed to ...
28 votes

How to replace tabs with spaces?

Vim provides :retab! command which will replace all sequences of <Tab> with new strings of white-space using the new tabstop (e.g. :set tabstop=2) value given, but all tabs inside of strings can ...
  • 17.6k
25 votes

How can I change the indent size?

In fact, aside from other answers, it was also possible to use autoindent(to automatically indent newlines) and smartindent (wich reacts to the synthax of the code). Then the shiftwidth I wanted was 2....
  • 1,036
22 votes
Accepted

How can I automatically indent on new lines?

This can be done using a combination of the autoindent option and the filetype plugin. The autoindent option will copy your current indentation when creating a new line. So if your current ...
22 votes
Accepted

How do you indent > < without leaving visual mode and losing your current selection?

I have this in my vimrc: "keep visual mode after indent vnoremap > >gv vnoremap < <gv Note that you could also simply use . (dot) to repeat the last indent action.
  • 9,510
21 votes

How to add indentation guides/lines?

For the sake of completeness, there is also the Indent Guides plugin, which shows the indentation levels by alternating the colors of white spaces. It requires version 7.2 or later. Since it uses the ...
  • 2,321
21 votes
Accepted

How to move the cursor to the correct indentation level without quiting insert mode?

As @jamessan mentioned, <C-f> will indent to the correct place from insert mode. You can also use <C-t> and <C-d> to increase or decrease the indention level from insert mode. ...
19 votes

Re-indenting badly indented code

The = command can be used to reindent. Like most normal mode commands it can be applied to a motion, so you can reindent the just pasted code with =']. This reindents from the current cursor ...
  • 10.6k
19 votes
Accepted

Unexplained gq indentation issue

The problem is that you have set smartindent in your ~/.vimrc. The smartindent options causes Vim to assume that your text is some C-like programming language when indenting. Among other things, it ...
  • 6,144
18 votes

How do I move vertically until reaching a non-whitespace character?

I like using search for these things, because Vim's regexs have an insane amount of things you can search for, like specific virtual column numbers, in this case: nnoremap cd /\%<C-R>=virtcol("....
  • 641
18 votes

How do I make Vim adapt to the current indenting style of the file I'm editing?

ciaranm/detectindent might be what you're looking for. It's unmaintained (last updated in 2015, there was also a fork that was last updated in 2016: roryokane/detectindent) and I haven't used it, but ...
  • 2,511
18 votes
Accepted

How do I shift the line indent to a specific value?

You can use the :left command: :[range]le[ft] [indent] Left-align lines in [range]. Sets the indent in the lines to [indent] (default 0). {not in Vi} Note that :left sets the indent to n ...
17 votes
Accepted

Vim doesn't use the correct indentation in Python files

It gets reset by the Python filetype plugin; from /usr/share/vim/vim74/ftplugin/python.vim: " As suggested by PEP8. setlocal expandtab shiftwidth=4 softtabstop=4 tabstop=8 This file is loaded ...
14 votes

How can I change the default indentation based on filetype?

You can do this with autocommands in your .vimrc. For example, I have a function html_like_mode that sets up various things for editing HTML files. In my .vimrc, I have: au BufNewFile,BufRead *.html ...
  • 1,905
13 votes
Accepted

How can I copy/paste new text without auto-indentation at every new line?

OK, here is a "yank & put" primer… In Vim, the primary commands for yanking (copying) and putting (pasting) are y and p. Yanking places the yanked text in a register. That register is ...
  • 36.2k
13 votes

How can I stop vim automatically inserting a tab in CSS files?

I found a couple of ways to solve the problem. 1. reload the buffer after running :filetype indent off Vim's docs (:help :filetype-indent-off) provided an explanation: You can disable :filetype ...
13 votes
Accepted

Why does >3k move the cursor up when >3j does not move it down?

Yes, Vim automatically moves to the top most indented line. To see this, position the cursor in the middle of a paragraph and use >ap to indent the whole paragraph. Note that this is not specific ...
  • 1,020
13 votes
Accepted

How to visually select based on indentation?

I've used vim-indent-object for a while with some success. It provides text objects like ii, ai, iI, and aI, each with slightly different semantics. For your sample code def some_method if @foo ...
  • 23.4k
12 votes

How can I change the default indentation based on filetype?

augroup allows you to group related autocommands into named groups. (See here to learn about Autocommand Groups.) We use augroups to add autocommands to ~/.vimrc in a way that won't add a duplicate ...

Only top scored, non community-wiki answers of a minimum length are eligible