The "simplest" way is to just use :substitute:
:%s to run :substitute over the range %, which is the entire buffer.
\s t match all whitespace characters.
\+ to repeat them 1 or more times.
$ 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 combination 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 wanted 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 ...
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 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'.
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 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 ...
Yes, look at :help listchars.
listchars is a string that will be parsed when list is set to determine what to render for certain special characters. You can set listchars in your .vimrc like so:
This will use the ! character to show the end of every line, tabs like >=== (assuming four-space tabs), and ...
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 ...
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.
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 ...
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 ...
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.
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.
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. Personally I find adding indentation settings here a bit surprising though.
At any rate, to override this use this in your vimrc:
You can omit tab from listchars, but that will cause vim to fall back on the default (showing tabs as ^I). So using tab:\ \ as you have is a reasonable way to keep them "invisible" when list is set.
The characters shown with list are highlighted with the SpecialKey or NonText groups, depending. Tabs, in particular, use SpecialKey, so it sounds like your ...
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
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 ...
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:
Using vim's substitution is probably one of the better ways to accomplish your task, but you can use a macro as well. Suppose we have the following data, with the cursor somewhere on line 1:
1 a b c d e f
Starting in normal mode, hitting qq will start recording a macro callable via @q. Anything typed after qq will be repeatable via aforementioned call. ...
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 position and last search pattern) while running an arbitrary command:
" Preparation: save window state
let l:saved_winview = ...
Here's what I would do. Visually select all of your HTML you want to do this to, and hit the following:
Note that <C-v>, <esc>, and <space> are keystrokes, not literal text.
:norm Means apply the following set of keystrokes to every selected line. f= means move to ...
You can use a mapping and replace all spaces by e.g. \_s* which would match linebreaks as well. Something like this should work:
if getcmdtype() =~# '[?/]'
return substitute(getcmdline(), ' \+', '\\_s*', 'g')
:cmap <F7> <C-\>eReplaceSearch()<CR>
This maps F7 in ...
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 ...