The "simplest" way is to just use :substitute:
:%s to run :substitute over the range %, which is the entire buffer.
\s to match all whitespace characters.
\+ to match 1 or more of them.
$ 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).
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 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 left as ...
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 ...
If you have Vim 7.3 or later, you can set colorcolumn, or cc.
From :help colorcolumn:
'colorcolumn' is a comma separated list of screen columns that are
highlighted with ColorColumn hl-ColorColumn. Useful to align text.
You can also set it relative to your textwidth variable, which is maybe more
useful than setting it to an ...
A simple way is this: simply select your lines (all but the last one) - or use % - and run:
(where, of course, the '<,'> part was already inserted after : by Vim, to target the selection)
Building on the above (and Sato Katsura's comment), here's a possible "interactive join" ...
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 'formatoptions'). When this option is empty 'formatprg' is used.
Setting your 'formatexpr' correctly will work both when using gq and while inserting text.
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
If you have hlsearch enabled and you don't want to clutter ...
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 more information about this here. In short you don't have to worry about the absence a new lines at the end of the file in vim.
You can do this small ...
To delete all trailing whitespace (at the end of each line), you can use the command:
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 *.*
:py import vim
:pydo vim.current.buffer[linenr ...
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 character class. For instance, \_s is the same as \s, except it also matches newline characters. Here is your search and replace statement with this special ...
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, Vim only honors a missing EOL for binary files. For text files, it always writes with an (added) EOL. (That's why you need solutions like my PreserveNoEOL ...
Another trick you may try is to use replace. Sometimes this might be useful.
Scenario: Delete the last character and join with the next line:
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
if getline(".")=~'^.*\[.*\](.*)$' " If the line ends with Markdown link - set big value for textwidth
'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/fortran.vim:
You can set other fortran setting you wish to override here as well.
You can also use :verbose to see where an option was ...
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 opening paren and before the closing paren so that strings are on separate lines.
"Some string parameter that goes on and on and on ...
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 python indenting plugin to indent properly. Try it out.
let brx = '^\s*"'
let erx = '"\s*$'
let fullrx = brx . '\(.\+\)' . erx
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 convention allows text-processing tools to work together. It avoids problems of ambiguity in commands such as cat foo bar when the last character of foo is not a ...
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, 'fileformat' and 'fileformats' options will not be used, the file is read and written like 'fileformat' was "unix" (a single <NL> separates lines).
The second part ...
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:
" Preparation: save window state
let l:saved_winview = ...
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 normal mode, you follow gq with a motion.
For example, gql will split one line to the currently set width. To set the width of the split lines to be different ...
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. ...
In addition to the colorcolumn option, you can tell vim to automatically push you down to the next line by putting:
into your .vimrc file.
From the docs (:h textwidth)
Maximum width of text that is being inserted. A longer line will be
broken after white space to get this width. A zero value disables
this. 'textwidth' is set to ...
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 marked as error (normally, red background, but it depends on the color scheme).
This can be added to .vimrc to do it automatically on each file
As Alexander mentioned, Vim doesn't show EOL at EOF, that's why it's so confusing (especially with empty/blank new line which is another thing), therefore it actually writes every-time on file save.
Here is simple test how to test if that works:
$ printf foo > foo.txt
$ cat foo.txt
foo$ wc foo.txt
0 1 3 foo.txt
$ vim -cwq foo.txt
$ cat foo.txt
$ wc ...
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
Join lines with separator (string)
Join lines with or without trimming the leading/trailing whitespaces
Join lines with negative count (backwards join)
Join lines in reverse
Join lines and keep joined ...
Here is example using external command:
where %! replaces current buffer (content of the file) with the command: paste -sd, (parameters -s is used to join multiple lines and -d sets the delimiter character to ,).
Just in case someone doesn't like typing Regex and memorizing more Plugin Ex Commands:
Visual-block Append is available in the Visual Block mode (<C-v>) and can do a block Append (and block Insert for prepend). (ref: :help v_b_A, :help v_b_I)
And if the last motion in Block Visual Mode is $, the append will be done at the end of ...