Vim Tips Wiki
No edit summary
Tag: sourceedit
No edit summary
Tag: sourceedit
Line 19: Line 19:
 
==Navigating long lines with Vim's built-in capabilities==
 
==Navigating long lines with Vim's built-in capabilities==
   
Vim can attempt to show long lines with <code>:set wrap</code> (on by default). With <code>:set linebreak</code>, Vim will wrap the lines at characters in <code>'breakat'</code>; this generally makes the text easier to read. Also very useful is <code>:set display+=lastline</code>, which will try to show as much as possible of the last line in the window (rather than a column of "@", which is the default behavior).
+
Vim can attempt to show long lines with <code>:set wrap</code> (on by default). With <code>:set linebreak</code>, Vim will wrap the lines at characters in <code>'breakat'</code>; this generally makes the text easier to read (in older versions of Vim, you will also have to <code>:set nolist</code>). Also very useful is <code>:set display+=lastline</code>, which will try to show as much as possible of the last line in the window (rather than a column of "@", which is the default behavior).
   
 
The normal mode movements <code>j</code> and <code>k</code> move by logical lines, but these have the analogous <code>gj</code> and <code>gk</code> for moving by visual lines. Similarly, <code>g0</code>, <code>g^</code>, and <code>g$</code> all work. In addition, <code>gm</code> will move the cursor to the middle of the visual line.
 
The normal mode movements <code>j</code> and <code>k</code> move by logical lines, but these have the analogous <code>gj</code> and <code>gk</code> for moving by visual lines. Similarly, <code>g0</code>, <code>g^</code>, and <code>g$</code> all work. In addition, <code>gm</code> will move the cursor to the middle of the visual line.
Line 86: Line 86:
 
==See also==
 
==See also==
   
* [[Word wrap without line breaks]]
 
 
* {{help|25.4}}, "Dealing with long lines", which has a couple of parts:
 
* {{help|25.4}}, "Dealing with long lines", which has a couple of parts:
 
** {{help|edit-no-break}}
 
** {{help|edit-no-break}}
** {{help|edit-paragraph-join}} (the version of the help files on vimdoc seems to be old so this won't work; just type it in Vim)
+
** {{help|edit-paragraph-join}} (the version of the help files on vimdoc seems to be old so this won't work; just type it in Vim); this is similar to [[Copy paragraphs without excess newlines to MS Word]]
 
* [[Word wrap without line breaks]]
  +
* [[Chop long lines]]
  +
* [[Format only long lines]]
  +
* [[Move through wrapped lines]]
  +
* [[Move cursor by display lines when wrapping]]
   
 
==References==
 
==References==

Revision as of 07:38, 2 November 2016

This page explains several strategies for dealing with long lines. Vim does not handle long lines well; there are restrictions on when a long line can partially be displayed, and commands like <C-E> act on logical lines rather than visual lines. Part of the reason for this is that Vim is a descendant of line-based editors that only acted on logical lines. The upshot is that Vim will often feel "jumpy" when scrolling and moving in a buffer with long lines, especially ones that fill up the entire window.

You may encounter files with long lines in various situations:

  • Conventions can require it; e.g. the English Wikipedia favors single-line paragraphs so lines tend to be long
  • Some data files (JSON, CSVs, etc.) use long lines
  • Content on the web is often presented in minified form, leading to long lines

At a high level, the strategies for dealing with long lines can be grouped in the following way:

  • Making use of Vim's built-in capabilities for navigating long lines (i.e. work around long lines without modifying them)
  • Preprocessing the file to use shorter lines; the two strategies here are "chopping" and pretty-printing
  • Increasing the terminal window size so that more text can fit in
  • Taking steps to prevent long lines (rewriting code to use shorter lines, enforcing a manageable text width, etc.)
  • Giving up on Vim for these files and using editors that work fine with long lines (gedit, GNU Emacs, etc.)

This article will cover the first two points.

Navigating long lines with Vim's built-in capabilities

Vim can attempt to show long lines with :set wrap (on by default). With :set linebreak, Vim will wrap the lines at characters in 'breakat'; this generally makes the text easier to read (in older versions of Vim, you will also have to :set nolist). Also very useful is :set display+=lastline, which will try to show as much as possible of the last line in the window (rather than a column of "@", which is the default behavior).

The normal mode movements j and k move by logical lines, but these have the analogous gj and gk for moving by visual lines. Similarly, g0, g^, and g$ all work. In addition, gm will move the cursor to the middle of the visual line.

Search-to-navigate is another useful way to move. It's advisable to :set incsearch so that you can see the cursor move as you type, and then hit enter when you get to the desired location.

Note that the commands f, t, F, T, ;, and , are even more powerful within long lines: they can cover more ground.

Using :set scrolloff=99999 (or any large number instead of 99999) will try to keep the cursor in the middle of the screen, providing more context even within very long lines. This will allow you to type something like 5gj to simulate 5<C-E>.

Preprocessing long lines

Generic formatting tools

Most UNIX-like programming environments offer generic tools for formatting text. These include fmt, fold, and par. From inside Vim, one can invoke:

:%!fmt -80 -s

to format the current buffer. The -80 specifies 80 columns as the desired width and the -s tells fmt to split lines only (i.e. to not join short lines together, called "refilling").

fold is more aggressive: it will chop lines even inside of words.

In addition, Vim itself provides the command gqq to format the current line, so something like:

:%normal! gqq

will format the buffer according to the value of 'textwidth'.

If the original linebreak locations must be preserved, you can leave a unique character in place of linebreaks. Below, the character '↵' (typed using <C-V>u21B5) is provided as a suggestion, but it must not appear in the file itself.

command! ShortLines :%s/.\{71}/&↵\r/g | 1
command! ShortLinesAtSpace :%s/.\{70,79} /&↵\r/g | 1
command! LongLines :%s/↵\n// | 1

Note that this sort of regex replacement will be slow on large files.

Filetype-specific tools

Instead of generic tools, one can sometimes do better by using a tool suited to a particular filetype.

If you are working with JSON files and have Python installed (note: this is just the system binary rather than Python support being compiled into Vim):

:%!python -m json.tool

will convert the buffer into pretty-printed JSON. There are also plugins like jdaddy.vim that are designed to make work with JSON easier.

For HTML and XML, there are tools like tidy and xmllint; see Cleanup your HTML for the main article.

In some cases, gg=G might make things nicer.

Some plugins that try to unify this sort of pretty-printing/tidying are:

Languages like Go have strong source formatting requirements and provide tools like gofmt.

See also

References