Home > vim > Format text in vim in 5 seconds

Format text in vim in 5 seconds

Problem
You use vim and you don’t want any lines to be longer than 80 characters. However, when you select a paragraph in the browser and you paste it in vim, the whole paragraph is inserted in one long line. Since you are just as lazy as me, you don’t want to break the line manually. What to do?

Solution
Select the line in visual mode (Shift + V). At the bottom you should see “-- VISUAL LINE --“. Press ! (now at the bottom you should see :'<,'>!), type fmt (thus you have :'<,'>!fmt at the bottom) and press Enter. Done.

What just happened?
We passed the selected text to the external Unix command fmt, which reformatted the text. Vim then inserted the output of fmt to the place of the selected text.

So, if you have a list of words, one word in a line, and you want to sort them, just pass them to the external command sort: select the lines visually, !sort and Enter.

[ reddit discussion ]

Advertisements
Categories: vim Tags: , , ,
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: