Podcast #128: We chat with Kent C Dodds about why he loves React and discuss what life was like in the dark days before Git. Listen now.
99

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 specify a new tabstop size or it is zero, Vim uses the current value of 'tabstop'. [...] With 'expandtab' on, Vim replaces all tabs with the appropriate ...


48

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 the indentation shifted such that the first line pasted is the same depth as the line I'm on. E.G. source copied to buffer while (1) { dostuff(); } E.G. ...


46

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 yourself by reading :help :filetype-overview. Detection What does filetype "detection" do? From the docs: Each time a new or existing file is edited, Vim will try ...


39

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 support for showing this kind of thing. Example output: | if (1) { | | func(); | } However, this doesn't work for leading spaces. As ...


28

To add to @Undo 's answer above. Tim Pope too has a plugin that tries to do the same at tpope/vim-sleuth


25

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 setlocal softtabstop=4 setlocal noexpandtab Using an ftplugin is prefered to using autocommands because Vim already does filetype checks by itself and already ...


25

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 only enable this option for specific languages, see this question. For help on this option, see :help 'expandtab'.


24

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/style of the code you are editing (especially for C). When having it on you also should have autoindent on. :help autoindent also mentions two alternative ...


23

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 (where ␣ is a space character): if :; do ␣␣echo "␣␣hello" end This can produce unexpected side-effects in some scenarios, and it's even more of an issue when ...


22

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 " softtabstop: Fine tunes the amount of white space to be added " shiftwidth Determines the amount of whitespace to add in normal mode " expandtab: When ...


22

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 'shiftwidth'. You could also be interested by by :h 'tabstop' which defines the number of spaces that a tab character in the file counts for. As a bonus see :h '...


19

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 cursorcolumn set cursorline This has the trade off that you have to move your cursor to the column you want to see, but you get less visual clutter. Usually when I ...


19

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.


18

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 tabstop and shiftwidth variables, it will probably work for your indentation settings right away. Once the plugin is installed, you can simply use:...


18

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 spaces, and is not aware of shiftwidth or tabstop, so using :left 8 will use 8 spaces. If you want to set the indentation level, you can easily define a command: ...


18

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 treats for like a keyword and gives an extra level of indentation to the line following. See :help 'smartindent' :help 'cinwords'


16

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. However, you may be able to avoid getting into that situation completely by using one of the following techniques: Use o/O to start a new line from a previous line ...


15

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 position to the '] mark, which is the last line of the paste. = can also be used from visual mode. Another useful command is ]p, which pastes at the same indent ...


15

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 detecting indent settings. This plugin adds a :DetectIndent command, which tries to intelligently set the 'shiftwidth', 'expandtab' and 'tabstop' options based ...


14

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 call s:html_like_mode() That keys off of the file extension. You can also key off of the filetype, if you have that enabled: au FileType perl setlocal ...


13

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(".")<CR>v\S<CR> nnoremap cu ?\%<C-R>=virtcol(".")<CR>v\S<CR> Use cd to go down and cu to go up. I'm thinking "column up" and "...


12

When you use one of the commands {count}>>, {count}<<, >{motion} or <{motion}, on some lines which have already been indented, and you want their new indentation level to be a multiple of your 'shiftwidth' option value, you can enable the 'shiftround' option, and add this line in your vimrc: set shiftround


12

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 to indenting. If you use !ipsort to sort all lines of the current paragraph, the cursor moves to the top of the sorted paragraph as well.


11

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 be modified (e.g. in a C program, you should use \t to avoid this)! So alternatively you can change all tabs into spaces using the following command: :%s/\t/ ...


11

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 indentation level is 4 when you start the new line, the cursor will be moved to the same indentation level on the next line. In your ~/.vimrc file, you would put set ...


11

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 the unnamed register, ", by default but one can use other registers: "ay " yank into register a "by " yank into register b "+y " yank into clipboard register […]...


11

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


10

First things first; the line below has absolutely nothing to do with Vundle or plugin management: filetype plugin indent on That command does three things: enables filetype detection, enables filetype-specific scripts (ftplugins), enables filetype-specific indent scripts. That line is there because some plugin managers have to make sure filetype ...


9

How about this function? function FloatUp() while line(".") > 1 \ && (strlen(getline(".")) < col(".") \ || getline(".")[col(".") - 1] =~ '\s') norm k endwhile endfunction You could nnoremap gU :call FloatUp()<CR> (gU for "go up"), and then invoke with gU. Here's an explanation: The code ...


9

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 every time we source it. augroup vimrc autocmd! augroup END autocmd vimrc FileType html setlocal shiftwidth=4 tabstop=4


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