We’re rewarding the question askers & reputations are being recalculated! Read more.
178

The "simplest" way is to just use :substitute: :%s/\s\+$//e :%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). However, ...


121

Yes, use listchars: set list set listchars=tab:>- 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.)


87

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 ...


73

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 ...


59

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: %.o: %.cpp ^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). ...


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'.


21

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 ...


17

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 You get: foo bar baz If you have hlsearch enabled and you don't want to clutter ...


15

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 which is short for :nohlsearch —no high lighting. Permanent Tab highlighting " Highlight tabs as errors. " https://vi.stackexchange.com/a/9353/3168 match ...


11

To delete all trailing whitespace (at the end of each line), you can use the command: :%s/ \+$// 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 *.* Python way: :py import vim :pydo vim.current.buffer[linenr ...


10

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 ...


9

It's only a clumsy workaround, but if you :set list the cursor will be positioned on the other "side" of the tab.


9

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.


9

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: ...


8

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 becomes: xxxx123


8

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 characters in the 'paragraphs' option. :h section A section begins after a form-feed (<C-L>) in the first column and at each of a set of section macros, ...


8

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 ...


8

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's shortcut for space and tabs in regex.


6

The cursor in normal mode is on the character not to either side. So you can do i and a to go to insert mode to the left or right of the tab respectively.


6

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 / \...


6

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: [*] # other #stuff trim_trailing_whitespace = true I then subsequently added a section for diff ...


6

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 ...


5

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: set list set listchars=tab:>-,trail:.,extends:#,nbsp:.


5

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:.,extends:\#,nbsp:. endif (Adding for completeness, in case others find it useful).


5

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. ...


5

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: function! Preserve(command) " Preparation: save window state let l:saved_winview = ...


5

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 But your question is a little confusing so I'm not sure I understood what you want. With a substitution: :%s/\(\S\+\s\).*/\1 With a macro: :%normal! WD


5

Here's what I would do. Visually select all of your HTML you want to do this to, and hit the following: :norm f=i<space><C-v><esc>la<space> Note that <C-v>, <esc>, and <space> are keystrokes, not literal text. Explanation: :norm Means apply the following set of keystrokes to every selected line. f= means move to ...


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