Last call to make your voice heard! Our 2022 Developer Survey closes in less than a week. Take survey.
136 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 ...
user avatar
  • 2,251
74 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 ...
user avatar
  • 1,664
62 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 " ...
user avatar
  • 5,770
61 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 '...
user avatar
  • 45.9k
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 ...
user avatar
  • 7,992
54 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 ...
user avatar
53 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 ...
user avatar
  • 7,992
49 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 ...
user avatar
42 votes
Accepted

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

To add to @Undo 's answer above. Tim Pope too has a plugin that tries to do the same at tpope/vim-sleuth
user avatar
  • 5,145
41 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/...
user avatar
  • 1,175
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 ...
user avatar
  • 32.6k
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 ...
user avatar
26 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 ...
user avatar
  • 16.5k
24 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....
user avatar
  • 966
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 ...
user avatar
  • 2,251
21 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.
user avatar
  • 9,260
20 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 ...
user avatar
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 ...
user avatar
  • 10.3k
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 ...
user avatar
  • 5,854
19 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. ...
user avatar
18 votes

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

I haven't used it, but ciaranm/detectindent might be what you're looking for. It seems to be active (last updated 25 days ago), and looks like it does what you want: A Vim plugin, for automatically ...
user avatar
  • 2,433
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 ...
user avatar
17 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("....
user avatar
  • 631
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 ...
user avatar
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 ...
user avatar
  • 1,815
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 ...
user avatar
  • 32.6k
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 ...
user avatar
  • 980
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 ...
user avatar
  • 20.7k
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 ...
user avatar
12 votes
Accepted

Count the number of whitespace in the beginning of a line

so easy i'm ashamed. :echo indent(2)
user avatar

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