I'm using Prettier to automatically reformat JavaScript (well, TypeScript, but same deal in this situation) source-code.

It enforces a comment-style like this, with leading spaces replaced with ·:

····*·Foo bar baz,
····*·something something!

Note that in a three-space width, the * is centered. This is contrary to what Vim likes to do with the default 'comments' setting of s1:/*,mb:*,ex:*/. For example, when typing exactly the keys <Space><Space><Space>/* test<CR>test2<CR>|, you end up with the following:

   * test
   * test2
   * |

This wouldn't be a huge problem, as I can depend on Prettier to undo that hot mess when it reformats the source-code. However, what is a huge problem, is that this screws up the 'textwidth': a line that is within 'tw' when the asterisk is misaligned can become outside the 'tw' once reformatted to align the asterisks.

To fix this, I've attempted adding the whitespace to the 'comments' setting, as described in :help option-backslash and :help 'comments':

:set comments=s1:/*,mb:\ *\ ,ex:\ */,://,b:#,:%,:XCOMM,n:>,fb:-

Unfortunately, not only does this not work as expected, but it completely breaks the fo+=j line-joining behaviour — the * gets retained when joining comment-lines.

How can I use Vim to write comments within a fixed text-width, while also using Prettier?

1 Answer 1


What version of vim are you using?

With vim -Nu NONE and :filetype plugin indent on, :syn on I don't have that issue:

enter image description here

Default set comments I have for javascript is

  comments=sO:* -,mO:*  ,exO:*/,s1:/*,mb:*,ex:*/,://
  • I did have this issue with vim --clean and setlocal comments=s1:/*,mb:*,ex:*/, at least I think I did, lol
    – D. Ben Knoble
    Commented Sep 1, 2021 at 21:47

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.