Say I have a super long line in the VIM editor (say around 300+ characters). How would I break that up into multiple lines so that the word boundaries roughly break at 80 chara
To split long lines in the complete document without removing already present line breaks, use:
:set formatoptions+=w
:set tw=80
gggqG
First set your vim so that it understands that you want 80 characters:
:set tw=80
then, hilight the line:
V
and make vim reformat it:
gq
For solid lines of text highlight the area using v in normal mode, then press
:s/\v(.{80})/\1\r/g
This will add a newline at the end of every 80th character.
:s/ replaces within the current select
\v uses regular expressions
(.{80}) selects 80 characters & placed them into group one
\1\r replaces group one with group one and a newline
I manually break up the long line at place. I think where the main point is by pressing "r" (normal mode) then press .
This will make delete a character where cursor is. So remember to do it at the space before the word you want to make a new line, else you will have to insert the missing character.
I just don't know how to break the line and shift it down 2 line space so that there will be space between the 2 lines.
As a quick and nasty, maybe try the following map:
map q 080lwbels<CR><ESC>
which says:
Then hitting q and CR will break the line up into chunks on the word boundary.
If you're on *nix you probably have fold
available.
Select the region you want using v
, then you can break on spaces at width 80 using:
!fold --spaces --width=80
This is esentially the same as using gq
.
However, if you just want to break at character 80 and not be restricted to whitespaces you can use:
!fold --width=80
If you want it with one keystroke just set a mapping - I've used
vmap <f1> !fold --width=80<CR>