Delete trailing whitespace, alternative to substitute command - vim

To delete trailing whitespace on a line or across a file I can do:
:[range]s/\s\+$//
However, I was wondering for a single line in normal mode if there's an easier approach, for example if the line is:
Hi, I am a line |
And my cursor is past the e, is there a more generic command than doing dTe in normal mode? The next best I could find was dg_, but that goes one too far. And then, one more option might be gElD.

Generic way without :substitute
Use diw to delete multiple whitespace (including tabs) while the cursor is on one of the whitespaces (like in the example).
This works for any whitespace sequence: trailing, leading, etc.
Use dw to delete from the cursor to the end of the sequence. This is useful when you want to re-indent a line or fix alignment.
As this is independent of the first/last character of the previous/next word this is the most generic way.
Using a mapping with :substitute
Use nmap <leader>tr :%s/\s\+$// to remove all trailing whitespace in the whole buffer by pressing \tr (assuming the original mapleader)1.
Replace tr with what ever suits you best.
Omit the the % in the mapping to make it only work on the current line.
--
As a side note: Use set list listchars=trail:· to show trailing whitespace (replace · with any character you like).
--
1 Removing all whitespace may not be what you want, especially when using a version control system and the file contains whitespace not added by yourself.

Related

Deleting to beginning of line - d0 leaves extra space

I am trying to delete an indented line to the beginning of the line, with d0, but that leaves an extra space that I must delete with x.
The use case is that often I want to insert a blank line between two lines, and yes, I could use 'o' or 'O' and 'Esc' but often I enter insert mode out of habit and enter a line. The autoindent in vim adds a line with extra space (even with smartindent) so I am left with some dangling space that I have to delete with 'd0x'.
The extra 'x' seems awkward given that 'D' deletes to the end of the line leaving no extra space, and yes I could use '0D' to do the same with in one less stroke. But I would like your opinions as to the best approach for this situation. Thanks.
Addressing the very specific point of why d0 leaves an extra space: The 0 motion is an exclusive motion, which means the last character towards the end of the region is excluded from the operation.
You can use the v modifier to toggle the characterwise motion and make it inclusive:
dv0
This should remove all the characters from the beginning of the line, including the one under the cursor.
If there's some complicated thing you want to do in vim with minimal keystrokes, the usual approach is to just create a macro for it and bind that macro to a specific key sequence.
For example, for your use case of inserting a blank unindented line, you could just bind to O<ESC>0D (or whatever other key sequence you need). You can test this with the keystrokes (in normal mode, and <ESC> is a single press of the ESC key, not the five individual characters):
qdO<ESC>0Dq
Then just go to some line in your file, enter #d and, voila, an unindented blank line.
To make this permanent, just add it to your vimrc file:
let #d='O<ESC>0D'
where, if you're editing it with vim, ESC can be entered as CTRL-VESC.
Another possibility is to just not worry about indents until some point in the future. By all means, use whatever commands you desire to give yourself a blank line (possibly indented) but either fix that before final write by deleting all trailing tabs and spaces:
:g/[ <TAB>]\+$/s///
or run a script on all files to fix this in a batch operation (even better if this is done as part of automatic pre-checks before source code commit), for example:
find . -name *.cpp -exec sed -iE 's/[ \t]+$//' {} \;

Remove White space in vim, keep indentation

I am looking to remove all white space in vim, but keep the default indentation as is. Currently all out files use spaces instead of tabs so:
content
content inside[Sneeky white space of epicness]
content
Should be:
content
content inside
content
Note: [Sneeky white space of epicness] represents a block of white space.
Use substitute
%s/ *$//g
$ means end of line.
* means match zero or more instances of the previous element (as suggested by Jite)
Be aware that you could have tab... To represent any white space, use \s
%s/\s*$//g
EDIT:
As suggested by kojiro you could use + instead of *. With a *, vim does the replace on EVERY line. With a +, the replace is done only where it needs to be done in that case.
+ means match at least one instance of the previous element.
With vim you have to escape the +.
See :help pattern-overview for more details.
My final answer :
%s/\s\+$//g
There are several plugins that can detect and (on demand, or even automatically) delete trailing whitespace. My DeleteTrailingWhitespace plugin is one of them, and handles way more cornercases that the simple :%s command usually given. (The plugin page has links to alternative plugins.)

How to repeat a substitution the number of times the search word occurs in a row in a substitution command in Vim?

I would like to use tabs in a code that doesn’t use them. What I did until now to implement tabs was pretty handcrafty:
:%s/^ /\t/g
:%s/^\t /\t\t/g
. . .
Question: Is there a way to replace two spaces ( ) by tab (\t) the number of times it was found at the beginning of a line?
There are (at least) three substitution techniques relevant to this case.
1. The first one takes advantage of the preceding-atom matching
syntax to naturally define a step of indentation. According to the
question statement, an indent step is a pair of adjacent space
characters preceded with nothing but spaces from the beginning
of line. Following this definition, one can construct the actual
substitution pattern, right to left:
:%s/\%(^ *\)\#<= /\t/g
Indeed, the pattern designates an occurrence of two literal space
characters, but only when they are preceded by a zero-width match
of the atom just before \#<=, which is the pattern ^ * wrapped in
grouping parentheses \%(, \). These non-capturing parentheses are
used instead of the usual capturing ones, \(, \), since there is no
need in further referring to the matched string of leading spaces. Due
to the g flag, the above :substitute command runs through the
leading spaces pair by pair, and replaces each of them by single tab
character.
2. The second technique takes a different approach. Instead of
matching separate indent levels, one can break each of the lines
starting with space characters down into two lines: one containing
the indenting spaces of the original line, and another holding the
rest of it. After that, it is straightforward to replace all of the pairs
of spaces on the first line, and concatenate the lines back together:
:g/^ /s/^ \+/&\r/|-s/ /\t/g|j!
3. The third idea is to process leading spaces by means of Vim
scripting language. A convenient way of doing that is to use the
substitute with an expression feature of the :substitute command
(see :help sub-replace-\=). When started with \=, the substitute
string of the command enables to substitute the matches of a pattern
with results of evaluation of the expression specified after \=:
:%s#^ \+#\=repeat("\t",len(submatch(0))/2)
If you specifically want to convert spaces into tabs (or vice-versa) at the start of a line, there's the useful :retab command which takes care of that. For example:
:retab! 2 will convert spaces in groups of two to tabs
:set expandtab and then :retab! 2 will convert tabstops (of width 2) back to spaces
See :h :retab (and :h 'ts') for the details.
This is not a general solution for the original problem, but I think it covers the most common use case.
There is no general way of doing this using :s regex's. You can't make the /g modifier look backwards otherwise it'd be unusable, and you can't reliably check that you're at the beginning of the line without looking backwards.
The only way of doing it generally is to loop, like so:
:for i in range(100)
: %s/^\t*\zs /\t/e
:endfor
Which is ugly, slow and highly unrecommended. Use :retab

Why does Vim add spaces when joining lines?

I want to unwrap text in Vim. When I join lines I get an additional space between sentences.
Why is that?
I have a feeling this is what you really want: gJ
From :h gJ:
gJ Join [count] lines, with a minimum of two lines.
Don't insert or remove any spaces. {not in Vi}
This is handy if you've copied something from a terminal and it's pasted it as a big rectangular block into vim, rather than a single line.
I usually use it in visual mode. Hilight stuff, gJ.
Formatting destroys information. There are many different blocks of text which will result in the same one once formatted. Therefore, there's no way to reverse the operation without prior knowledge (i.e. undo).
Unformatted:
Unformatted text could start out as either all one line, or several, yet look the same when formatted.
Unformatted text could start out as either all one line, or several,
yet look the same when formatted.
Formatted:
Unformatted text could start out as
either all one line, or several,
yet look the same when formatted.
If you want your paragraph all on one line, or if you're okay with a little manual fiddling, you can use J to join lines back together. You can use visual mode to apply the J command to several lines at once, perhaps combined with ap or ip to select a paragraph, e.g. vipJ. Again, you'll still lose some information - multiple spaces at line breaks before formatting will end up collapsed to single spaces. (You can actually join without modifying spaces by using gJ instead of J, but you'll already have lost them when you formatted)
If you're bothered by the extra spaces after sentences (lines ending in !, ?, or .), turn off joinspaces: set nojoinspaces
I guess the simple solution to join the lines without spaces between is:
:j!
With ! the join does not insert or delete any spaces. For the whole file, use :%j!.
See: :help :join.
This is the answer that ended up working for me, none of the above worked in my use case.
Essentially, use gJ like multiple others have said, but highlight all of file, so in command mode typing ggVGgJ.
I still got the extra one space after join, if the line we work on does not end with space. Usually this is the desired behaviour. Example
first line without space
second line
after joining with J, become
first line without space second line
Although in some case, we do not wish to apply it,
myInstance->methodA()
->methodB()
And we would want the join to become myInstance->methodA()->methodB() without any space in between!
Here the helpers mapping i use
nmap <leader>jj Jx
<leader> key can be checked with :let mapleader, default to key \ i believe.
so in normal mode, just \jj to perform join without any extra space!

How can I insert text in the middle of the line to multiple lines in Vim?

Say I have ten lines and I want to prepend text to some word that occurs in those lines? It does not have to be at the beginning of the line.
From:
sdfsd foo sdfsd
sfsd foo fsdf
sdfsdf foo sdfsdf
to:
sdfsd bar(foo sdfsd
sfsd bar(foo fsdf
sdfsdf bar(foo sdfsdf
Is it also possible to not only prepend the bar( but actually surround foo with bar(foo)?
I would also like a quick way to append // comments to multiple lines (C-style comments).
I use Vim/GVim 7.2.
Go to the first foo, press Ctrl-v to enter visual block mode and press down until all the lines with foo are marked. Then press Shift-i to insert at the beginning (of the block). When you are finished and press Esc, the inserted characters will be added to each line at the left of the marked block.
To insert at the end, press again Ctrl-v, move up/down to mark all affected lines and then press End or $ to extend the selection until the end of the lines. Now you can press Shift-a to append at the end of all the lines, just like previously with Shift-i.
The visual selection can also be done with normal movement commands. So to comment a whole block in C you could move to the opening brace and type Ctrl-v % Shift-i // Esc.
To answer your first question, the below
:%s/foo/bar(&)/g
will look for foo, and surround the matched pattern with bar(). The /g will do this multiple times in one line.
Since you're just matching foo, you could do a simple :s/foo/bar(foo)/g. The above will work, however, if you decide to match on a regular expression rather than a simple word (e.g. f[a-z][a-z]). The '&' in the above represents what you've matched.
To prefix a set of lines I use one of two different approaches:
One approach is the block select (mentioned by sth). In general, you can select a rectangular region with ctrl-V followed by cursor-movement. Once you've highlighted a rectangle, pressing shift-I will insert characters on the left side of the rectangle, or shift-A will append them on the right side of the rectangle. So you can use this technique to make a rectangle that includes the left-most column of the lines you want to prefix, hit shift-I, type the prefix, and then hit escape.
The other approach is to use a substitution (as mentioned by Brian Agnew). Brian's substitution will affect the entire file (the % in the command means "all lines"). To affect just a few lines the easiest approach is to hit shift-V (which enables visual-line mode) while on the first/last line, and then move to the last/first line. Then type:
:s/^/YOUR PREFIX/
The ^ is a regex (in this case, the beginning of the line). By typing this in visual line mode you'll see '<,'> inserted before the s automatically. This means the range of the substitution will be the visual selection.
Extra tip: if your prefix contains slashes, you can either escape them with backslash, or you can use a different punctuation character as the separator in the command. For example, to add C++ line comments, I usually write:
:s:^:// :
For adding a suffix the substitution approach is generally easier unless all of your lines are exactly the same length. Just use $ for the pattern instead of ^ and your string will be appended instead of pre-pended.
If you want to add a prefix and a suffix simultaneously, you can do something like this:
:s/.*/PREFIX & SUFFIX/
The .* matches the whole line. The & in the replacement puts the matched text (the whole line) back, but now it'll have your prefix and suffix added.
BTW: when commenting out code you'll probably want to uncomment it later. You can use visual-block (ctrl-V) to select the slashes and then hit d to delete them, or you can use a substitution (probably with a visual line selection, made with shift-V) to remove the leading slashes like this:
:s:// ::
:normal to the rescue!
:%norm Wibar(
:%norm WEa)
:norm(al) replays the commands as if you had typed them:
W - goes to the next word
i - starts insertion mode
bar( - types the sequence 'bar('
Or in one line:
:%norm Wibar(ctrlvESCEa)
If you're running Windows then type ctrlq instead of ctrlv.
Yet another possibility (probably not-so-useful in your test case, but handy in other situations) is to cordon off the area you want to change with marks.
Put the cursor anywhere in the top line and press 'a
Put the cursor anywhere in the last line and press 'b
Issue the command :'a,'b s/foo/bar(&)/
I usually like visual block mode if everything is visible on the screen, and I usually prefer marks if the start and stop are separated by many screens.
Another simple regular expression is:
%s/^/<text you want to prepend>/
For the C-style comments, use the regexp answer by Brian, and match on line ending $, and insert away.

Resources