Created
March 18, 2025 09:32
-
-
Save Konfekt/0007d3d507e4dc5f7a9e037b0b0b83c7 to your computer and use it in GitHub Desktop.
mappings to navigate and preview keywords, tags and (macro) more conveniently
This file contains 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 characters
" :tjump opens a menu if multiple tags match whereas :tag does not | |
nnoremap <c-]> g<C-]> | |
xnoremap <c-]> g<C-]> | |
" open tag in preview window | |
nnoremap g] <c-w>g}<c-w>Pzv<c-w>p | |
xnoremap g] <c-w>g}<c-w>Pzv<c-w>p | |
" (forcefully) close preview window and jump back | |
nnoremap <silent><expr> g[ ':<c-u>silent! pclose<cr>:silent! '..v:count1..'pop<cr>' | |
" with set foldopen+=jumps the <c-w>Pzv<c-w>p to open the fold is not needed | |
nnoremap [i :<c-u>psearch \C<c-r><c-w><CR><c-w>Pzv<c-w>p | |
nnoremap <expr> ]i line('.') == line('$') ? ':<c-u>echoerr "E389: Couldn''t find pattern"<cr>' : ':<c-u>+,$psearch \C<c-r><c-w><CR><c-w>Pzv<c-w>p' | |
" jump to selected include / list selected includes | |
nnoremap <expr> [I ':<c-u>Ilist \V' . escape(expand('<cword>'), '\') . '<cr>' | |
nnoremap <expr> ]I line('.') == line('$') ? ':<c-u>echoerr "E389: Couldn''t find pattern"<cr>' : ':<c-u>+,$Ilist \V'. escape(expand('<cword>'), '\') | |
" jump to selected makro / list selected makros | |
nnoremap [D :<c-u>Dlist <C-R><C-W><CR> | |
nnoremap <expr> ]D line('.') == line('$') ? ':<c-u>echoerr "E389: Couldn''t find pattern"<cr>' : ':<c-u>+,$Dlist <C-R><C-W><CR>' | |
command! -range=% -bang -nargs=? -complete=tag -bar Dlist call SearchList('d', <q-line1>, <q-line2>, <q-bang>, <q-args>) | |
command! -range=% -bang -nargs=? -complete=tag -bar Ilist call SearchList('i', <q-line1>, <q-line2>, <q-bang>, <q-args>) | |
" Show ]I and [I results in the quickfix window. | |
" From http://www.reddit.com/r/vim/comments/1rzvsm/do_any_of_you_redirect_results_of_i_to_the/ | |
function! SearchList(type, line1, line2, bang, search_pattern) abort | |
let type = a:type | |
let l:ouput = '' | |
redir => l:output | |
let search_pattern = | |
\ (a:search_pattern =~# '\v^/?$' ? '/' . @/ : a:search_pattern) | |
silent! execute a:line1 . ',' . a:line2 . type . 'list' . a:bang . ' ' . search_pattern | |
redir END | |
let lines = split(l:output, '\n') | |
" Bail out on errors. | |
if (len(lines) == 3) && ((type is# 'd' && lines[2] =~# '^E388:') || type is# 'i' && (lines[2] =~# '^E389:')) | |
echomsg 'Could not find "' . search_pattern . '" . ' | |
return | |
endif | |
" Our results may span multiple files so we need to build a relatively complex list based on filenames. | |
let filename = "" | |
let qf_entries = [] | |
for line in lines | |
if line !~ '^\s*\d\+:' | |
let filename = fnamemodify(line, ':p:.') | |
else | |
let lnum = split(line)[1] | |
let text = substitute(line, '^\s*.\{-}:\s*\S\{-}\s', "", "") | |
let col = match(text, a:search_pattern) + 1 | |
call add(qf_entries, {"filename" : filename, "lnum" : lnum, "col" : col, "vcol" : 1, "text" : text}) | |
endif | |
endfor | |
call setloclist(0, qf_entries) | |
lwindow | |
endfunction |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment