285
votes
What's the simplest way to strip trailing whitespace from all lines in a file?
The "simplest" way is to just use :substitute:
:%s/\s\+$//e
:%s to run :substitute over the range %, which is the entire buffer.
\s to match all whitespace characters.
\+ to match 1 or more ...
- 59.8k
133
votes
Accepted
How to join lines without producing a space?
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 ...
- 59.8k
103
votes
Accepted
What's the simplest way to strip trailing whitespace from all lines in a file?
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 ...
- 3,602
38
votes
Can I join lines with a comma (or another character)?
A simple way is this: simply select your lines (all but the last one) - or use % - and run:
:'<,'>s/\n/,/
or
:'<,'>s/\n/, /
(where, of course, the '<,'> part was already ...
- 9,510
35
votes
Keeping lines to less than 80 characters
If you have Vim 7.3 or later, you can set colorcolumn, or cc.
:set colorcolumn=80
From :help colorcolumn:
'colorcolumn' is a comma separated list of screen columns that are
highlighted with ...
- 3,396
25
votes
Accepted
How to set up Vim to work with one sentence per line?
The 'formatexpr' option
You can achieve this using the 'formatexpr' option. From the help:
Expression which is evaluated to format a range of lines for the gq operator or automatic formatting (see ...
- 7,430
25
votes
Accepted
How to make vim automatically add a newline to the end of a file?
Basic info
Vim doesn't show latest newline in the buffer but actually vim always place EOL at the end of the file when you write it, because it standard for text files in Unix systems. You can find ...
- 2,028
25
votes
How to split a line into multiple lines efficiently?
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 :...
- 4,642
18
votes
What's the simplest way to strip trailing whitespace from all lines in a file?
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
...
- 17.6k
12
votes
How to join lines without producing a space?
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
...
- 231
11
votes
Accepted
Prevent Vim from breaking up links mid-tag in markdown
In the past I had similar problem with function signatures. Here is solution adapted to your problem. Add to .vimrc:
au CursorMovedI *.md call ModifyTextWidth() " Use only within *.md files
function!...
- 468
10
votes
How to make vim automatically add a newline to the end of a file?
Vim only omits the EOL if the file is opened as 'binary' and the 'endofline' option is reset (when the binary file didn't have an EOL when opening, or you explicitly reset the option). In other words, ...
- 17.3k
10
votes
Accepted
Automatically breaking lines in comments?
I like this one
augroup comment_textwidth
autocmd!
autocmd TextChanged,TextChangedI * :call AdjustTextWidth()
augroup END
function! AdjustTextWidth()
let syn_element = synIDattr(synID(...
- 1,131
10
votes
Accepted
How do I configure vim to show a character when a line is too long to be displayed?
You should the this
:set listchars+=precedes:<,extends:>
Ref:
:h wrap
:h 'listchars'
- 15.6k
9
votes
Accepted
How to substitute text across line breaks?
You can use \n to do substitutions across multiple lines. There are also special characters which start with \_ that are the same as their normal counterparts, except it adds newline to their ...
- 7,780
8
votes
How to split a line into multiple lines efficiently?
The easiest way I've found to split lines in Vim is the normal mode command gq (type both letters in quick succession in normal or visual mode).
In visual mode, it will split whatever is selected, in ...
- 113
8
votes
Accepted
What does breakat&vim mean?
It resets the breakat option to the Vim default: ^I!@*-+;:,./?.
Where ^I stands for the tab character.
You get the corresponding help paragraph with the :help :set-default command.
:se[t] {option}&...
- 6,540
7
votes
Keeping lines to less than 80 characters
In addition to the colorcolumn option, you can tell vim to automatically push you down to the next line by putting:
set textwidth=80
into your .vimrc file.
From the docs (:h textwidth)
Maximum ...
- 713
7
votes
how to avoid vim breaking line beyond 80 characters in fortran files
'textwidth' is local to the buffer. I imagine Vim's default fortran plugin ($RUNTIME/ftplugin/fortran.vim) is setting 'textwidth'.
To override this default add the following to ~/.vim/after/ftplugin/...
- 15.3k
6
votes
Keeping lines to less than 80 characters
Instead of showing a line showing the maximum size, the extra characters can also been highlighted through
:let w:m1=matchadd('ErrorMsg', '\%>80v.\+', -1)
This will set the extra characters to be ...
- 2,977
6
votes
How can I reformat a multi-line string in Vim when using the Python filetype?
If this is a regular occurrence, you'd be best off looking for a plugin or using @Sukima 's function. If I were doing this on the fly however, here's what I'd probably do:
Add a newline after the ...
- 2,898
6
votes
How can I reformat a multi-line string in Vim when using the Python filetype?
Oh that's a tough one. I think the best approach is possibly a macro but more likely a plugin. Here is an example I whipped up (I need a better hobby). It seemed to work for me but would need the ...
- 1,172
6
votes
Accepted
Can I join lines with a comma (or another character)?
I wrote a Join plugin, can do what you want, and a lot more.
Except for all features provided by the build-in :join command, Join
can:
Join lines with separator (string)
Join lines with ...
- 1,260
6
votes
Can I join lines with a comma (or another character)?
Here is example using external command:
:%!paste -sd,
where %! replaces current buffer (content of the file) with the command: paste -sd, (parameters -s is used to join multiple lines and -d sets ...
- 17.6k
6
votes
Can I join lines with a comma (or another character)?
Just in case someone doesn't like typing Regex and memorizing more Plugin Ex Commands:
Visual-block Append
Visual-block Append is available in the Visual Block mode (<C-v>) and can do a block ...
- 462
6
votes
Why is a Line Feed converted into a Null character inside the search register and into a Carriage Return on the command line?
First, thank you for this very comprehensive and thoughtful post.
After some testing, I have come to this conclusion:
Control characters are displayed using the caret notation: ^M for <CR> (...
- 181
6
votes
Accepted
What is the significance of "Incomplete last line" when opening a file?
The Unix convention is that a line is a sequence of zero or more characters ending with a newline character. A text file is a sequence of such lines. This is just a convention, but adhering to that ...
- 6,144
6
votes
Accepted
Why does not Vim feed the correct set of newline characters?
There are two problems with your approach.
First is using -b which turns on the 'binary' setting and the binary setting will ignore fileformat. See this snippet of :help 'binary':
Also, '...
- 27.3k
6
votes
Accepted
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
line-breaks × 60wrapping × 13
vimrc × 8
formatting × 8
vimscript × 5
substitute × 4
whitespace × 4
command-line × 3
filetype-markdown × 3
key-bindings × 2
syntax-highlighting × 2
search × 2
indentation × 2
insert-mode × 2
functions × 2
register × 2
save × 2
neovim × 1
cursor-motions × 1
folding × 1
macro × 1
terminal-buffer × 1
variables × 1
filetype-python × 1
git × 1