Keep indented in comments in C ++ in vim

Is it possible to configure vim and cindent not to indent the C ++ comments when the file is pressed again (gg = G)?

I have some formed lists in comments consistent with 4 spaces, but vim interprets this as a bad indent and rebuilds everything.

For instance:

/**
    my list:
         * item 1
         * item 2
 */

becomes:

/**
    my list:
    * item 1
    * item 2
*/

I want to find out vim: "Don't touch the contents of the comments, but back off everything else."

This is important because our project uses markdown doxygen, such as a parser to generate documentation, and indents are used at list levels.

+4
source share
2 answers

, vi stackexchange :

'cinoptions'.

, , , indentexpr, C-indenting ( cindent()) , .

:

, , . .

function! IndentIgnoringComments()
 let in_comment = 0
  for i in range(1, line('$'))
    if !in_comment
      " Check if this line starts a comment
      if getline(i) =~# '^\s*/\*\*'
        let in_comment = 1
      else
        " Indent line 'i'
        execute i . "normal =="
      endif
    else
      " Check if this line ends the comment
      if getline(i) =~# '\*\/\s*$'
        let in_comment = 0
      endif
    endif
  endfor
endfunction

:call IndentIgnoringComments(), . :.

nnoremap <leader>= :call IndentIgnoringComments()<CR>

command, . (:%s/\s*$//g).

Rich https://vi.stackexchange.com

: https://vi.stackexchange.com/a/13962/13084

+1

, , :

/**
*    my list:
*        * item 1
*        * item 2
*/
+3

Source: https://habr.com/ru/post/1687699/


All Articles