This question already has answers here:
How can you automatically remove trailing whitespace in vim
(14 answers)
Closed 9 years ago.
I have:
int x = 1;______
(underscores means spaces)
and I would like to get:
int x = 1;
My naive solution is $bld$, is there a quickest way?
In Emacs I use M-\ (delete-horizontal-space)
For the current line:
:s/\s\+$
For all lines:
:%s/\s\+$
The substitution text can be omitted if blank, so we don't need to write s/\s\+$//.
I do this with a search and replace mapping:
map <leader>W :%s/\s\+$//<CR>:let #/=''<CR>
:%s/\s\+$// deletes all trailing white space and then :let #/='' clears the search register.
:%s/\s\+$//
What it does is it searches for white spaces at the end of the line and replace them by nothing.
Source: http://vim.wikia.com/wiki/Remove_unwanted_spaces
Related
This question already has answers here:
Delete all characters after "." in each line
(5 answers)
Closed 17 days ago.
I want to remove anything from : to the end of line in all of the following lines in Vim:
key1: A
key2_long: B
key3_longerrr: C
key: D
So the result would be:
key1
key2_long
key3_longerrr
key
For a single line I use f:d$. What are better ways to do this for all lines, wihtout repeating f:d$ for all lines?
:%s/:.*//g
It searches and replaces what matches the regex between the first and second / with what is between the second and third in all the file.
you can find more on this in the documentation, look for substitute
If you want to stick with f:d$ (which should be f:D), you can use :normal for a one-off macro:
:,+3normal f:D
or:
vjjj:normal f:D
See :help :range and :help :normal.
This question already has answers here:
VIM: Deleting from current position until a space
(6 answers)
Closed 4 years ago.
I would like delete everything from the cursor until the end of the bracket
For example: (the cursor is after "is")
[this is my text] -> [this is]
How can I do?
I would something like di]..., but don't delete the text before the cursor
To delete text from the cursor until a certain character ] in normal mode, use dt] or df]. The difference between t and f is that f will also delete ].
Assuming your curser is after is in [this is my text], typing
dt]
in normal mode will give you [this is] as result.
If you want to delete the bracket ] too, type
df]
instead to get [this is.
This question already has answers here:
Delete n lines in the up direction in vim
(5 answers)
Closed 9 years ago.
Is there a way to apply a vim command backward? For example, I want to kill 5 lines backward, instead of 5dd, is there something like -5dd?
From :he d:
["x]d{motion} Delete text that {motion} moves over [into
register
x]. See below for exceptions.
How about 5dk 4dk (k being the motion for upwards)?
Edit: changed count to 4 as this results in 5 lines being deleted apparently...
This question already has answers here:
Make Vim show ALL white spaces as a character
(23 answers)
Closed 9 years ago.
I use spaces over tabs. In Sublime Text 2, I would have leading spaces show like so:
I have my .vimrc setup to show tabs, line endings, etc. But I'm not sure how to replicate what I have in Sublime. It was handy as I could still see indentation much more easily when just using spaces.
Here's my line for it now:
set listchars=eol:¬,tab:→→,extends:>,precedes:<
The list+listchars combo can show trailing spaces but not leading spaces.
You could try vim-indent-guide.
This question already has answers here:
How do I join two lines in vi?
(9 answers)
Closed 9 years ago.
I'm sure this has been asked before, but I haven't had much luck with a search. Is there an easy way to append the next line to the end of the current line?
Example:
this.thing = that
.getThing();
I am looking for a one-stroke command that turns it into:
this.thing = that.getThing();
Shift-J joins the next line with the current one. It removes indentation but usually adds a space, unfortunately.
These commands, except "gJ", insert one space in place of the <EOL> unless
there is trailing white space or the next line starts with a ')'. These
commands, except "gJ", delete any leading white space on the next line.
Use J which is short for join.
Vim doc copied below
*J*
J Join [count] lines, with a minimum of two lines.
Remove the indent and insert up to two spaces
Adding to the other answers, if you want to keep the cursor position you can use a mark:
:nnoremap J mzJ`z
mz - add mark to register z
J - join as usual
`z - go to the mark set