2

Is there a vimscript function that wraps text in a similar manner like gqgq does?

For example, if I have the following string variable:

let txt = 'One foo, two bars and three bazes went up the hill'

I'd like to call something like:

let indent   = 4
let textwith = 12
let wrapped = wrap(txt, indent, textwidth)

After the call, the value of wrapped should be

    One foo, two
    bars and
    three bazes
    went up the
    hill

Is there something that does that?

2
  • While I didn't found any function, you could create one that paste the text into a buffer, operate over it (there you can gq) and copying it afterward.
    – nobe4
    Commented Sep 2, 2015 at 14:01
  • I was thinking of that, too, but I am going this route only as last resort. Commented Sep 2, 2015 at 14:03

4 Answers 4

2

This function does what I need

fu! TextWrap(text, width, indent)

  let l:line = ''
  let l:ret  = ''

  for word in split(a:text)

    if len(l:line) + len(word) + 1 > a:width

       if len(l:ret)
          let l:ret .= "\n"
       endif
       let l:ret .=  repeat(' ', a:indent) . l:line

       let l:line = ''

    endif

    if len (l:line)
       let l:line .= ' '
    endif

    let l:line .= word

  endfor

  let l:ret .= "\n" . repeat(' ', a:indent) . l:line

  return l:ret

endfu

The function is then called, for example, like so

echo TextWrap("one two three four five six seven eight nine ten", 8, 2)

Which results in

  one two
  three
  four
  five six
  seven
  eight
  nine ten

The parameter indent specifies how many empty (space) characters there are in front of the first word. width specifies the maximum number of characters after indent characters. (for example five six or nine ten each constists of 8 characters).

4
  • Confused on the expected values to be passed in here. do you have an example of the arguments? Assuming: text = "Lorem ipsum dolar sit...", width = 80, indent = ?? Not sure what the value of indent would be here.
    – DebugXYZ
    Commented Aug 13, 2016 at 5:24
  • You must pass a value for indent. Commented Aug 13, 2016 at 14:07
  • Right, just wasn't sure what the indent value should be. Would indent be "\t" or " " or what is it used for. An example function call would be amazing.
    – DebugXYZ
    Commented Aug 27, 2016 at 0:28
  • 1
    I have added an example function call. (and discovered a bug in the function that I have now fixed) Commented Aug 27, 2016 at 7:20
0

No there is no such function and it wouldn't make much sense.

3
  • 1
    Why so ? Can you add details for your answer please ?
    – nobe4
    Commented Sep 3, 2015 at 8:55
  • 1
    VimL function work with datatypes and do not handle the current buffer. Therefore wrapping makes no sense for an string datatype. If you want to wrap in VimScript, use the existing normal mode commands. Commented Sep 3, 2015 at 10:36
  • 1
    @ChristianBrabandt I was not talking about wrapping in a buffer. Despite your assertion, such a function would make much sense for me. Commented Sep 4, 2015 at 13:41
0

I have wrote something a bit alike for indenting with options my mail texts (with a justify option). It's called yaposting:

whithin it there's a mail format tool with enough parameters to do that:

and it is exposed as a function within the vimscript file to do so:

Though I did that plugin a long time ago, before I actually understood vim. If I was to rewrite this plugin, I'd change it to have the python function work within :set formatprg with the parameters of how you want to setup your indentation given as either arguments to set formatprg or another global vim setting. And then use the traditional gqap (and eventually enable other gq- like shortcut to make it available.

But the right thing is to use the "standard" vi thing, which is gqap, and the right tool that formats things how you want it!

HTH

0

Vim's paragraph formatting abilities can be extended by installing an old program called 'par'.

:set formatprg=par\ -w12
" (gq will now format using par. Does not affect gw)

:map <A-q> {v}!par -w12<CR>
" (maps alt-q to format selection using par)

source: http://vim.wikia.com/wiki/Par_text_reformatter

1
  • Not terribly old. Two and a half years since the last update isn't bad considering what it is. Author's Web site is here.
    – MDeBusk
    Commented Nov 6, 2022 at 5:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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