1

I do quite often use this mapping:

autocmd FileType cpp nnoremap <buffer> == :%!clang-format -style=file<cr>2g;zz

It reindents the whole file and returns the cursor to the last edit position.

Problem is, if I have folds, they get closed. All of them.

How do I keep each fold's state after this command? Some suggest saving a session, but this seems inefficient, and in my case I neither close the file nor leave vim.

  • 2
    You could save only the view? (:h :mkview) – VanLaser Sep 24 '16 at 18:32
  • Thanks, it worked! If you write it as an answer I'll accept it. And, it removes the need of 2g;zz. – Al.G. Sep 24 '16 at 18:47
  • It's ok to accept your own answer! :) – VanLaser Sep 24 '16 at 19:55
2

Here is how I did it:

autocmd FileType cpp nnoremap <buffer> == :mkview<cr>:%!clang-format -style=file<cr>:loadview<cr>
  • I won't accept this answer as it's entirely based on others' suggestions. – Al.G. Sep 24 '16 at 18:57
1

EDIT: Here's something that might solve the issue. It's really a template of a possible solution (not actually tested).

function! ReindentMyCpp()
  mkview!
  execute 'normal! :nnoremap == :%!clang-format -style=file2g$' . "\r"
  execute 'normal zz'
  loadview
endfun

autocmd FileType cpp ReindentMyCpp()

BEFORE EDIT:

Here's some code that keeps the folds when you leave a buffer and restores it when you come back.

" save folds states in a file 'view'
set viewoptions=folds,cursor
autocmd BufWinLeave *.* mkview!
autocmd BufWinEnter *.* silent loadview 

This works well, and might somehow be adapted to your situation. It works of course if you have some autocmd that invokes mkview to save the fold's state before your own autocmd acts, and another that restores it afterwards.

I'm thinking you might want to define a function that does all three in a row (save the view in this way, do what your original autocmd did, then restore the view in this way), then have your autocmd invoke that function (as to save folds, do the indentation stuff, restore folds). I don't know if this will work for your problem, but it's probably worth a shot.

  • As I said in my question, I don't leave buffers here. I just run clang-format and write it's output back to the file. I managed to keep the folds using mkview as @VanLaser proposed in his comment. I already saw such script in the linked question but didn't think I could use mkview that way. – Al.G. Sep 24 '16 at 18:52
  • What I'm really suggesting is this: use the autocmd that you have, but invoke mkview and loadview from it with your original code "sandwiched" in between. This would probably be better done with a function. In fact, I'm going to try to piece that function together and edit this answer accordingly. – Dalker Sep 24 '16 at 18:55
  • As per your edit - this will reindent code only entering a cpp buffer. I'm using a mapping as I constantly need this; that's why I'm looking for something more efficient. With my current approach, there is a half-a-second full-screen blink. Not that bad, but could be better. – Al.G. Sep 24 '16 at 19:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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