When coding, I like to check the code by running :! g++ %. I map the command to <F5>. sometimes it takes a while to compile and I want to see the errors without spending time recompiling. Also, sometimes I want to compare the new output to previous one.
Is there a way to see the previous output of :! ...?
Provided you have g++ configured with makeprg, you can use :copen to reopen the last list of errors from the :make command.
set makeprg=g++\ %
Then, to compile, use
:make
When the compile completes, any errors will be listed in the quickfix window, which can (assuming errorformat is correctly configured) be used to jump to the lines on which errors occur. This usually works out of the box for C/C++.
If you dismiss the quickfix window, retrieve the last error list with
:copen
Review :help quickfix and :help makeprg for full gory details on how this works.
If you are using Vim from the terminal just suspend vim with Ctrl+z.
This will send you back to your terminal. You should see the commands that you just ran via :!. To get back to vim issue the fg. Note: This maybe different depending upon your shell.
:sh starts you a shell within Vim. If you're using Vim in a terminal, this switches back to what's known as the "primary buffer", which means that you see the history from the terminal before you started Vim, as well as any of the :! commands you ran in Vim.
You can get back to your Vim session with Ctrl-d.
Michael gave you the right answer: :makeis the way to go. The old :!compilation-command is to be forgotten. Vi is, well, ... history.
Now to clarify : you'd almost never have to tweak &makeprg. The default value is the one you want to use, unless:
you're using something else to compile (ant, ...)
you want to compile a mono file program, and you don't want write a Makefile, and you're under mingw whose gnumake installation s*cks (all other installations of gnumake do permit to transform foo.c/cpp into foo(.exe) without having to write any single Makefile)
you want to play with the compilation directory, or with things that can't be injected through $CXXFLAGS, $LDFLAGS, $CFLAGS, etc.
Related
I'm trying to make the jump from VS Code to pure terminal+vim development and I'm missing one efficiency booster from VS Code I used often. When I make some project, and errors are present, the stdout shows the path to the source file with the error, along with the line:column number. In the VS Code terminal I could just left-click with a mouse on that and it would open the VS Code text editor (with the VIM emulator enabled of course), to that location. Is there any similar time saver in the terminal and/or vim? I.e. something that would enable me to go directly from make error to editing that location in vim rather than reading the error log, memorizing the path:line:column and opening that file manually?
For example, lets say I had the following line within the make output:
/home/myusername/someProject/src/foo.c:30:5: warning: implicit declaration of function 'bar'; did you mean 'barr'? [-Wimplicit-function-declaration]
30 | bar(26);
| ^~~~~~~~~~~~~
| barr
Is there any easy way to jump to /home/myusername/someProject/src/foo.c:30:5 without highlighting it with a mouse and copy/pasting it?
The closest thing I found on SO so far was this but I'm guessing the make output will catch errors that only occur after attempting to compile, whereas something in live time like Syntastic is only going to catch syntax errors. I haven't used Syntastic yet, but I'm guessing what they catch has some union of items, but it obviously would be to costly to continuously be compiling in the background to check for compile errors.
I also note the errors themselves, i.e. -Wimplicit-function-declaration are hyperlinks. Maybe there is some setting in make/cmake to generate links for the src files as well?
The user manual, a mandatory read if you are serious about using Vim, dedicates a whole section to compiling: :help 30.1.
In a nutshell…
You choose what Vim calls a "compiler" with the :help :compiler command:
:compiler gcc
It tells Vim:
what external command to execute for compiling your code,
how to interpret the eventual error output.
You execute the :help :make command:
:make %
It tells Vim:
to execute the external command defined above,
and to capture any output in case there are errors to report.
You use the :help quickfix feature to display, navigate, and interact with eventual errors:
:cwindow " opens the quickfix window if there are valid errors
:cn " jump to next error
etc.
FWIW, Vim has very strong roots in C and it shows all over the place. Since running make is the default behavior of the :make command and the default :help 'errorformat' already handles most common C compilers, doing:
:make
followed by:
:cw
in your current project should give you a taste of what to expect:
Real life considerations…
Setting the compiler manually for each file you edit is a PITA. It makes a lot more sense to spend a few minutes putting together an autocommand.
Vim comes with a large and growing collection of "compilers". Check out $VIMRUNTIME/compilers.
Vim provides the basics of the "edit, compile, run" cycle but the experience may not be polished enough out of the box. For example, you might want to open the quickfix list automatically when there are errors, etc. There are basically two ways to address this: using some third-party plugins and/or writing your stuff yourself. I tend to like the latter the most (see this and this for example) but the third-party plugin path is valid, too.
I recently dumped all ide's and decided to program solely with vim. So far it works good, but I have a minor annoyance. Everytime I want to run my recently compiled program, I have to write
:!./myProgramExecutable
I could do this in my .vimrc:
nmap <key> :!./myProgramExecutable<CR>
but that would only work for executables of that name. Is there a way to generalize this command for my current project in CMakeLists.txt for example? (or another way to find the correct name)
If the program name can be derived from the current buffer's path and/or name, you can use :help filename-modifiers in the command execution.
However, if it's okay for you to specify the program name once, a neat trick is that :!! repeats the last :! command with the same arguments. If you bind that to a key, you have a quick way to re-execute it.
Oh, and if you're working with Makefiles or similar, why not create a target (always with the same name) that executes the program; you can then do that from Vim via :make run, for instance.
So I am programming in C with VIM by using the make command to compile my code.
I would like to use the quickfix plugin to quickly move to the different compile errors but the only error that quickfix shows is the failed command in the make file.
I have failed to find any clues on google for this problem, is there something I could be missing to make this work?
The quickfix list (it's built in, not a plugin) parses the output of :make according to the rules in the 'errorformat' option, in order to extract file names, line numbers, and error messages.
Usually, you don't write those yourself, but you simply choose the appropriate compiler plugin. If your build uses GCC, you can set the compiler by
:compiler gcc
See :help compiler for details and a list of compiler plugins that ship with Vim.
I have finally solved the problem.
It turns out that if your shell is set to use FISH the output of the make command displays correctly but for some reason quickfix is unable to pick up any errors except for the failed part in the make file.
I solved the problem by switching vim to use bash instead by adding set shell=/usr/bin/bash to my .vimrc file.
Due to some complicated environmental variables required, I have chosen to run Make through GNU screen. Using the screen vim plugin, I have the following setup in my .vimrc:
map <Leader>mm :call ScreenShellSend("cd ".expand("%:p:h")." && make 2>&1 | tee /path/to/errorfile") <CR>
Roughly translated, this will run make in the current working directory through an existing screen session with all of the required environment variables preset. I can then see the output of that command in a separate terminal window.
My question is, assuming I output the results of make to a text file, how do I tell automate the vim make process to:
A.) set make to use a vimscript function, i.e. call SreenShellSend() instead of an external program.
B.) set errorfile to /path/to/errorfile
Unfortunately, you cannot set 'makeprg' to a Vim function or command, it has to be a shell program. (I wish for an enhancement that treats commands starting with a colon as Vim commands, similar to the :help exception in 'keywordprg', but haven't come around to implementing this.)
There are workarounds, though. One is to use vim --remote-send as 'makeprg' and use this other temporary Vim instance to call back into the original Vim. What I do though is overriding the :make command through the cmdalias.vim plugin for those buffers. That alias then simply :calls my function.
Once you're able to invoke a Vim function, setting the 'errorfile' is just a matter of putting a function wrapper around ScreenShellSend() and setting it in there.
I am using Vim as my main editors for a few days now.
Using http://www.github.com/astrails/dotvim as the base of my installation.
The problem I am having is that when I run the specs, the result of the specs is not delayed on the screen and I can't see what is going on.
to further explain this, I have a video to demo the situation:
http://www.youtube.com/watch?v=gUB48XwNq0M&feature=plcp
I need to hide vim with CTRL+Z and then fg to show it again, that's obviously not good.
Would love help on this
** Edit **
I posted about the problem in more detail here:
http://avi.io/blog/2012/08/05/problem-with-running-spec-in-vim/
I would first check if this is the same behavior for any shell command you run, or just when you run spec files.
E.g. how does a simple run of 'ls' from within VIM behave?
:!ls
If the above waits for 'Enter', I would guess that you have a trailing space (or similar) in the mapping that runs your spec.
For example, adding this line to your .vimrc will map Leader-l to the 'ls' command and will prompt for 'Enter'.
nmap <leader>l :!ls<cr>
However if you add the same line with a trailing space at the end, it will exit the console immediately:
nmap <leader>l :!ls<cr>
So I would check the mapping you use to run the specs.
This might have something to do with :help more, but I think the best solution for this sort of thing is a combination of tmux and turbux.vim. See the video here.