Last active
March 21, 2022 11:22
-
-
Save matveyt/2717fb5cbe1076c1caa0f008b29c6c6e to your computer and use it in GitHub Desktop.
Revisions
-
matveyt revised this gist
Mar 21, 2022 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -10,7 +10,7 @@ So my best attempt is: " ToggleComment({line1}, {line2}, {preserveindent}) function! ToggleComment(line1, line2, pi) abort let l:lnum = nextnonblank(a:line1) let l:end = prevnonblank(a:line2) if l:lnum >= 1 && l:lnum <= l:end let l:pat = printf('^\(\s*\)\(%s\)$', printf(escape(&cms, '^$.*~[]\'), '\(.*\)')) let l:sub = '\=empty(submatch(2)) ? submatch(0) : submatch(1)..submatch(3)' -
matveyt revised this gist
Oct 6, 2021 . 1 changed file with 3 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -18,12 +18,13 @@ function! ToggleComment(line1, line2, pi) abort let l:pat = a:pi ? '^\s*\zs.*' : '.*' let l:sub = printf(escape(&cms, '&\'), '&') endif call setline(l:lnum, map(getline(l:lnum, l:end), {_, v -> empty(v) ? v : \ substitute(v, l:pat, l:sub, '')})) endif endfunction " :[range]Comment[!] command! -range -bar -bang Comment call ToggleComment(<line1>, <line2>, <bang>0) ``` Pretty compact, isn't it? -
matveyt revised this gist
Oct 6, 2021 . 1 changed file with 1 addition and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -18,8 +18,7 @@ function! ToggleComment(line1, line2, pi) abort let l:pat = a:pi ? '^\s*\zs.*' : '.*' let l:sub = printf(escape(&cms, '&\'), '&') endif call setline(l:lnum, map(getline(l:lnum, l:end), {_, v -> empty(v) ? v : substitute(v, l:pat, l:sub, '')})) endif endfunction -
matveyt revised this gist
Oct 6, 2021 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -2,7 +2,7 @@ This is a pretty common question by beginners: how do I comment out some stuff? But then the novice keeps asking more and more: How do I uncomment? How do I comment with different filetypes? How do I... And then we come again to the usual "go get and install another 10000+ lines plugin named xxx or yyy". But is it really worth one? So let's put a problem: (1) write a (very!) small command/function able both to comment and uncomment arbitrary line range; (2) an action to be taken depends on the first non-blank line; (3) comment format follows `:h 'commentstring'` option to allow for easy customization; (4) blank lines are not to be changed; (5) there are two comment styles supported - with indentation (`....//foo`) and without one (`//....foo`). So my best attempt is: -
matveyt revised this gist
Oct 6, 2021 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -2,7 +2,7 @@ This is a pretty common question by beginners: how do I comment out some stuff? But then the novice keeps asking more and more: How do I uncomment? How do I comment with different filetypes? How do I... And then we come again to the usual "go get and install another 10000+ lines plugin named xxx or yyy". But is it really worth one? So let's put a problem: (1) write a (very!) small command/function able both to comment and uncomment arbitrary line range; (2) an action to be taken depends on the first non-blank line; (3) comment format follows `:h 'commentstring'` option to allow for easy customization; (4) blank lines are not to be changed; (5) there are two comment styles supported - with indentation (`....// foo;`) and without one (`//....foo;`). So my best attempt is: -
matveyt revised this gist
Oct 6, 2021 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -2,7 +2,7 @@ This is a pretty common question by beginners: how do I comment out some stuff? But then the novice keeps asking more and more: How do I uncomment? How do I comment with different filetypes? How do I... And then we come again to the usual "go get and install another 10000+ lines plugin named xxx or yyy". But is it really worth one? So let's put a problem: (1) write a (very!) small command/function able both to comment and uncomment arbitrary line range; (2) an action to be taken depends on the first non-blank line; (3) comment format follows `:h 'commentstring'` option to allow for easy customization; (4) blank lines are not to be changed; (5) there are two comment styles supported - with indentation (` // foo;`) and without one (`// foo;`). So my best attempt is: -
matveyt renamed this gist
Oct 6, 2021 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
matveyt created this gist
Oct 6, 2021 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,30 @@ This is a pretty common question by beginners: how do I comment out some stuff? And a pretty common answer is to use Visual-Block mode or substitute command. There's even [a wiki page](https://vim.fandom.com/wiki/Commenting_out_a_range_of_lines) to explain this in detail. But then the novice keeps asking more and more: How do I uncomment? How do I comment with different filetypes? How do I... And then we come again to the usual "go get and install another 10000+ lines plugin named xxx or yyy". But is it really worth one? So let's put a problem: (1) write a (very!) small command/function able both to comment and uncomment arbitrary line range; (2) an action to be taken depends on the first non-blank line; (3) comment format follows `:h 'commentstring'` option to allow for easy customization; (4) blank lines are not to be changed; (5) there are two comment styles supported - with indentation (` // foo;`) and without one (`// foo;`). So my best attempt is: ```VimScript " ToggleComment({line1}, {line2}, {preserveindent}) function! ToggleComment(line1, line2, pi) abort let l:lnum = nextnonblank(a:line1) let l:end = type(a:line2) == v:t_number ? a:line2 : line(a:line2) if l:lnum >= 1 && l:lnum <= l:end let l:pat = printf('^\(\s*\)\(%s\)$', printf(escape(&cms, '^$.*~[]\'), '\(.*\)')) let l:sub = '\=empty(submatch(2)) ? submatch(0) : submatch(1)..submatch(3)' if getline(l:lnum) !~# l:pat let l:pat = a:pi ? '^\s*\zs.*' : '.*' let l:sub = printf(escape(&cms, '&\'), '&') endif call setline(l:lnum, map(getline(l:lnum, l:end), {_, v -> empty(v) ? v : \ substitute(v, l:pat, l:sub, '')})) endif endfunction " :[range]Comment[!] command! -range -bar -bang Comment call ToggleComment(<line1>, <line2>, <bang>0) ``` Pretty compact, isnt'it?