1

The context

I've the following options in my vimrc

set formatoptions=cnroqj
set textwidth=80

When editing tex files, lines whose content is longer than &textwidth is splitted and formatted as it follows

% - foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo
%   foo foo foo foo foo foo foo foo foo foo foo foo
%
% 1. bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar
%    bar bar bar bar bar bar bar bar bar bar bar 

When editing cpp files, lines whose content is longer than &textwidth is splitted and formatted as it follows

// - foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo
// foo foo foo foo foo foo foo foo foo foo foo 
//
// 1. bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar
//    bar bar bar bar bar bar bar bar bar bar bar bar 

As you can see, in files whose filetype is cpp, lists present in comments lines that start with the - character are not formatted when reaching the value of &textwidth just as happens with files whsoe filetype is tex.

The question

How can I make lists from comment lines that start with the - character to be automatically indented in files whose filetype is cpp?

Additional context

  1. I've noticed that the /usr/share/vim/vim82/ftplugin/c.vim file sets the value of the formatoptions option for cpp files. However, even when I set the same value for the &formatoptions option in both buffers (tex and cpp) I got the same behavior.

  2. Some people recommend modifying the comments option, modifying the formatlistpat option (also here), setting the autoindent option but I'm not sure which one I must modify for this specific case.

2 Answers 2

2

Add this to your after/ftplugin/c.vim or after/ftplugin/cpp.vim:

let &l:formatlistpat .= '\|^\s*-\s*'

It tell vim to recognize - as numbered list.

set them in vimrc is a mistake, this option is local to buffer. You can use :verbose setlocal formatlistpat to check if it's set properly.

Result:

// - foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo
//   foo foo foo foo foo foo foo foo foo foo foo 
//
// 1. bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar
//    bar bar bar bar bar bar bar bar bar bar bar bar 

when you execute gqip, vim first format them as comments(see :h format-comments if you are interested), then it formats then as numbered list, I guess the // is ignored durring pattern match for :h formatlistpat

1

You can also get this behavior with this minimalist vimrc

set nocompatible
filetype on

set textwidth=80
set formatoptions=c

let g:tex_flavor="latex" 

au Filetype tex set comments=sO:%\ -,mO:%\ \ ,:%
au Filetype cpp set comments=sO:\/\/\ -,mO:\/\/\ \ ,:\/\/

When editing tex files you would get the following

% - foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo
%   foo foo foo foo foo foo foo foo foo foo foo

When editing cpp files you would get the following

// - foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo
//   foo foo foo foo foo foo foo foo foo foo foo

Your Answer

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

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