Skip to content

Commit 0715953

Browse files
committed
vim: ToggleComment now comments at the beginning of line
1 parent 1a21b9a commit 0715953

File tree

2 files changed

+96
-28
lines changed

2 files changed

+96
-28
lines changed

nvim/init.vim

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
" nvim file: ~/.config/nvim/init.vim
2-
" Last Change: 2018 mar 22 18:47
2+
" Last Change: 2018 mar 23 14:18
33
" vim: ff=unix ai et ts=4
44
" Author: Sérgio Luiz Araújo Silva
55
" Reference: http://sergioaraujo.pbworks.com/w/page/15864094/vimrc
@@ -326,10 +326,38 @@ xmap S <Plug>VSurround
326326
xmap gS <Plug>VgSurround
327327
328328
nnoremap <F2> :NERDTreeToggle<cr>
329-
nnoremap <Leader>t :Commentary<cr>
330-
vnoremap <Leader>t :Commentary<cr>
329+
" nnoremap <Leader>t :Commentary<cr>
330+
" vnoremap <Leader>t :Commentary<cr>
331331
" comment a paragraph → gcap
332332

333+
" these lines are needed for ToggleComment()
334+
autocmd FileType c,cpp,java let b:comment_leader = '\/\/'
335+
autocmd FileType arduino let b:comment_leader = '\/\/'
336+
autocmd FileType sh,ruby,python let b:comment_leader = '#'
337+
autocmd FileType zsh let b:comment_leader = '#'
338+
autocmd FileType conf,fstab let b:comment_leader = '#'
339+
autocmd FileType matlab,tex let b:comment_leader = '%'
340+
autocmd FileType vim let b:comment_leader = '"'
341+
342+
function! ToggleComment()
343+
if exists('b:comment_leader')
344+
let l:pos = col('.')
345+
if getline('.') =~ '\v(\s*|\t*)' .b:comment_leader
346+
execute 'silent s/\v^(\s*|\t*)\zs' .b:comment_leader.'[ ]?//g'
347+
let l:pos -= 2
348+
else
349+
exec 'normal! 0i' .b:comment_leader .' '
350+
let l:pos += 2
351+
endif
352+
call cursor(line("."), l:pos)
353+
else
354+
echo 'no comment leader found for filetype'
355+
end
356+
endfunction
357+
nnoremap <Leader>t :call ToggleComment()<CR>
358+
inoremap <Leader>t <C-o>:call ToggleComment()<CR>
359+
xnoremap <Leader>t :'<,'>call ToggleComment()<CR>
360+
333361
" source: https://github.com/junegunn/vim-plug/issues/164
334362
"command! Gstatus call LazyLoadFugitive('Gstatus')
335363
"command! Gdiff call LazyLoadFugitive('Gdiff')

vim/wiki/dicasvim.md

Lines changed: 65 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# dicasvim.md Intro - Last Change: 2018 mar 23 06:49
1+
# dicasvim.md Intro - Last Change: 2018 mar 23 12:45
22
vim: set ts=4 et:
33

44
+ http://yannesposito.com/Scratch/en/blog/Learn-Vim-Progressively/#navigation
@@ -143,6 +143,7 @@ To copy the line 16 to the line bellow just type:
143143
:/user-friend/m$ ......... move next "user-friend" to the final line
144144
:g/TITLE/ m+1 ............ moves down lines with TITLE
145145
:m+ ...................... move current line to line below
146+
:g/pattern/t<line number>
146147

147148
This is one anti pattern
148149

@@ -1670,35 +1671,70 @@ iab fname <c-r>=expand("%:p")<cr>
16701671
:h g@
16711672

16721673
```vimL
1674+
autocmd FileType c,cpp,java let b:comment_leader = '\/\/'
1675+
autocmd FileType arduino let b:comment_leader = '\/\/'
1676+
autocmd FileType sh,ruby,python let b:comment_leader = '#'
1677+
autocmd FileType zsh let b:comment_leader = '#'
1678+
autocmd FileType conf,fstab let b:comment_leader = '#'
1679+
autocmd FileType matlab,tex let b:comment_leader = '%'
1680+
autocmd FileType vim let b:comment_leader = '"'
1681+
16731682
function! ToggleComment()
1674-
let l:win_view = winsaveview()
1675-
if getline('.')[0] == "#"
1676-
normal! 0"_x
1677-
else
1678-
normal! I#
1679-
endif
1680-
call winrestview(l:win_view)
1683+
if exists('b:comment_leader')
1684+
let l:pos = col('.')
1685+
if getline('.') =~ '\v(\s*|\t*)' .b:comment_leader
1686+
"exec 'normal! _"_2x'
1687+
execute 'silent s/\v^(\s*|\t*)\zs' .b:comment_leader.'[ ]?//g'
1688+
let l:pos -= 2
1689+
else
1690+
exec 'normal! I' .b:comment_leader .' '
1691+
let l:pos += 2
1692+
endif
1693+
call cursor(line("."), l:pos)
1694+
else
1695+
echo 'no comment leader found for filetype'
1696+
end
16811697
endfunction
16821698
nnoremap <Leader>t :call ToggleComment()<CR>
1683-
vnoremap <Leader>t <C-o>:call ToggleComment()<CR>
1699+
inoremap <Leader>t <C-o>:call ToggleComment()<CR>
1700+
xnoremap <Leader>t :'<,'>call ToggleComment()<CR>
1701+
16841702
1685-
" Another version
1686-
" Because sometimes comments are not at the beginning of line
1703+
" another one
1704+
autocmd FileType c,cpp,java let b:comment_leader = '\/\/'
1705+
autocmd FileType arduino let b:comment_leader = '\/\/'
1706+
autocmd FileType sh,ruby,python let b:comment_leader = '#'
1707+
autocmd FileType conf,fstab let b:comment_leader = '#'
1708+
autocmd FileType matlab,tex let b:comment_leader = '%'
1709+
autocmd FileType vim let b:comment_leader = '"'
16871710
16881711
function! ToggleComment()
1689-
let l:win_view = winsaveview()
1690-
exec 'normal! _'
1691-
let char = getline('.')[col('.')-1]
1692-
if char == "#"
1693-
normal! _"_x
1712+
" help with :h \v or pattern-atoms
1713+
if exists('b:comment_leader')
1714+
if getline('.') =~ '\v^\s*' .b:comment_leader
1715+
" uncomment the line
1716+
execute 'silent s/\v^\s*\zs' .b:comment_leader.'[ ]?//g'
16941717
else
1695-
normal! I#
1718+
" comment the line
1719+
execute 'silent s/\v^\s*\zs\ze(\S|\n)/' .b:comment_leader.' /g'
16961720
endif
1697-
call winrestview(l:win_view)
1721+
else
1722+
echo 'no comment leader found for filetype'
1723+
end
16981724
endfunction
1699-
nnoremap <Leader>t :call ToggleComment()<CR>
1700-
inoremap <Leader>t <C-o>:call ToggleComment()<CR>
1725+
1726+
nnoremap <leader>c :call ToggleComment()<cr>
17011727
```
1728+
1729+
```viml
1730+
nnoremap <expr> gcc getline('.') =~ '\v^(\s+)?#' ? '_"_x' : "gI#\<esc>w"
1731+
xnoremap <expr> gc ':norm! ' . (getline("'<") =~ '^#' ? '0"_x' : "gI#") . "\<cr>"
1732+
```
1733+
1734+
# Set cursor line and column (position)
1735+
1736+
:call cursor(1682,2)
1737+
17021738
# Print some lines to pdf
17031739
+ http://www.vimweekly.com/
17041740

@@ -2654,6 +2690,10 @@ Supose you have these lines:
26542690

26552691
read more at: `help sub-replace`
26562692

2693+
# get char under cursor
2694+
2695+
:echo matchstr(getline('.'), '\%'.col('.').'c.')
2696+
26572697
# Converting numbers to chars
26582698
The range of printable chars spams from 32 to 122
26592699

@@ -3422,12 +3462,12 @@ OBS: These commands above do exactly the same
34223462
+ https://stackoverflow.com/a/49446424/2571881
34233463

34243464
I need to modify the certain charactors between two patterns in each line.
3425-
Eample:: (File content saved as myfile.txt)
3465+
Eample: (File content saved as myfile.txt)
34263466

3427-
abc, def, 1, {jsdfsd kfgdsf lgfgd}, 2, pqr, stu
3428-
abc, def, 1, {jsdfsqwe k fdfsfl}, 2, pqr, stu
3429-
abc, def, 1, {asdasdj kgfdgdf ldsfsdf}, 2, pqr, stu
3430-
abc, def, 1, {jsds kfdsf fdsl}, 2, pqr, stu
3467+
abc, def, 1, {,jsdfsd,kfgdsf,lgfgd}, 2, pqr, stu
3468+
abc, def, 1, {,jsdfsqwe,k,fdfsfl}, 2, pqr, stu
3469+
abc, def, 1, {,asdasdj,kgfdgdf,ldsfsdf}, 2, pqr, stu
3470+
abc, def, 1, {,jsds,kfdsf,fdsl}, 2, pqr, stu
34313471

34323472
Remove commas inside { block
34333473

0 commit comments

Comments
 (0)