Managing multiple makefiles in vim - vim

I would like to effectively manage different project in Vim. When it is time to rebuild I have several Makefiles in different locations. In Vim if I use :make it will try to compile the Makefile in my current location.
I can use or :make -f URL-MAKEFILE but it's too much work. Is there a way or a plugin to build all those separate makefile in an efficient way without me remebering all Makefile url?

You can instruct vim to find a "Makefile" file upwards in your directory structure, then call :make with it
:exec ":make -f " . findfile("Makefile", ".;")
That way it will call the Makefile related to the project tree of the buffer currently open.
However, I'd advise not to use make -f path_to_makefile that much, because it launches the make command inside the directory where you are. Usually Makefiles are written to be used at the top of your project tree (and then eventually call more Makefile in the subdirectories), so it's more natural to call "make -C path_to_top_of_tree"
To find the first directory upward with a Makefile in it then call :make -C with it:
:exec ":make -C " . fnamemodify(findfile("Makefile", ".;"), ":h")

Don't know if there is a such plugin, but you can create a kind of a shortcuts for your URLs.
Create main makefile and add targets such:
.PHONY : shortcut
make -f URL target
Afterwards you can just call :make shortcut

I'm maintaining multiple projects, each one having its own makefile with one of the numerous local_vimrc scripts.
Actually I'm also encapsulating the update of the &makeprg option with my BuildToolsWrapper plugin (prefer Vim-Addons-Manager to install it). It goes further and enables me to choose the compilation directory and other options (which is quite useful when the makefile has been generated with CMake). See the two *.vim files here to see how I configure my projects.

Related

read tags when buildir is not srcdir

I' using an autotools project with source on
~/jhbuild/checkout/<project>
and compilation on
~/.cache/jhbuild/build/<project>
inside ~/.cache/jhbuild/build/<project> I can make
$ make ctags
and tags are generated correctly for ~/jhbuild/checkout/<project>
but I would like to open vim on ~/jhbuild/checkout/<project> to be able to navigate files directly (not only with tag definitions). Is there a way to instruct vim to search tags recursively on cache dir?
On ~/jhbuild/checkout/<project> I can make ctags -R * and mostly have the same (with a non recursive tag file) Is there any benefit in use autotools ctags rule? I see CTAGS_DEPENDECIES CTAGS_FLAGS CTAGS_ARGS but never see anyone using it

How to fix duplicate cscope ? is it a better way?

It is several years I am programming with vim and I used ctags.
I am working with a reasonably large C/C++ package and I need to find definition of functions. I usually use grep + ctags.
Recently I tried to use cscope instead of ctags and installed it with Vundle.
I see the following error for some of my files
E568: duplicate cscope database not added
I searched the web and found this:
https://blogs.oracle.com/natarajan/entry/avoiding_duplicate_cscope_database_error
It doesn't work.
How can I fix this?
Expanding on Artem's answer:
The Vim help for cscopeverbose is as follows:
If 'cscopeverbose' is not set (the default), messages will not be printed
indicating success or failure when adding a cscope database. Ideally, you
should reset this option in your .vimrc before adding any cscope databases,
and after adding them, set it. From then on, when you add more databases
within Vim, you will get a (hopefully) useful message should the database fail
to be added.
The problem here is that (a) there are multiple scripts attempting to load the cscope.out file and (b) they're not following the best practices of disabling the "verbose" cscope warnings before loading the file then re-enabling it afterwards, as suggested by the help text above.
The full error output should tell you which script is triggering this warning; for me it looked like this:
Error detected while processing /home/me_and/.vim/plugin/cscope_maps.vim:
line 42:
E568: duplicate cscope database not added
The fix was then to edit the ~/.vim/plugin/cscope_maps.vim file to add set nocscopeverbose immediately before the cs add ... lines. My version of this file already had set cscopeverbose immediately after, but if yours doesn't you should add that too.
Found the solution which worked for me (here: http://thoughtsolo.blogspot.com/2014/02/cscope-issue-duplicate-cscope-database.html):
Just add this line "set nocscopeverbose " to your ~/.vimrc file.
As per the blog, "This error pops up when VIM is already compiled with 'CSCOPE' module and you have also installed "cscopemenu.vim"". I assume that you have a vim executable with has been configured with --enable-cscope option.
Here's what I do:
Download cscope source and build it, install the executable in a directory which is available in your PATH
Download vim source code and configure it with --enable-cscope, build the source and install the executable
Download cscope_maps.vim and place it under $HOME/.vim/plugin directory. This contains cscope settings for vim.
Create cscope database out of the source and header files. You may do something like the following
find $PROJECT_HOME -name *.c -o -name "*.cpp" -o -name "*.cc" -o -name "*.h" -o -name "*.hpp" > cscope.files
cscope -qbR -i cscope.files
You can add these commands in an alias and excute the alias every time you want to update your cscope database. These two commands create finally create cscope.out database file.
Update .vimrc file to have the following
if has("cscope")
set csprg=<location to cscope executable>
set csto=0
cs add <location to cscope.out>
endif
I hope after doing these steps you should be able to use cscope with vim easily.
Note that if you are working on multiple projects, you should be able to add appropriate environment variables to enable vim to pick the correct cscope database.
To answer your second question, may I suggest using tagbar. This will list your function names in the current source or header file. You can install it using Vundle. Add the following line to your .vimrc
Plugin 'majutsushi/tagbar'
Add this to your .vimrc to toggle tagbar view
nmap <F4> :TagbarToggle<CR>
Note that F4 is just an example and you may use any binding to do the same.

How to run vim commands/scripts from project root?

I have a multitude of commands I'd like to run not in the current directory, but in the project root directory. i.e. going up directories until I reach some indication of a root, like .git directory for example.
For example running vimgrep -r (recursive) on all my project, or running tags generation recursively on whole project.
How do I get that path? The only close indication I found is this:set tags=./tags;~/Projects
But that just saves the string as it is into tags. Assigning something similar but for the use case described, gets me the string verbatim.
Any help is greatly appreciated!
Thanks!
Avoid the idea of "changing the working directory" or distinguishing between "working directory and project root", because almost no tool is prepared to properly handle those concepts.
The only tools that do (e.g. git) are those that don't care about the current directory to begin with.
Otherwise, it's madness to try to get everything working without bad side effects. "Working directory" is a concept too fundamental to even attempt to change within a running program.
The best approach is open a new Vim sessions inside directories where you want to do "local" things - and switch back to the "project" session to run project commands. Vim will protect you from accidentally overwriting changes in another session.
The alternative is to wrap commands in shells so they can have their "own" working directories, e.g.:
:!cd ../../..; ctags -R
(Which would allow you to regenerate tags file for the project, and not just the current dir)
or:
:!cd ../../..; grep -r foo **/*
But any output with file names would be relative to that root directory, and not the current one.
So you may prefer to do:
:!cd ../../..; vim
which creates a new Vim session within the current one, but in the context of the root directory.
Or, you may prefer the reverse (assuming Vim is running in the project root):
:!cd $(dirname %); vim
Which lets you work in the directory of the current file - and you'd have to exit to the main session to run project-wide tools again.
So instead of "changing" directories, you're "changing vim sessions" (either by having 2 sessions or "nesting" one in another like above).
I use a local_vimrc plugin to set project related variables. Then, I use (/write) plugins that rely on (buffer/project)-local variables to do stuff. (Unfortunately, most plugin out there rely of global variables which is not the best choice to specialize their behaviour to the current project in multi-projects sessions)
Regarding ctags generation, in use lh-tags that requires a few variables to be set.
Regarding grepping, as other, I usually start from the current directory which is often the root directory of my project. But you could also easily have a plugin/command that would run something like:
exe "vimgrep -r ".pattern." ".
\ map(file_extensions,
\ string(lh#option#get('project_root_directory').'/**').'v:val')
EDIT:
If you don't want other configuration files, and if the .git/ directory is enough to identify your project root directory, then, you can have this kind of function to get your project root directory:
function! ProjectRootDirectory()
return fnamemodify(finddir('.git', '.;'), ':h')
endfunction
Then when you'll run ctags, you'll have to execute ctags from the result of this vim function. I don't know which plugin you use to run ctags, at this moment. My lh-tags doesn't support functions through its configuration variables. It can easily been added if need be.
However, I don't see a simple way to configure &tags once from the .vimrc in order to configure this setting on a per project basis.
I personally like to keep it simple and have my current directory be my project root. I can use % to represent the current file in commands, e.g. :!git add %. Along with % you can use filename modifiers, e.g. :e %:h/foo.txt. See :h filename-modifiers.
However you mentioned that you use CtrlP and you like the way it changes the current directory. This means you need to be a bit more creative.
Here is what I would recommend:
Update your tags via git hooks. See Effortless Ctags with Git
If your tags are in .git/tags fugitive.vim will automatically setup these tags for you.
Use :Ggrep which uses git grep so it already know about your project root directory and as a bonus is much faster than :vimgrep.
Running arbitrary commands from the root is trickier. I use projectionist to manage my projects. Projectionist provides the command, :ProjectDo, which does exactly what you want.
Now a word of caution: Vim has no understanding of a "project". The closest thing to is 'exrc' option (See :h 'exrc') which is pretty lame.

VIM - Sourcing tags from multiple locations in project

Good day,
I typically work on relatively small (less than 20,000 lines of code) projects that are all self contained within a single directory, have their own Makefile, and are fairly easy to work with.
VIM is my preferred editor, and when I open a project, I typically build the ctags list via a mapping to the F10 key:
map <F10> :!ctags -R --c++-kinds=+p --fields=+iaS --extra=+q .<CR>
This allows me to jump to the definition of a variable/struct/etc via moving the cursor over the text, and hitting CTRL+], as well as using code completion with a drop-down list via OmniCppComplete.
However, I am now working on a slightly larger project which makes use of LOTS of structures. Furthermore, many of these structures have arrays of other custom structures as members, so code completion is a very useful and important tool for me right now.
I have two paths that include a lot of .C files and .h files, and they may change from machine to machine. On each machine, however, we have an environment variable in our .bashrc file that points to them like so:
SDK_SRC_PLUS_HEADERS=/public/sdk
THIRD_PARTY_SDK=/private/sdk
I would like to be able to have VIM automatically refer to the contents of these additional paths when I attempt to do code completion (via VIM's built-in OmniCppComplete feature), or to jump to the files in these locations when I use CTRL+] in VIM to jump to the definition of a struct, function, variable, etc.
So, for both of the above paths, I cd into them, and generate the tags via ctags -R. Then, I modified my ~/.vimrc file to include additional tags paths, like so:
tags=./tags
tags+=$SDK_SRC_PLUS_HEADERS/tags
tags+=$THIRD_PARTY_SDK/tags
I then cd into my project at /home/user1/projects/test, start VIM, and hit F10 in VIM to index it. However, this does not work at all. In fact, it breaks my ability to even use tags just for the project itself (ie: CTRL+] now does nothing).
Does anyone have any suggestions on how I could have code completion source tags and jump-to-definitions using multiple source directories via environment variables?
Thank you all in advance for your time and assistance!
I wanted to add to the solution provided by #sehe.
This is the final set of changes I made to my .vimrc. The first lines are for adding expanded environment variable paths to my tags variable. The other is for auto-updating tags in the event that I have to update my SDK and don't want to be able to accidentally use out-of-date tags:
" CTAGS tag generation for OmniCppComplete
set tags+=./tags
exec expand("set tags+=$SDK_SRC_PLUS_HEADERS/tags")
exec expand("set tags+=$THIRD_PARTY_SDK/tags")
" Can verify taglist is correct via ":set verbose tags?" command
" Create a mapping to delete the old tags, re-generate them, and use them
map <F10> :!ctags -R --c++-kinds=+p --fields=+iaS --extra=+q . \| rm -f $SDK_SRC_PLUS_HEADERS/tags \| ctags -R -f $SDK_SRC_PLUS_HEADERS/tags $SDK_SRC_PLUS_HEADERS/tags \| rm -f $THIRD_PARTY_SDK/tags \| ctags -R -f $THIRD_PARTY_SDK/tags $THIRD_PARTY_SDK/tags \| echo "Done re-generating tags."<CR>
It indeed appears to be the problem that you can't use environment variables inside the tags setting.
I came up with this as a workaround:
:let &tags.=expand(",$SDK_SRC_PLUS_HEADERS/tags")
This might be slightly more friendly:
:exec expand("set tags+=$SDK_SRC_PLUS_HEADERS/tags")

Vim run project specific make with key command?

I'm working on a compiled project in Vim (Typescript). make % will build an individual file. I use this to check for errors. This is great for error checking, but it creates compiled files next to the source files that I don't need.
For my actual build process, I have a single command that compiles everything. This is in a Makefile.
I'd like to be able to map a key command to "build my whole project" in a generic way, so if I'm editing any .ts file underneath my project directory, it runs that specific command.
How can I do this?
The trick would be to actually use a Makefile:
all: complete.exe
complete.exe: *.ts
somecompilation-command $^ -o $#
This way, you can just leave makeprg at 'make':
:set makeprg&
And happily do:
:mak

Resources