1

I have text in vim that I converted from a pdf file.

The problem I got is that every line within the paragraph is ending in a carriage return and in between the paragraphs there is no empty line to differenciate the beginning or ending of each paragraph. So if I put the text in Microsoft Word I can't make the text smaller and wrap the paragraph around it.

It will be easy if there was an empty line in between paragraphs:

%norm vipJ

But because there isn't, I am thinking of deleting the extra carriage only if the last character is a letter and the column is 65 or more. I know how to search and replace for any letter,

but my question is, how I can execute the search and replace in vim ONLY if last character on the line is on column>65?

UPDATE: So far I managed to do:

:%s/\%>65v\n//g

The problem with that is that as the regex is replacing from the top of the paragraph by the time it comes to the last line, the line was already 'wrap' and obviously the column will be more than 65. I figure it out I should execute the search and replace regex it in reverse, from the bottom of the text to the beginning. How can I do that?

milarepa
  • 759
  • 9
  • 28

2 Answers2

1

/\%>65c\r matches all carriage returns after the 65th byte character.

See :help for %c and %v. Also note the difference between \r(CR) and \n(EOL).

QuestionC
  • 10,006
  • 4
  • 26
  • 44
  • I just updated my question, what I tried so far, which is similar to your answer. What I really need to do now is performing the search in reverse, from the bottom up, any ideas? – milarepa Jul 28 '14 at 20:00
  • how about using `tac > tempfile && vim tempfile` and working in there? – sjas Jul 28 '14 at 20:02
  • 1
    @sjas I think there should be a way to do that in vim. – milarepa Jul 28 '14 at 20:07
1

Well, you could just add a line between paragraphs.

%s/\%<65c\n/\r\r/g adds a newline to any line shorter than 65 characters.

PS. The switching between \n and \r was confusing to me but apparantly it's correct.... Why is \r a newline for Vim?

Community
  • 1
  • 1
QuestionC
  • 10,006
  • 4
  • 26
  • 44