As of May 31, 2023, we have updated our Code of Conduct.
75 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 " ...
Martin York's user avatar
  • 6,270
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 ...
EvergreenTree's user avatar
37 votes

Displaying tabs as characters

Highlighting tabs Temporary Tab highlighting For occasional use, one can simply highlight all tabs in a document using the following search / command: /\t To remove the highlighting, simply type :noh ...
Serge Stroobandt's user avatar
32 votes
Accepted

Setting TAB to 2 spaces

Have you sourced your .vimrc since making these changes? When having vim open just type source ~/.vimrc (assuming it is in it's default location) EDIT: This could help too
Gleland's user avatar
  • 491
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 ...
Martin Tournoij's user avatar
15 votes
Accepted

What is the rationale for \r and \n meaning different things in s command?

At the most basic level, there's already an asymmetry between the search and replace portions of :substitute because the former is a regular expression and the latter is text, with specific additional ...
jamessan's user avatar
  • 10.7k
12 votes

How to join lines without producing a space?

Another trick you may try is to use replace. Sometimes this might be useful. %s/$\n//g Scenario: Delete the last character and join with the next line: %s/=$\n\(.\)/\1/g For example, xxxx= 123 ...
insidepower's user avatar
10 votes
Accepted

delete entire line after first whitespace on every line

The two commands below will reduce every line to the first sequence of non-whitespace character followed by a space: from 0.453945 -2.14126e-54 3.40152e-49 101325 214.355 to 0.453945 <-- space ...
romainl's user avatar
  • 37k
10 votes

Displaying tabs as characters

I personally like the use of the plugin indentLine, which displays a vertical line at indentation levels. It is of great help especially in python, where the indentation is important. Among other ...
mike's user avatar
  • 929
10 votes
Accepted

How to remove Neovim trailing white space?

This should do the trick: vim.api.nvim_create_autocmd({ "BufWritePre" }, { pattern = { "*" }, command = [[%s/\s\+$//e]], }) Be sure to read the h:nvim_create_autocmd() for ...
InsertSON's user avatar
  • 196
9 votes
Accepted

What does vim consider a paragragh?

You may want to move to the next section: ]] Documentation: :h paragraph A paragraph begins after each empty line, and also at each of a set of paragraph macros, specified by the pairs of ...
nobe4's user avatar
  • 15.7k
9 votes
Accepted

vim search for arbitrary combination of spaces and tabs

If you're looking for a substitute command: :s/a[ \t]\+b/c/ The default regex mode doesn't see + as an operator. You have to escape it to make it special. You can also use: :s/a\s\+b/c/ \s is Vim'...
muru's user avatar
  • 24.5k
9 votes

How do I replace single whitespaces without affecting multi-whitespace such as indentation in Vim?

The substitution is easier to read with word-boundaries in my opinion, and very magic mode makes typing easier: :%substitute/\v>\s+</_/g
D. Ben Knoble's user avatar
  • 24k
8 votes

How to split a line into multiple lines efficiently?

The easiest way I've found to split lines in Vim is the normal mode command gq (type both letters in quick succession in normal or visual mode). In visual mode, it will split whatever is selected, in ...
vdicarlo's user avatar
  • 113
8 votes
Accepted

SpecialKey foreground color w/ cursorline set

I've been struggling with that behavior for a long time. Basically, the foreground color of SpecialKey on the current line is changed to the foreground color of Normal if: the cursorline option is ...
romainl's user avatar
  • 37k
8 votes
Accepted

How to show illegal whitespace (such as unbreakable space) in source code?

List & Listchars 'list' is a boolean option that, when set, instructs vim to display whitespace characters. And it is completely customizable using 'listchars'. For example, to show non-breaking ...
D. Ben Knoble's user avatar
  • 24k
8 votes
Accepted

Moving to the end of the last word on a line

g_ will to go to the last non-blank character of the line For more help see :h g_
Peter Rincker's user avatar
8 votes
Accepted

What does breakat&vim mean?

It resets the breakat option to the Vim default: ^I!@*-+;:,./?. Where ^I stands for the tab character. You get the corresponding help paragraph with the :help :set-default command. :se[t] {option}&...
Vivian De Smedt's user avatar
7 votes
Accepted

Expand <TAB> into 2 spaces when and only when editing HTML

Put this in your .vimrc file: autocmd BufRead,BufNewFile *.htm,*.html setlocal tabstop=2 shiftwidth=2 softtabstop=2 Briefly, autocmds get processed when the specified events occur for the specified ...
B Layer's user avatar
  • 19.1k
6 votes

How to add space around a symbol using vim?

I'm not sure why you don't want to use search/replace, but here's a reusable command: command! HTMLArgEq %s/\s\@<!=\+\s\@!/ \0 /g It uses search/replace, but you only need to create it once. ...
Tommy A's user avatar
  • 6,610
6 votes
Accepted

Stop vim from removing whitespace on save for diff files

Since there was nothing explicit in my .vimrc that acted on only-whitespace lines and the diff syntax file did no such thing either, this had to with my plugins somehow. I find out that I had an ...
oligofren's user avatar
  • 1,013
6 votes

What is the rationale for \r and \n meaning different things in s command?

A NUL byte is a string terminator in C, and for this reason Vim uses this convention, described in the manual at :h NL-used-for-Nul: <Nul> characters in the file are stored as <NL> in memory. ...
Sato Katsura's user avatar
  • 3,949
6 votes
Accepted

Moved from vim to neovim and now <BS> deletes 4 spaces at a time, instead of 1 as I'm used to. How to fix?

Neovim sets smarttab by default, which will cause this behaviour. Try turning it off: :set nosmarttab
Rich's user avatar
  • 30.4k
5 votes

delete entire line after first whitespace on every line

If you wanted to delete entire line starting with a space, This pattern will be useful. :g/^\s.*/d g -global ^ - start of the line \s - space .* - anything after that d - delete If you want the ...
SibiCoder's user avatar
  • 3,212
5 votes
Accepted

From a TextChanged autocommand event, how to get the range that has been changed?

Is your concern about performance? Calling out to Python code will add unneeded overhead that would make an attempt at performance optimization moot. In any case, you can get the boundaries with ...
Tommy A's user avatar
  • 6,610
5 votes
Accepted

How does vimscript know which buffer these buffer-local variables use?

if I have two files open, one with b:dostrip set to 1, and one 0. How does StripTrailingWhitespace know which buffer it's operating on? b: always relates to the currently active buffer. It's exactly ...
Martin Tournoij's user avatar
5 votes

Displaying tabs as characters

You may want to show tabs differently in a regular terminal and gvim. set list! if has('gui_running') set listchars=tab:▶\ ,trail:·,extends:\#,nbsp:. else set listchars=tab:>.,trail:.,...
ideasman42's user avatar
  • 3,613
5 votes

Displaying tabs as characters

This can be easily achieved using listchars by specifying the tab as: ,tab:\ \ ┊, By providing two escaped spaces in front of your tab declaration, vim will repeat the spaces. This is opposed to the ...
Swivel's user avatar
  • 151
5 votes

What's the simplest way to strip trailing whitespace from all lines in a file?

Exapnding on Christopher Bottoms's answer a bit: Jonathan Palardy wrote a good article on this. In it, he writes a functions, Preserve(command), that preserves the state of the editor (mainly cursor ...
Alex's user avatar
  • 1,253

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