I spend 20% of my life writing code in vim, almost exclusively javascript and python. The other 80% of the time I am mostly scrolling up and down my source file, trying to remember which function I'm currently editing and what class the function belongs to.
This may be technically impossible for reasons I don't understand, but are there any vim plugins which allow the vim status line to show the function the cursor is currently in for Python and/or Javascript?
It would look like this:
It's possible this already exists in, say, SublimeText. If so, I might finally stop crying and make the switch.
Some Vim plugins which don't offer this functionality:
Since writing this question I've found ctags which does the same thing for C knows this kind of information. But how do I get it to display in the 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…
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
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.
Metadata about where each function resides in a particular file is gathered and stored by a command-line tool called ctags.
The tagbar plugin for Vim manages ctags
calls in order to show a hierarchy of the document currently being edited.
Finally, the airline plugin comes with an extension for tagbar
which allows the current tag (i.e. the name of the current function) to be displayed in the Vim status line.
This can be configured to show the whole hierarchy of the tag by adding this line to your .vimrc
:
let g:airline#extensions#tagbar#flags = 'f'
which looks like this:
Inspiration for this answer comes from this answer and a comment on this question.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With