vim函数定义跳转相关设置

修改下vim的一些ctags相关快捷键,个人用着顺手点。

小结如下:

  • normal模式下的gk,用来打开一个预览窗口预览函数定义(需要ctags生成好tags文件)。
  • normal模式下的gd,修改映射为ctrl+],即跳转到函数定义(vim内置的ctags的跳转)。
  • normal模式下的F5,用来替换优化下原来的gd(我的个人用法是优化),作用是高亮当前单词,并加入vim的搜索mode中,这样按下n可以跳到下一个位置,N可以上一个位置。

以下是具体内容,加入到 ~/.vimrc中即可。

lua 复制代码
" preview defination in a preview window
" nnoremap gk :ptag expand("<cword>"), This can not be used...
func PreviewSymbol()
    let l:word_under_cursor = expand("<cword>")
    echo "find def for symbol " . l:word_under_cursor
    " :ptag (l:word_under_cursor)
    execute "ptag " . l:word_under_cursor
endf
nnoremap gk :call PreviewSymbol()<CR>




let g:current_word_match_id = -1
function! ToggleHighlightCurrentWord()
  " if has hightword already, clear it
  if g:current_word_match_id != -1
    silent! call matchdelete(g:current_word_match_id)
    let g:current_word_match_id = -1
    return
  endif

  let l:current_word = expand('<cword>')

  " if empty, do nothing
  if empty(l:current_word)
    return
  endif

  " Use the same color with Search highlight group
  highlight link CurrentWord Search

  " set highlight and store the highlighted word to current_word_match_id 
  let g:current_word_match_id = matchadd('CurrentWord', '\<' . escape(l:current_word, '\') . '\>')

  " set cur highlighted word as vim's search pattern
  execute 'let @/ = "\\<' . escape(l:current_word, '\') . '\\>"'
endfunction

" Bind this function to <F5>
nnoremap <F5> :call ToggleHighlightCurrentWord()<CR>

nnoremap gd <c-]>
nnoremap gt <c-]>
相关推荐
johnny2332 小时前
编辑器和笔记软件汇总:Typst、Reminds、Memos、Editor、MDX Notes、Jotty
笔记·编辑器
宫瑾3 小时前
vscode未定义标识符报错的解决方法
ide·vscode·编辑器
宫瑾3 小时前
C语言开发时,自定义vscode语法颜色
ide·vscode·编辑器
yj爆裂鼓手4 小时前
unity编辑器下ab包模式下textMeshPro文本不显示材质是紫色的异常,真机无异常的问题
unity·编辑器·材质
Cxiaomu4 小时前
Python 文件解析: Excel / Word / PDF 的解析、处理、预览与下载
python·word·excel
张3蜂4 小时前
Visual Studio Code 详细解析与竞品分析(2026版)
ide·vscode·编辑器
Eternity∞4 小时前
数据结构基础
c语言·开发语言·数据结构·学习·vim
2501_930707785 小时前
如何使用C#代码从 PDF 中提取表格并另存为Excel文件
pdf·excel
pacong5 小时前
B生所学EXCEL
人工智能·excel
城数派5 小时前
2014-2025年全国监测站点的逐年空气质量数据(15个指标\Excel\Shp格式)
数据分析·excel