我一生中 20% 的时间都在用 vim 编写代码,几乎完全是 javascript 和 python。另外 80% 的时间我主要是上下滚动我的源文件,试图记住我当前正在编辑哪个函数以及该函数属于哪个类。
由于我不明白的原因,这在技术上可能是不可能的,但是是否有任何 vim 插件允许 vim 状态行显示光标当前在 Python 和/或 Javascript 中所处的功能?
它看起来像这样:
这可能已经存在于 SublimeText 中。如果是这样,我可能最终会停止哭泣并进行转换。
一些不提供此功能的Vim 插件:
由于写这个问题我已经找到的ctags这不会对C同样的事情,知道这方面的信息。但是如何让它显示在 Vim 状态行中呢?
与其在状态行中显示当前方法/类的名称,不如简单地……跳转到声明并跳回。
在 Python 中:
?def<Esc>
Run Code Online (Sandbox Code Playgroud)
或内置:
[[<C-o>
Run Code Online (Sandbox Code Playgroud)
在 JavaScript 中:
?fun<Esc>
Run Code Online (Sandbox Code Playgroud)
它不需要配置……它不依赖于第三方工具……它与语言无关……它是轻量级的……
lightline.vim 和 tagbar基于https://github.com/itchyny/lightline.vim/issues/42:
vimrc
let g:lightline = {
\ 'active': {
\ 'left': [ [ 'mode', 'paste' ], [ 'filename', ], [ 'tagbar' ] ]
\ },
\ 'component': {
\ 'tagbar': '%{tagbar#currenttag("%s", "", "f")}',
\ },
\ 'component_function': {
\ 'modified': 'LightLineModified',
\ 'readonly': 'LightLineReadonly',
\ 'filename': 'LightLineFilename',
\ 'fileformat': 'LightLineFileformat',
\ 'filetype': 'LightLineFiletype',
\ 'fileencoding': 'LightLineFileencoding',
\ 'mode': 'LightLineMode'}
\ }
function! LightLineModified()
return &ft =~ 'help' ? '' : &modified ? '+' : &modifiable ? '' : '-'
endfunction
function! LightLineReadonly()
return &ft !~? 'help' && &readonly ? 'RO' : ''
endfunction
function! LightLineFilename()
let fname = expand('%:t')
return fname == '__Tagbar__' ? g:lightline.fname :
\ ('' != LightLineReadonly() ? LightLineReadonly() . ' ' : '') .
\ ('' != fname ? fname : '[No Name]') .
\ ('' != LightLineModified() ? ' ' . LightLineModified() : '')
endfunction
function! LightLineFileformat()
return winwidth(0) > 70 ? &fileformat : ''
endfunction
function! LightLineFiletype()
return winwidth(0) > 70 ? (strlen(&filetype) ? &filetype : 'no ft') : ''
endfunction
function! LightLineFileencoding()
return winwidth(0) > 70 ? (strlen(&fenc) ? &fenc : &enc) : ''
endfunction
function! LightLineMode()
let fname = expand('%:t')
return fname == '__Tagbar__' ? 'Tagbar' :
\ winwidth(0) > 60 ? lightline#mode() : ''
endfunction
let g:tagbar_status_func = 'TagbarStatusFunc'
function! TagbarStatusFunc(current, sort, fname, ...) abort
let g:lightline.fname = a:fname
return lightline#statusline(0)
endfunction
Run Code Online (Sandbox Code Playgroud)
您应该尝试基于 ctags的Tagbar 插件。如果您检查该链接上的屏幕截图,您会注意到状态行显示了当前方法的名称,正如您所要求的那样。
插件文档解释了如何设置状态行;以下是我在 vimrc 上使用的配置:
command! -nargs=0 TagbarToggleStatusline call TagbarToggleStatusline()
nnoremap <silent> <c-F12> :TagbarToggleStatusline<CR>
function! TagbarToggleStatusline()
let tStatusline = '%{exists(''*tagbar#currenttag'')?
\tagbar#currenttag('' [%s] '',''''):''''}'
if stridx(&statusline, tStatusline) != -1
let &statusline = substitute(&statusline, '\V'.tStatusline, '', '')
else
let &statusline = substitute(&statusline, '\ze%=%-', tStatusline, '')
endif
endfunction
Run Code Online (Sandbox Code Playgroud)
由于有时我处理非常大的源文件,并且由于 ctags 执行,大文件之间的交换会导致很小的延迟,因此我更喜欢使用上面定义的映射 ( Ctrl+ F12) 或命令来启用和禁用此功能。