Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim static word wrap interactive 'smart' rebuilding

Tags:

vim

Am a long time kate user switching to vim.

Wonder whether vim has an easily activable option (or for it has been coded a plugin) to 'smartly' apply static word wrap to large strings when coding major languages: C/C++, Java, Python, PHP, (more follow).

Not only while writing but also while applying an indentation modification to a visual text block, or (un)?commenting it. Let us have a pseudo-Java situation like:

  1         String loremIpsum = "Lorem ipsum dolor sit amet, consectetur adipi" +
  2                         "sicing elit, sed do eiusmod tempor incididunt ut " +
  3                         "labore et dolore magna aliqua. Ut enim ad minim v" +
  4                         "eniam, quis nostrud exercitation ullamco laboris " +
  5                         "nisi ut aliquip ex ea commodo consequat. Duis aut" +
  6                         "e irure dolor in reprehenderit in voluptate velit" +
  7                         " esse cillum dolore eu fugiat nulla pariatur. Exc" +
  8                         "epteur sint occaecat cupidatat non proident, sunt" +
  9                         " in culpa qui officia deserunt mollit anim id est" +
 10                         " laborum.";
  ~

At some point would want to add or remove some indentation levels, but relying in the editor to rebuild the whole language provisioned string with our static word wrap rules. Suppose now by some reason it is desirable to remove two spaces of indentation, the desired output would be:

  1       String loremIpsum = "Lorem ipsum dolor sit amet, consectetur adipisi" +
  2                       "cing elit, sed do eiusmod tempor incididunt ut labo" +
  3                       "re et dolore magna aliqua. Ut enim ad minim veniam," +
  4                       " quis nostrud exercitation ullamco laboris nisi ut " +
  5                       "aliquip ex ea commodo consequat. Duis aute irure do" +
  6                       "lor in reprehenderit in voluptate velit esse cillum" +
  7                       " dolore eu fugiat nulla pariatur. Excepteur sint oc" +
  8                       "caeact cupidatat non proident, sunt in culpa qui of" +
  9                       "ficia deserunt mollit anim id est laborum.";
  ~

Which is the tool for this to be constructed by vim?

like image 974
1737973 Avatar asked Mar 24 '26 04:03

1737973


1 Answers

With Vim, the gq command reformats lines; this can even be done as-you-type with :set formatoptions+=a.

Unfortunately, Vim's built-in capabilities are limited to basic stuff (see :help fo-table); elaborate and language-specific formatters are meant to be provided by external programs ('formatprg'), or Vimscript ('formatexpr'), the latter one I haven't actually seen used yet.

So, if you're lucky you'll find an external code formatter program that can be integrated, or you'll have to write such a thing yourself.

like image 98
Ingo Karkat Avatar answered Mar 26 '26 18:03

Ingo Karkat