Podcast #128: We chat with Kent C Dodds about why he loves React and discuss what life was like in the dark days before Git. Listen now.
179

The "simplest" way is to just use :substitute: :%s/\s\+$//e :%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). However, ...


87

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 ...


73

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 ...


26

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 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 ...


23

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. ...


21

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 inserted after : by Vim, to target the selection) (2nd) Update: Building on the above (and Sato Katsura's comment), here's a possible "interactive join" ...


18

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 You get: foo bar baz If you have hlsearch enabled and you don't want to clutter ...


17

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 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. Experiment 1 You can do this ...


12

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 All the files in the current directory (recursively use **/*.*): $ ex +'bufdo!%s/\s\+$//e' -cxa *.* Python way: :py import vim :pydo vim.current.buffer[linenr ...


10

You should the this :set listchars+=precedes:<,extends:> Ref: :h wrap :h 'listchars'


9

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! ModifyTextWidth() if getline(".")=~'^.*\[.*\](.*)$' " If the line ends with Markdown link - set big value for textwidth setlocal textwidth=500 ...


9

I like this one augroup comment_textwidth autocmd! autocmd TextChanged,TextChangedI * :call AdjustTextWidth() augroup END function! AdjustTextWidth() let syn_element = synIDattr(synID(line("."), col(".") - 1, 1), "name") let &textwidth = syn_element =~? 'comment' ? 72 : 79 echo "tw = " . &textwidth endfunction Source For more ...


8

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 ...


8

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 ...


8

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 becomes: xxxx123


6

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. myobj.myfunc( "Some string parameter that goes on and on and on ...


6

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. function ReformatMultiLines() let brx = '^\s*"' let erx = '"\s*$' let fullrx = brx . '\(.\+\)' . erx ...


6

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 ...


6

'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: setlocal textwidth=0 You can set other fortran setting you wish to override here as well. You can also use :verbose to see where an option was ...


5

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: function! Preserve(command) " Preparation: save window state let l:saved_winview = ...


5

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 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 ...


5

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 :au ...


5

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. ...


5

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 foo $ wc ...


5

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 or without trimming the leading/trailing whitespaces Join lines with negative count (backwards join) Join lines in reverse Join lines and keep joined ...


5

I got the macro working: qqgg5j^V4j$xgg$p9jV4kd 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 ...


4

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 ...


4

Try set textwidth=0 to not break up long lines. See :h tw. Note that if you set the textwidth to 0, then wrapmargin might take effect, see :h wm, so consider also using set wrapmargin=0.


4

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. ...


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