7

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:

Vim status line with location

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:

Update

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?

Community
  • 1
  • 1
LondonRob
  • 73,083
  • 37
  • 144
  • 201
  • looks like your editor is older than you are... get a new editor with VIM key bindings. – dandavis Nov 13 '15 at 18:05
  • @dandavis ha ha! Yes, I'm open to suggestions... – LondonRob Nov 13 '15 at 18:07
  • (exuberant) ctags actually supports JavaScript and Python: http://ctags.sourceforge.net/languages.html. You may just have to enable the vim plugin explicitly for the other file types. – Felix Kling Nov 13 '15 at 18:07
  • @FelixKling yes! So maybe this question should collapse down into: how do you get ctags to show the current function in the status line? [This](http://vimtricks.blogspot.co.uk/2012/05/wanna-see-function-name-in-status-line.html) looks promising. – LondonRob Nov 13 '15 at 18:11
  • 4
    @FelixKling Going on a tangent: it would be great if people would stop recommending the unmaintained Exubernt `ctags`. Use [universal `ctags`](https://github.com/universal-ctags/ctags) instead. It's an actively maintained fork (and successor) of Exubernt `ctags`. – Sato Katsura Nov 13 '15 at 18:17
  • @LondonRob `ctags` plus the [tagbar](https://github.com/majutsushi/tagbar) plugin. – Sato Katsura Nov 13 '15 at 18:23
  • The 'vim-airline' plugin has a `tagbar` extension that (if both are properly configured) does exactly what you want (i.e. shows current function in the status line); although `tagbar` by itself may be enough. – VanLaser Nov 13 '15 at 18:32
  • @Sato: Good to know, thanks! I was just following the clues from the plugin ;) – Felix Kling Nov 13 '15 at 18:43

6 Answers6

8

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…

func

romainl
  • 186,200
  • 21
  • 280
  • 313
  • I didn't know about `?`, so thanks for that! Also, even if I *had* known about it, this is a neat usage of it. – LondonRob Nov 17 '15 at 13:55
7

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
Hotschke
  • 9,402
  • 6
  • 46
  • 53
5

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.

Flimm
  • 136,138
  • 45
  • 251
  • 267
mMontu
  • 8,983
  • 4
  • 38
  • 53
  • Love this. I'm too dumb to work out from the [Airline docs](https://github.com/bling/vim-airline#fine-tuned-configuration) how to add a section for the current class. Then this question will be completely answered! – LondonRob Nov 17 '15 at 13:47
2

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:

airline plus tagbar

Inspiration for this answer comes from this answer and a comment on this question.

Community
  • 1
  • 1
LondonRob
  • 73,083
  • 37
  • 144
  • 201
1

Just try [[ to jump to the function's first brace, where you can see the function's name you are editing; Then type CTRL-O to return. :)

guo-sj
  • 43
  • 7
  • This is actually a perfectly legimitate suggestion. Although it's already in [this answer](https://stackoverflow.com/a/33700601/2071807) buried in the middle – LondonRob May 20 '21 at 14:10
  • Oh, I didn't notice that, sorry~ By the way, this is my first answer in stack overflow, thank you for voting me :-) – guo-sj May 21 '21 at 03:44
0

Show context in split window/header line

There are vim/neovim plugins which show you automatically the context of your cursor w.r.t. the function/method/case you are in.

vim plugin

neovim treesitter plugin

Hotschke
  • 9,402
  • 6
  • 46
  • 53