The "simplest" way is to just use :substitute:
:%s to run :substitute over the range %, which is the entire buffer.
\s t match all whitespace characters.
\+ to repeat them 1 or more times.
$ 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 combination 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 wanted 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 ...
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 ...
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.
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" ...
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 ...
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 ...
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
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:
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 ...
'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 ...
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 = ...
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
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. ...
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 ...
I got the macro working:
qq starts a macro called q
gg sets the startingpoint to the first line of the file
5j goes down 5 lines (the start of the block below it)
^V4j$ goes into column selection mode and selects the first block
x cut the content of the selection
gg$p paste it to the end of the first block
9jV4kd delete the empty ...
So as we troubleshooted in the comments the problem came from the formatoption value.
According to :h formatoptions :
This is a sequence of letters which describes how automatic
formatting is to be done.
Your settings contained the letter l which according to :h fo-table does the following:
Long lines are not broken in insert mode: When a line ...
Like so many people, you seem to mix up "new line" and "newline".
When editing a file where the last line doesn't end with a "newline" character, Vim will add that "newline" character on write unless the 'noeol' and 'binary' option are set. So, basically, you probably always have that "newline" character at the end of your files, whether you see it or not.