Display current function in vim status line
Rather than having the name of the current method/class displayed in your status line, you could simply… jump to the declaration and jump back.
In Python:
?def<Esc>
or the built-in:
[[<C-o>
In JavaScript:
?fun<Esc>
It doesn't need configuration… it doesn't depend on third party tools… it's language-agnostic… it's lightweight…
You should try the Tagbar plugin, which is ctags based. If you check the screenshots on that link you will notice the status lines shows the name of the current method, exactly as you asked.
The plugin documentation explain how you could set your status line; the following is the configuration I'm using on my 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
As sometimes I work with very large source files, and swapping between large files causes a small delay due to ctags execution, I prefer to enable and disable this functionality by using the mapping (Ctrl+F12) or command defined above.
lightline.vim
and tagbar
based on 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