I am making a c++ program with vi. It has only one file but it's getting kind of big. It would be nice if I could easily see all the functions I created and jump to any one of them without having to search for them. Can vi do this, or is there a similar program that can?
This seems like a dup of Jump to function definition in vim.
To sum up that answer, use ctags, and take a look at Vim and Ctags tips and tricks.
I use a vim plugin to do this :
http://www.vim.org/scripts/script.php?script_id=273
It summarizes classes, struct, function, with jump functionality.
Related
Different STL containers like vector, stack, set, queue, etc support different access methods on them.
If you are coding for example in Notepad++ or vim, you have to continuously refer to the documentation to see what all methods are available, atleast I have to.
Is there some good way of remembering which container supports which methods??
The names of the methods aren't different for the sake of being different. It helps in remembering which containers have which methods, to understand the meaning of the name. push_back for example is nonsensical in relation to sets. insert doesn't make any sense when talking about stacks (of course stacks don't have a front or a back either, so it doesn't support push_back, just push). For a vector, both have a well-defined meaning, so vector supports both insert and push_back.
Use them enough so that you remember the methods of each.
If your memory keeps failing you, try keeping a reference of them all up in another window. If you have more than one monitor, it's really handy to have stuff like this on a second monitor (for documentation of any kind).
Alternatively I highly recommend a real coding IDE with intellisense! Notepad++ is probably too simple for being productive in C++.
Use something that has built in intellisense such as Visual Studio on Windows or KDevelop on Linux.
There are also add-ons for vim and emacs for intellisense.
Even if you remember all the "methods", that is only one part of the story. To effectively use STL, you need to know algorithms as well. I would suggest reading about STL in a good book (Stroustrup, Josuttis, ...) to just remember what is available, and then return to the books or have reference site open when you need the exact syntax.
This may not be exactly what you're looking for, but Scott Meyers (of "Effective C++" fame) has compiled the following list of STL algorithms based on Nicolai Josuttis's book "The C++ Standard Library":
Josuttis’ Summary of STL Algorithms
Learn what they are, and the common methods, and then it should be fairly easy to remember which ones apply. The STL isn't perfectly consistent, but it's pretty good.
Admitting that it doesn't support remembering you can get some kind of intellisense running on vim. The advantage is that you can create tags from both own and external source code files. Anyhow STL needs a special treatment which is described here.
Download these vim-scripts OmniCppComplete and SuperTab.
Install OmniCppComplete:
Unzip the plugin to ~/.vim.
Install SuperTab:
Open the file in vim ($ vim supertab.vba).
Source the file (:so %).
Install ctags via your favourite package manager. Download and unpack this file and run ctags on it.
$ ctags -R --c++-kinds=+p --fields=+iaS --extra=+q --language-force=C++ tags_stl cpp_src
This will generate a ctags file named 'tags_stl' containing the STL-Tags. Copy it anywhere you like. Add the following lines which do not already exist to your ~/.vimrc:
set tags+=~/path/to/your/tags_stl
filetype on
filetype plugin on
let OmniCpp_GlobalScopeSearch=1
let OmniCpp_NamespaceSearch=2
let OmniCpp_MayCompleteDot=1
let OmniCpp_MayCompleteArrow=1
let OmniCpp_MayCompleteScope=1
let OmniCpp_DisplayMode=1
let OmniCpp_DefaultNamespaces=["std"]
This completes STL statements on 'tab', '.', '::' and '->' even when 'using namespace std;'. Don't do it if you hate magenta.
Besides ctags / cscope,
what are good vim plugins for navigating C++ code base?
Ideally, when my cursor is on a variable name, I would like to be able to know:
what is the class of this variable,
what functions can I call on this varaible,
jump to the class of this varaible ...
and do this across multiple namespaces.
What tools do you suggest?
You might look into Eclim, Vim and Eclipse integration. Best of both worlds.
I am a big fan of ctags Hence I am wondering if I have cscope, will I benefit more there two programs. Seems like the latter has the same features as ctags, namely, facilitating the finding of symbols.
What are the features scope offers that can further increase my productivity with VIM?
Thanks
cscope can certainly improve your productivity.
ctags only allows you to navigate to the declaration of a symbol (one-way lookup).
cscope allows you to:
Go to the declaration of a symbol
Show a selectable list of all references to a symbol
Search for any global definition
Functions called by a function
Functions calling a function
Search for a text string
Search for a regular expression pattern
Find a file
Find all files including a file
Tutorials:
Vim/Cscope tutorial
Using Vim and cscope within cygwin
Related SO questions:
cscope or ctags why choose one over the other?
What is a good tool to aid in browsing/following C code?
Vim+ctags tips and tricks
I'm using Vim for a C++ project that I've started working on and I've been spending a lot of time lately browsing through the existing code to get a hang of it. To make the browsing easier, I've setup ctags and cscope in Vim to jump to definitions and find references.
However, I find that neither of them are intelligent enough to know which class a member variable/function belongs to. For example:
class Square;
...
Square a;
...
a.rotate();
Attempting to find the definition of rotate() will bring up member functions from other classes too, like Triangle. I use g] in Vim to bring up a list of matching tags and fortunately ctags lists the associated class for each entry. However, when there are 200 classes with the same member function, it can be tiresome to hunt down the correct tag.
Also, if I am at a function definition:
Square::rotate()
{
...
}
Attempting to find all calls to rotate() using cscope brings up calls to Triangle's and other classes' rotate functions.
Because of this, I find myself jumping to Visual Slickedit every now and then to find the definition or reference to a member function or member variable. Is there any way I can accomplish this in good old Vim?
SrcExpl might be the plugin you needed. Try it.
I've looked for better solutions than cscope in the past, but never found something. In the end perhaps cscope lack of intelligence isn't really that much of a bother.
The problem is that there is no powerful and open intellisense library on the market. Perhaps CodeInsight.
Seems like this would be a good candidate http://vim.wikia.com/wiki/C%2B%2B_code_completion. I had some good luck with it doing similar things in Java. Not entirely sure it gets you everything you're trying to do though.
You could try taglist plugin, though it still to some extend suffers the same problem as the built-in ctag support. It works the same sort of way that SrcExpl does.
Many times when I am watching others code I just want to find where and how a variable is defined. Normally what I do now is look for the type of the variable until I find the definition, that is very time consuming. And I guess that there are some tools that can help me in this rutinary situation. Any suggestion in some tools or commands to help me in this task?.
I know that using a GUI and creating a project this is done automatically I am talking of a way to do this without a GUI. I am working with only text mode. I am running under Linux and I am using C/C++, but suggestions for other languages are welcome.
Thanks a lot.
A possible solution
Michel in one of his comments propose a simple an effective solution define again the variable, in that case in compilation time, the compiler will inform where is the previous definiton. Of course to apply this solution we need to think previously in the locality of the variable.
You've already given the most appropriate tool: an IDE. This is exactly the kind of thing which an IDE excels at. Why would you not want to use an IDE if you're finding development painful without one?
Note that Emacs, Vim etc can work as IDEs - I'm not talking about forcing you the world of GUIs if you want to stay in a text-only situation, e.g. because you're SSHing in.
(I'm really not trying to be rude here. I just think you've discounted the obvious solution without explaining why.)
Edit: OK, you say you're using C++. I'm editing my response. I would use the C preprocessor and then grep for the variable. It will appear in the first place.
cpp -I...(preprocessor options here) file.cpp | grep variable
The C preprocessor will join all the includes that the program uses, and the definition has to be before any usage of that variable in the file. Not a perfect thing, but without an IDE or a complete language description/managing tool, you only have the text.
Another option would be using ctags. It understands the C and C++ syntaxes (among others), and can be searched for variables and functions using command line tools, emacs and vi, among others.
I use cscope and ctags-exuberant religiously. Run it once on my code base and then in Vim, I can use various commands like ^] or [D or [I or similar to find any definitions or declarations for a given word.
This is similar to facilities provided by mega-IDEs like Visual Studio and Eclipse.
Cscope also functions as a stand-alone tool that performs these searches.
I use one of three methods:
I will use CTags to process my source tree (nightly) and then can easily use commands in Vim (or other editors) to jump right to the definition.
I will just use grep (linux) or findstr (windows) to look for all occurrences of the variable name or type. The definition is usually quite obvious.
In Vim, you can just search backward in the scope and often find what you are looking for.
Grep for common patterns for variable declarations. Example: *, &, > or an alphanumeric followed by one or more whitespace characters then the name of the variable. Or variable name followed by zero or more whitespace characters, then a left parenthesis or a semicolon. Unless it was defined under really weird circumstances (like with some kind of macro), it works every time.
In VIM you can use gd to see local variable declarations or gD to see global variable declarations, if they're defined in the current file. Reference Go_to_definition_using_g
You can also use [i to see the definition without jumping to it, or [I to see all occurrences of the variable in all the included files as well, which will naturally show the definition as well.
If you work in Microsoft Visual Studio (which I think you could use for C++ as well, but would require working on a Windows workstation) there's an easily accessible right-click menu option for "Go to Definition...", which will take you to the definition of any currently marked variable, type or method.
if you insist on staying text mode, you can do this with either emacs or vi with the appropriate plug-ins.
But really, move into the 21st century.
EDIT: You commented that you are doing this over SSH because you need the build speed of the remote server cluster.
In that case, mount the drive on your local machine and use an IDE, and just SSH in to kick off a build.