0

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?

topher217
  • 1,188
  • 12
  • 35

1 Answers1

1

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…

  1. 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.
  2. 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.
  3. 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:

make

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.
romainl
  • 186,200
  • 21
  • 280
  • 313