3

I often use the sequence gqap to hard wrap the current paragraph. However I'd like some line to be excluded from the process.

Example

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. <<--no wrap here-->>
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

after gqap it becomes:

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

Is this possible?

  • Which pattern you are going to use for such exceptional lines? – SibiCoder Apr 24 '16 at 12:29
  • I have no real preference. I'm writing a document in latex which use % as comment. So far I've used to insert a single line with a single comment in the middle, which actually works fine. However, this uses a new line which I'd rather avoid. – nicmus Apr 24 '16 at 12:52
  • Then, how to identify which part should be not wrapped as mentioned here? – SibiCoder Apr 24 '16 at 12:57
  • I could use two spaces at the end of a line to instruct vim I want this line to be split exactly there. But I don't really have a preference, that is, I could also use a comment % at the end of a line where I want a break. – nicmus Apr 24 '16 at 13:05
1

I'm assuming you are wanting to not wrap if the line is below textwidth and ends with consequat.

function! ConsequatWrap() abort
  if v:char != '' || mode() == 'i'
    return 1
  endif

  let start_line = v:lnum
  let end_line = v:lnum + v:count - 1

  while start_line <= end_line
    call cursor(start_line, &l:textwidth)

    if col('.') < &l:textwidth && getline('.') !~? 'consequat\.$'
      normal! J
      let end_line -= 1
      continue
    endif

    if col('$') >= &l:textwidth
          \ && (search('consequat\.\%(\_$\)\@!', 'cbeW') == start_line
          \     || search('\s', 'cbeW') == start_line)
      execute "normal! a\<cr>"
      let end_line += 1
    endif
    let start_line += 1
  endwhile
endfunction

set formatexpr=ConsequatWrap()

What's happening above is that lines shorter than textwidth are joined to the current line, unless the line ends with consequat.. Once the line is long enough to break, search backwards from textwidth for a space or an occurrence of consequat., then insert <cr> right after it.

Using formatexpr will allow you to format using any direction or text object.

  • Thanks, exactly what I was looking for, I'm going to try it right away. – nicmus Apr 24 '16 at 13:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.