vim-lsc icon indicating copy to clipboard operation
vim-lsc copied to clipboard

Add support for code formatting

Open cdacheng opened this issue 7 years ago • 5 comments

Is it in the plan for code format?

cdacheng avatar Aug 01 '18 08:08 cdacheng

Yes, I do have plans to add support eventually. I'll keep this issue open to track.

natebosch avatar Aug 01 '18 17:08 natebosch

@Cosson2017 use vim's default functionality equalprg , formatprg, formatexpr.

For example, for c/c++, install clang-format and put the following code in .vimrc

if &ft == c
    setlocal formatprg=clang-format\ --style='Webkit'
    setlocal equalprg=clang-format\ --style='Webkit'
endif

and then either use gq + text object(ap,aw,...) to format selected block or = to format the whole file.

Piping avatar Oct 12 '18 06:10 Piping

@Piping Just FYI (not trying to be Mr. Smarty pants here), the right way to set formatprg, or any other filetype-specific options, is either through autocmd in your vimrc:

" File: ~/.vimrc
augroup myvimrc
  autocmd!
  autocmd FileType c,cpp setlocal formatprg=clang-format\ --stype='Webkit'
  autocmd FileType c,cpp setlocal equalprg=clang-format\ --stype='Webkit'
augroup END

or by using the after/ftplugin/ directory:

" File: ~/.vim/after/ftplugin/c.vim
setlocal formatprg=clang-format\ --style='Webkit'
setlocal equalprg=clang-format\ --style='Webkit'

bfrg avatar Jan 15 '19 04:01 bfrg

@bfrg Thanks! I did put all my code environment steup in a function and then call the function from autocmd! But It is a good point.

Piping avatar Jan 16 '19 20:01 Piping