The "simplest" way is to just use :substitute:
:%s to run :substitute over the range %, which is the entire buffer.
\s to match all whitespace characters.
\+ to match 1 or more of them.
$ to anchor at the end of the line.
The e flag to not give an error if there is no match (i.e. the file is already without trailing whitespace).
Yes, use listchars:
If you put these two lines in your .vimrc, tabs will be shown as > for the start position and - through the rest of the tab.
(Sidenote: listchars can also show trailing spaces with trail:x (replace x with the character you want to use for a trailing space), which can be useful as well.)
The gJ mapping does this; from :help gJ:
Join [count] lines, with a minimum of two lines. Don't insert or remove any spaces.
You could rebind it to J, if you want to save a keystroke:
:nnoremap J gJ
Note that this doesn't remove any spaces, so if either the current line ends with a space or next line starts with one or more spaces they will be left as ...
Use a keybinding to strip all trailing whitespace
Since some pages that I edit actually need trailing whitespaces (e.g. markdown) and others don't, I set up a keybinding to F5 so that it's trivial to do without being automatic. To do so, add the code below (from vim.wikia) or some variation of it to your .vimrc:
"Remove all trailing whitespace by pressing ...
The basic answer is :set list, which causes tabs to display as ^I.
However, I recommend going beyond that. Just :set list is problematic in that it fails to preserve visual alignment on screen. For example:
^Ig++ -c $<
doesn't look good, especially when you expect the g of g++ to appear under the first p of cpp (assuming tabstop=8).
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 ...
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'.
Temporary Tab highlighting
For occasional use, one can simply highlight all tabs in a document using the following search / command:
To remove the highlighting, simply type :noh which is short for :nohlsearch —no high lighting.
Permanent Tab highlighting
" Highlight tabs as errors.
You could simply replace any consecutive space on the line with a newline using the command :s/\s\+/\r/g. \s\+ mean one or more whitespace characters, and \r is a newline, see :help :substitute and :help regexp for details. If you run the command on a line like:
foo bar baz
If you have hlsearch enabled and you don't want to clutter ...
To delete all trailing whitespace (at the end of each line), you can use the command:
To include tabs, use \s instead of space.
From the command-line:
$ ex +'%s/\s\+$//e' -cwq file.c
All the files in the current directory (recursively use **/*.*):
$ ex +'bufdo!%s/\s\+$//e' -cxa *.*
:py import vim
:pydo vim.current.buffer[linenr ...
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 every time a Python file is loaded.
To override it add this in your vimrc:
" Add shiftwidth and/or softtabstop if you want to ...
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 escape sequences. This is just highlighted by the intuition you have about what \n means.
For example, consider that \n in the search doesn't match a literal \...
Another trick you may try is to use replace. Sometimes this might be useful.
Scenario: Delete the last character and join with the next line:
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 things, it provides a toggle command, that can be mapped to a key combination, so that you can turn it off, when you don't need it.
The two commands below will reduce every line to the first sequence of non-whitespace character followed by a space:
0.453945 -2.14126e-54 3.40152e-49 101325 214.355
0.453945 <-- space
But your question is a little confusing so I'm not sure I understood what you want.
With a substitution:
With a macro:
If you're looking for a substitute command:
The default regex mode doesn't see + as an operator. You have to escape it to make it special.
You can also use:
\s is Vim's shortcut for space and tabs in regex.
You may want to move to the next section:
A paragraph begins after each empty line, and also at each of a set of
paragraph macros, specified by the pairs of characters in the 'paragraphs' option.
A section begins after a form-feed (<C-L>) in the first column and at each of
a set of section macros, ...
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 enabled,
the CursorLine highlight group has a background color set.
I've never been able to fix that behavior, no matter what I tried. The problem lies ...
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 file name patterns. Here we just set the tab values for the current buffer to the desired value in the event of reading or creating a file with .htm or .html ...
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. Explanation:
%s/ - Substitute entire buffer
\s\@<! - Not preceded by whitespace
=\+ - One or more equal characters
\s\@! - Not followed by whitespace
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 EditorConfig file in this project, and in it it had these lines:
trim_trailing_whitespace = true
I then subsequently added a section for diff ...
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. In the display they are shown as "^@". The translation is done when reading and writing files. To match a <Nul> with a search pattern you can just enter ...
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 spaces as ! (you can use whatever character you like, especially with digraphs and unicode support):
set list listchars=nbsp:!
I use 'listchars' extensively, ...
This can be done with :set list. The Characters displayed for the different whitespace chars can be controlled by :set listchars. See the help topics for detailed information. Here's an example from my .vimrc: