0

I have some questions regarding the use of VI while working on c or cpp programs, this can serve as a quick reference to people who are coming to vi for the first time.

  1. How to run CPP programs from withing vi, without exiting it.
  2. How to get type hierarchy in vi
  3. How to get call hierarchy in vi
  4. How to refactor code in vi, so that it will not only refactor on current file but also on all the other dependent files.
  5. How to open declaration of a method defined in some other cpp file from the place where it is used.
  6. How to search in different files for some keyword without exiting vi.
  7. How to maintain indentation of programs in vi.

Thanks in advance.

Ankit Zalani
  • 3,068
  • 5
  • 27
  • 47
  • Please ask separate questions for these, since they are quite unrelated. – Konrad Rudolph Jun 05 '14 at 18:58
  • `vi` is a text editor, not an `IDE` – PM 77-1 Jun 05 '14 at 19:00
  • These are all related. Please share your knowledge about whatever you feel is valued more. – Ankit Zalani Jun 05 '14 at 19:01
  • But vi is very powerful so, it might be able to suffice some of the needs. – Ankit Zalani Jun 05 '14 at 19:01
  • vi is powerful in its simplicity. It gives you the tools to do whatever you need to do on a file, but it's not an IDE. It has no inherit knowledge of languages or libraries, nor does it want to open the files to check. The idea is to keep the memory footprint, and processor intensity very low. All these features add overhead, which was impractical when vi was created. – Happington Jun 05 '14 at 19:09
  • Yes it can do a lot, although not as much as an IDE. I disagree with @Happington that vi is only about simplicity. Check out vundle: https://github.com/gmarik/Vundle.vim It is like a package manager for VIM and vim wiki: http://vim.wikia.com/wiki/Vim_Tips_Wiki My vim is pretty much set up for my own – SwiftMango Jun 05 '14 at 19:42
  • The IDE is your shell prompt. `vi` is its editor, `gdb` its debugger, etc. – RBerteig Jun 05 '14 at 19:45

3 Answers3

1

disclaimer -- I am not a vi(m) user, rather I use emacs.

I suspect that much of what you want is not available in a default installation of vi(m), however some of the functionality can be gained via additional programs:

for your question 3 (and possibly 5 & 6) cscope may provide what you are looking for. See this link

for your question 4, see this stack-overflow thread.

for your questions 5 (and possibly 6), ctags may provide what you are looking for. See this link.

I ran in to several of the same problems using emacs, and wound up integrating similar third-party programs into emacs, thus my somewhat limited knowledge on this topic. Hope these pointers help.

Community
  • 1
  • 1
thurizas
  • 2,473
  • 1
  • 14
  • 15
0

I got some of the answer to question:

1 How to run CPP programs from withing vi, without exiting it.

Solution: Using bang (!) followed by the command to be run. For instance, if you're editing a file in Vim and want to find out how many words are in the file, run

    :! wc %

We can also exit and go to shell by typing :shell or :sh while remaining within vi, the shell will open you can type in any no. of commands and when you are done you can just exit the shell and you will land into vi again at the same place.

6 How to search in different files for some keyword without exiting vi.

Solution: Using the same explained in answer 1. Using grep:

 :! grep <keyword or regular expression>
Ankit Zalani
  • 3,068
  • 5
  • 27
  • 47
0

All of the questions lumped together expose a point of view issue. From the point of view of the folks (starting with Bill Joy, the original author of vi) who created Unix and vi, the the shell prompt is the whole development environment. vi is just the editor part. make is the project builder. The file system along with ls, find, grep and a myriad other tools provides the file grouping and organizational tools.

This is why vi has a rich set of command line options that allow you to name multiple files to open, and where in each file to position the cursor. Along with the ctags tool, you can even ask vi to open the file holding a named function without typing the name of the file.

From within vi, you can always use the ! command to operate on text using an arbitrary program. !fmt is a quick and dirty way to do wrapped text, for instance. And, of course, the : command is your gateway into the wonderfully dangerous power of CLI based editing.

Underlying it all is a requirement that you really understand the commands that move the cursor and describe a span of text. The first step is to notice every time you find yourself repeating a command (like a simple movement command 'h', 'j', 'k' or 'l') until you reach the right spot and ask your self how you could have achieved that effect with less typing. Instead of 'lllll', say '5l' or perhaps 'w' or better 'fa' if the target was the next letter 'a' that just happened to be five characters over.

With the right mindset, and a good grasp of the larger universe of text processing filters, most of your questions boil down to identifying the right span of text and passing it to an external utility, in the context of a project already sensibly organized in folders and with ctags run from make to keep the tags database current.

There are lots of tutorials and quick references out there. This one seems to express the point of view I'm trying to convey here.

RBerteig
  • 41,948
  • 7
  • 88
  • 128