Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 667417
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T23:55:35+00:00 2026-05-13T23:55:35+00:00

I am using vim 7.x I am using alternate file. I have a mapping

  • 0

I am using vim 7.x

I am using alternate file.

I have a mapping of *.hpp <–> *.cpp

Suppose I’m in

class Foo {

  void some_me#mber_func(); // # = my cursor

}

in Foo.hpp

is there a way to tell vim to do the following:

  1. Grab word under # (easy, expand(“”)
  2. Look up the class I’m inside of (“Foo”) <– I have no idea how to do this
  3. Append `1 & 2 (easy: using “.”) –> “Foo::some_member_func”
    4: Switch files (easy, :A)
  4. Do a / on 4

So basically, I can script all of this together, except the “find the name of the enclosing class I’m in part (especially if classes are nested).

I know about ctags. I know about cscope. I’m choosing to not use them — I prefer solutions where I understand where they break.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T23:55:35+00:00Added an answer on May 13, 2026 at 11:55 pm

    This is relatively easy to do crudely and very difficult to do well. C and C++ are rather complex languages to parse reliably. At the risk of being downvoted, I’d personally recommend parsing the tags file generated by ctags, but if you really want to do it in Vim, there are a few of options for the “crude” method.

    1. Make some assumptions. The assumptions you make depend on how complicated you want it to be. At the simplest level: assume you’re in a class definition and there are no other nearby braces. Based on your coding style, assume that the opening brace of the class definition is on the same line as “class”.

      let classlineRE = '^class\s\+\(\k\+\)\s\+{.*'
      let match = search(classlineRE, 'bnW')
      if match != 0
          let classline = getline(match)
          let classname = substitute(classline, classlineRE, '\1', '')
          " Now do something with classname
      endif
      

      The assumptions model can obviously be extended/generalised as much as you see fit. You can just search back for the brace and then search back for class and take what’s in between (to handle braces on a separate line to “class”). You can filter out comments. If you want to be really clever, you can start looking at what level of braces you’re in and make sure it’s a top level one (go to the start of the file, add 1 every time you see ‘{‘ and subtract one every time you see ‘}’ etc). Your vim script will get very very very complicated.

    2. Another one risking the downvote, you could use one of the various C parsers written in python and use the vim-python interface to make it act like a vim script. To be honest, if you’re thinking of doing this, I’d stick with ctags/cscope.

    3. Use rainbow.vim. This does highlighting based on depth of indentation, so you could be a little clever and search back (using search('{', 'bW') or similar) for opening braces, then interrogate the syntax highlighting of those braces (using synIDattr(synID(line("."), col("."),1), "name")) and if it’s hlLevel0, you know it’s a top-level brace. You can then search back for class and parse as per item 1.

    I hope that all of the above gives you some food for thought…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Am using vim and have a large text file that contains some html thrown
I have just started using Vim in a terminal (PuTTY or MinTTY), after always
I am using Vim to edit a Java file, but I find the way
Using vim, I would like to effectively have expandtabs off if I'm to the
Using VIM, it is easy to display line numbers for any given file with:
Using vim with the Rails plugin, can you create a migration file and then
Using Vim 6.0. Say I'm editing this file: sdfsdg dfgdfg 34 12 2 4
I'm using Vim for editing source code, but I would also like to have
I'm using vim to do some pattern matching on a text file. I've enabled
I am using vim 7.3 and the NERDTree 4.1 plugin. I have been trying

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.