How can I make vim show the current class and method I'm editing

498

Solution 1

This will work if you install both the airline and tagbar plugins. These two plugins integrate automatically and you'll get the current function displayed in the status bar. If you want to have the full object hierarchy (e.g. class + method), you'll have to configure that in your .vimrc:

let g:airline#extensions#tagbar#flags = 'f'  " show full tag hierarchy

You might have to setup the tags file for tagbar to identify the current code position, if you are not using ctags, yet. If you don't know about that yet, here's a short intro to ctags.

Solution 2

This script puts the name of the current function in the title bar and/or the status line. It uses ctags to generate a list of tags for the current file and assumes that the tag preceding the cursor location is the name of the current function. Since it uses ctags, it can be customized to work with different languages, even structured text files. It works well in practice, but has some limitations. If nothing else, it might give you a starting point for hacking.

Solution 3

I would suggest using https://github.com/wellle/context.vim. It shows current context that you are in. Here is image from project

enter image description here

Solution 4

Having recently tried this, I believe the simplest solution is:

  1. Install Universal Ctags https://ctags.io/

  2. Install Tagbar https://github.com/preservim/tagbar

  3. call tagbar#currenttag() when setting your statusline, e.g. in .vimrc:

    :set statusline=%<%f\ %h%m%r%=%{tagbar#currenttag('%s\ ','','f')}%-.(%l,%c%V%)\ %P

Airline completely changes your statusbar and it's non-trivial to get

Note that:

  • Spaces must be slash-escaped in the strings you pass to currenttag()...and it's even different between running the command inside vim and putting it in your .vimrc?? Anyway, spaces can be weird, and they're probably something you want when outputting the function name.

  • It took me some digging but the default statusline is

    :set statusline=%<%f\ %h%m%r%=%-14.(%l,%c%V%)\ %P

Share:
498

Related videos on Youtube

prawdziwy-sok
Author by

prawdziwy-sok

Updated on September 18, 2022

Comments

  • prawdziwy-sok
    prawdziwy-sok over 1 year

    so I'm trying to style my application, and I've run into problems when using the Holo theme. I have no intrest in earlier versions of android, so I'don't want to use AppCompat. To do my error checking I have created an empty app with one blank activity. This is my styles.xml:

        <resources>
    
        <!-- Base application theme. -->
        <style name="AppTheme" parent="@android:style/Theme.Holo.Light.DarkActionBar">
            <!-- Customize your theme here. -->
            <item name="android:actionBarStyle">@style/AppTheme_ActionBarTheme</item>
        </style>
    
        <style name="AppTheme_ActionBarTheme" parent="@android:style/Widget.Holo.Light.ActionBar">
            <item name="android:background">@color/light_green</item>
            <item name="android:titleTextStyle">@style/AppTheme_ActionBarTheme_Text</item>
        </style>
    
        <style name="AppTheme_ActionBarTheme_Text" parent="AppTheme_ActionBarTheme">
            <item name="android:textColor">@color/light</item>
        </style>
    
    </resources>
    

    and this is my AndroidManifest.xml:

        <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.fokamb.chmielowskim.fokamb" >
        <uses-sdk android:targetSdkVersion="15"  android:minSdkVersion="14"/>
        <application
            android:allowBackup="true"
    
            android:label="@string/app_name"
    
            android:theme="@style/AppTheme"
    
            >
    
            <activity
                android:name=".MainActivity"
                android:label="@string/main_act_name" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
        </application>
    
    </manifest>
    

    I didn't change any other files, and I tried running this app on an emulator with API level 21 and my phone with api 19. On both, i get the message "Unfortunately, FokaMB has stopped" Even though, in the preview window in android studio the app looks the way I would've liked it to. So, where might be my mistake? If I use AppCompat app works fine, but im unable to change the text color of actionbar (even though the background changes). Any help would be greatly appreciated:)