Skip to content

Instantly share code, notes, and snippets.

@lgalke
Created September 8, 2021 15:40
Show Gist options
  • Save lgalke/8d2a69ee7b69c5c7b34291e005e10965 to your computer and use it in GitHub Desktop.
Save lgalke/8d2a69ee7b69c5c7b34291e005e10965 to your computer and use it in GitHub Desktop.
Command to properly delete all comments from a latex file
function! s:DeleteAllComments() range
" Deletes all tex comments (trailing and full line)
" without changing paragraph formatting.
" Put this function in $HOME/.vim/ftplugin/tex.vim
let l:save = winsaveview() " Save cursor position
" First: Remove full comment lines completely (to retain formatting)
global/\m^\s*%.*$/delete
" Second: Remove trailing comments (but dont match escaped \%)
%smagic/[^\\]\zs%.*//eI
" Third: Join multiple blank lines to one (aesthetics)
%smagic/\(\n\s*\)\{3,}/\r\r/eI
call winrestview(l:save) " Restore cursor position
endfunction
command! -buffer -nargs=0 DeleteAllComments :call <SID>DeleteAllComments()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment