267 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 ...
user avatar
122 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 ...
user avatar
97 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 ...
user avatar
33 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 ...
user avatar
  • 3,226
32 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 ...
user avatar
  • 9,250
24 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 ...
user avatar
  • 7,140
23 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 :...
user avatar
  • 4,452
21 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 ...
user avatar
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 ...
user avatar
  • 16.5k
11 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 ...
user avatar
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!...
user avatar
  • 438
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(...
user avatar
  • 1,111
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'
user avatar
  • 15k
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 ...
user avatar
9 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, ...
user avatar
  • 16.6k
7 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 ...
user avatar
  • 103
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 ...
user avatar
  • 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/...
user avatar
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 ...
user avatar
  • 2,818
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 ...
user avatar
  • 1,142
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 ...
user avatar
  • 2,697
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 ...
user avatar
  • 1,240
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 ...
user avatar
  • 16.5k
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> (...
user avatar
  • 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 ...
user avatar
  • 5,854
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, '...
user avatar
  • 25.3k
6 votes
Accepted

Join all non-blank lines

Maybe it sounds too simplistic, but why not %!fmt -9999
user avatar
  • 15.4k
5 votes

How to split a line into multiple lines efficiently?

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 ...
user avatar
  • 491
5 votes

What's the simplest way to strip trailing whitespace from all lines in a file?

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 ...
user avatar
  • 1,134

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