在 vim 状态行中显示当前函数
Display current function in vim status line
我一生中 20% 的时间都花在 vim 上写代码,几乎完全是 javascript 和 python。
其他 80% 的时间我主要上下滚动我的源文件,试图记住我当前正在编辑哪个函数以及该函数属于什么class。
由于我不明白的原因,这在技术上可能是不可能的,但是是否有任何 vim 插件允许 vim 状态行显示光标当前所在的功能 Python and/or Javascript?
看起来像这样:
这可能已经存在于 SublimeText 中。如果是这样,我可能终于停止哭泣并做出改变。
一些 Vim 插件 不 提供此功能:
更新
自从写了这个问题我发现 ctags which does the same thing for C 知道这种信息。但是如何让它显示在 Vim 状态行中?
您应该尝试 Tagbar plugin,它是基于 ctags 的。如果您查看 link 上的屏幕截图,您会注意到状态行显示了当前方法的名称,与您询问的完全一样。
插件文档解释了如何设置状态行;以下是我在我的 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
由于有时我使用非常大的源文件,并且在大文件之间交换会由于 ctags 的执行而导致小的延迟,我更喜欢使用映射 (Ctrl+F12) 或上面定义的命令。
与其在状态行中显示当前 method/class 的名称,您还可以简单地...跳转到声明并返回。
在Python中:
?def<Esc>
或内置:
[[<C-o>
在JavaScript中:
?fun<Esc>
它不需要配置……它不依赖于第三方工具……它与语言无关……它是轻量级的……
有关每个函数在特定文件中的位置的元数据由名为 ctags 的命令行工具收集和存储。
Vim 的 tagbar 插件管理 ctags
调用以显示当前正在编辑的文档的层次结构。
最后,airline 插件附带了 tagbar
的扩展,它允许当前标签(即当前函数的名称)显示在 Vim 状态行中.
通过将此行添加到您的 .vimrc
:
,可以将其配置为显示标签的整个层次结构
let g:airline#extensions#tagbar#flags = 'f'
看起来像这样:
这个答案的灵感来自 and a comment on this question。
lightline.vim
and 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
只需尝试 [[
跳转到函数的第一个大括号,在那里您可以看到正在编辑的函数的名称;然后键入 CTRL-O
到 return。 :)
在拆分 window/header 行
中显示 上下文
有 vim/neovim 插件可以自动显示光标的上下文 w.r.t。你所在的 function/method/case。
vim 插件
neovim treesitter 插件
我一生中 20% 的时间都花在 vim 上写代码,几乎完全是 javascript 和 python。 其他 80% 的时间我主要上下滚动我的源文件,试图记住我当前正在编辑哪个函数以及该函数属于什么class。
由于我不明白的原因,这在技术上可能是不可能的,但是是否有任何 vim 插件允许 vim 状态行显示光标当前所在的功能 Python and/or Javascript?
看起来像这样:
这可能已经存在于 SublimeText 中。如果是这样,我可能终于停止哭泣并做出改变。
一些 Vim 插件 不 提供此功能:
更新
自从写了这个问题我发现 ctags which does the same thing for C 知道这种信息。但是如何让它显示在 Vim 状态行中?
您应该尝试 Tagbar plugin,它是基于 ctags 的。如果您查看 link 上的屏幕截图,您会注意到状态行显示了当前方法的名称,与您询问的完全一样。
插件文档解释了如何设置状态行;以下是我在我的 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
由于有时我使用非常大的源文件,并且在大文件之间交换会由于 ctags 的执行而导致小的延迟,我更喜欢使用映射 (Ctrl+F12) 或上面定义的命令。
与其在状态行中显示当前 method/class 的名称,您还可以简单地...跳转到声明并返回。
在Python中:
?def<Esc>
或内置:
[[<C-o>
在JavaScript中:
?fun<Esc>
它不需要配置……它不依赖于第三方工具……它与语言无关……它是轻量级的……
有关每个函数在特定文件中的位置的元数据由名为 ctags 的命令行工具收集和存储。
Vim 的 tagbar 插件管理 ctags
调用以显示当前正在编辑的文档的层次结构。
最后,airline 插件附带了 tagbar
的扩展,它允许当前标签(即当前函数的名称)显示在 Vim 状态行中.
通过将此行添加到您的 .vimrc
:
let g:airline#extensions#tagbar#flags = 'f'
看起来像这样:
这个答案的灵感来自
lightline.vim
and 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
只需尝试 [[
跳转到函数的第一个大括号,在那里您可以看到正在编辑的函数的名称;然后键入 CTRL-O
到 return。 :)
在拆分 window/header 行
中显示 上下文有 vim/neovim 插件可以自动显示光标的上下文 w.r.t。你所在的 function/method/case。
vim 插件
neovim treesitter 插件