In this blog post we take a quick look at how to quickly format selected text and even entire buffers with just a few keystrokes.
Formatting Selected Content
We will demonstrate using the following un-formatted HTML file:
We first move the cursor to the first line we would like to format:
Then create the selection by entering Visual Mode v and moving down a few lines:
Finally, hitting = formats the selected lines:
Note that Vim formatted only the lines contained within the selection (including the entire lines containing the cursors), but didn't format the other lines.
Entire Buffer
We can extend this example to formatting entire buffers. Starting with the same buffer as the previous example:
The first step is to execute gg, which moves the cursor to the top of the buffer:
Next, we hit =, which tells Vim to format the lines traversed by the associated motion. We follow this with G, which indicates that we want to apply formatting from the current line to the bottom of the file:
The end result shows that the entire buffer was formatted.