Skip to content

Instantly share code, notes, and snippets.

@PeterRincker
PeterRincker / SortGroup.vim
Last active December 26, 2024 12:17
Sort groups of lines in vim
" :[range]SortGroup[!] [n|f|o|b|x] /{pattern}/
" e.g. :SortGroup /^header/
" e.g. :SortGroup n /^header/
" See :h :sort for details
function! s:sort_by_header(bang, pat) range
let pat = a:pat
let opts = ""
if pat =~ '^\s*[nfxbo]\s'
let opts = matchstr(pat, '^\s*\zs[nfxbo]')
@PeterRincker
PeterRincker / diff.md
Last active September 5, 2024 04:12 — forked from romainl/diff.md
:DiffOrig but smarter

:DiffOrig but smarter

This is an enhanced version of the snippet provided under :help diff-original-file.

Where the original :DiffOrig only shows differences between the buffer in memory and the file on disk, :Diff can be used in two ways:

  • against the file on disk, like the original, with:

    :Diff
    
@PeterRincker
PeterRincker / quick-replace.vim
Last active April 18, 2022 20:20 — forked from orlp/gist:8c25ed4abb36372bc6fe
Quick Replacements - gn with an n afterwards
" Quick Replace
" Makes *``cgn like workflows faster by automatically moving to next match. Repeat with `.`
"
" Requires repeat.vim
"
" Example mappings:
" nmap cm <Plug>(quick-replace)
" xmap C <Plug>(quick-replace)
"
" mnemonic: cm for change matches
" Build helptags for all packages even optional ones
command! -nargs=0 -bar Helptags for p in glob('~/.vim/pack/*/opt/*', 1, 1) | exe 'packadd '.fnamemodify(p, ':t') | endfor | silent! helptags ALL
" create parent directories
" http://stackoverflow.com/questions/4292733/vim-creating-parent-directories-on-save
function! s:MkNonExDir(file, buf)
if empty(getbufvar(a:buf, '&buftype')) && a:file!~#'\v^\w+\:\/'
let dir=fnamemodify(a:file, ':h')
if !isdirectory(dir)
call mkdir(dir, 'p')
endif
endif
endfunction
" Symbol text objects
for char in [ '_', '.', ':', ';', '<bar>', '/', '<bslash>', '*', '+', '%' ]
execute 'xnoremap i' . char . ' :<C-u>normal! T' . char . 'vt' . char . '<CR>'
execute 'onoremap i' . char . ' :normal vi' . char . '<CR>'
execute 'xnoremap a' . char . ' :<C-u>normal! F' . char . 'vf' . char . '<CR>'
execute 'onoremap a' . char . ' :normal va' . char . '<CR>'
endfor
" keep star from jumping
nnoremap <script> <silent> <expr> * <SID>JustHighlight('*')
function! s:JustHighlight(cmd)
" still messes up ' mark
let s:JustHighlight_view = winsaveview()
augroup just_hightlight
autocmd!
autocmd CursorMoved <buffer>
\ execute "autocmd! just_hightlight" |
\ keepjumps call winrestview(s:JustHighlight_view)
@PeterRincker
PeterRincker / var_segment_textobj.vim
Last active May 19, 2021 17:40
Basic textobject support for variable segments
" Basic support for variable segment textobjects
" e.g. "foo" in foo_var and fooBar
xnoremap iv :<c-u>call <SID>segment()<cr>
onoremap iv :normal viv<cr>
xnoremap av :<c-u>call <SID>segment(1)<cr>
onoremap av :normal vav<cr>
function s:segment(...)
let start_pat = '\C\v%(<|_+\zs\w|[a-z0-9]\zs[A-Z])'
@PeterRincker
PeterRincker / comment.vim
Last active March 17, 2021 22:02
SimpleComment
" Inspired by Tim Pope's commentary.vim
"
" Uses b:commentstring or 'commentstring' as the comment pattern
" example:
" let &commentstring = '/*%s*/'
nnoremap gcc :<c-u>.,.+<c-r>=v:count<cr>call <SID>toggleComment()<cr>
nnoremap gc :<c-u>set opfunc=<SID>commentOp<cr>g@
xnoremap gc :call <SID>toggleComment()<cr>
@PeterRincker
PeterRincker / Y.vim
Last active December 24, 2020 15:27
Exposing your clipboard over SSH
" Add line to remote vimrc file
" :[range]Y - send [range] lines to remote-pbcopy via netcat.
command! -range Y silent <line1>,<line2>w !nc localhost 5556