Using with vim substitute command: lines created are not scanned in future steps. Can they be?
Task: make a file made with long lines be wrapped to a desired width.
Bad solution: 1. Set textwidth variable with the desired width 2. Visually select the whole file: ggVG 3. Do the command gq This is a horrible solution because it will join consecutive lines that are already smaller that the wanted maximum width we set up in step 1. Soso solution: 1. Do the command :%s_^\(.\{44\}\)\([^.]\)\(.\+\)_\1\r \2\3_ge This is not a complete solution because it will split each line just one time, each time the command is run. So, if a line is longer than 2 times the wanted width, you will end up with a line with the wanted width, and the rest of it in a line below it. To have this line also splitted, we have to repeat the command. But if the lines of our first run of the command are 3 times longer than the wanted width, we will still have longer lines, in the end. So, we run the command again, and again, and again... recursively, until all lines are good. Do you know a solution in Vim for this? Example file given below, with very long lines, good to use with the example command. Each line contain numbers from 00 to 99, to easierly seeing the results of tests: Code:
00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
If I understand your requirement (?), try this page, particularly the first soln: https://stackoverflow.com/questions/...racters-in-vim
|
Not a Vim solution (as you were already given one), but have you heard of fold? It can break at spaces and count each tab as eight characters. Well, you can invoke it from Vim with
Code:
:%!fold |
Quote:
Code:
:%!fold |
Quote:
1. We choose textwidth=14 2. We have the file: Code:
33 66 999 Code:
33 66 999 33 |
Quote:
:%s/.\{80}\($\)\@!/&\r/gec It is great because it splits a line recursively. It is bad because it splits words. Any of you know how to make a regex to improve the command above, so it splits the line in the last space character before 80 (for the given example), or in the 80th position, if there is no space in the line? Or maybe a vim macro, which i use very limitedly) to the following algorithm? 1. Define desired width N. 2. If line is visually longer than N (tabs), it is a candidate. Else, go to next line, repeat this step. If no more lines, we finished. 3. Find the last space (space char or tab) before Nth character. 4.1. If no space was found, insert "\r " (newline + 4 spaces), and write the rest of the current line in it. 4.2. Else, a space was found. Remove this space and all spaces or tabs together, before it. Insert "\r " (newline + 4 spaces), and write the rest of the current line in it. 5. Change current line to the line created in 4. Go to step 2. |
So, have you tried fold? Specifically, fold -s?
Quote:
Code:
:%s/\v.{72}\S{,8}($)@!/&\r/g |
Quote:
Code:
:%!fold -s -w [N] 1. File with 9 lines: first 3 with a word of 10 chars, a space, a word of 9 chars, a space, another word of 9 chars; next 3 lines with a word with 20 chars, a space, a word of 9 chars; 3 short lines with 3 words each. All lines in the file are consecutive. Code:
1234567890 234567890 234567890 Code:
:%!fold -w 14 -s Code:
1234567890 So, the only difference i want, is to put some tabbing to splitted parts of lines, to help us know when a line is possibly the continuation of a line we just read. |
"fmt -t" does indentation.
|
Quote:
Quote:
|
you can always implement your own fold like tool to do exactly what you need.
|
Quote:
File: Code:
1234567890 234567890 234567890 Code:
:%!fmt -t -w 14 Code:
1234567890 |
Quote:
|
Quote:
|
Quote:
|
All times are GMT -5. The time now is 11:28 PM. |