Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you insert a newline after every 80 characters in vim?

Tags:

vim

I have a long line of characters in vim. I'd like to insert a newline after 80 characters. How do I do that?

like image 975
Raffi Khatchadourian Avatar asked Jun 27 '14 20:06

Raffi Khatchadourian


People also ask

Does Vim add newline at end of file?

A sequence of zero or more non- <newline> characters plus a terminating <newline> character. And, therefore, they all need to end with a newline character. That's why Vim always adds a newline by default (because, according to POSIX, it should always be there). It is not the only editor doing that.

How do I break a line in vi?

To break a line without affecting text, move the cursor to a space where you want the line to break and type r (for "replace") followed by Return . Note that if you type r with the cursor on a character and then press Return, that character will be replaced by the Return.


1 Answers

:%s/.\{80}/&\r/g
  • %: process the entire file
  • s: substitute
  • .: matches any character
  • {80}: matches every 80 occurrences of previous character (in this case, any character)
  • &: the match result
  • \r: newline character
  • g: perform the replacement globally
like image 61
Raffi Khatchadourian Avatar answered Oct 14 '22 09:10

Raffi Khatchadourian