I've started learning Rust, and have been setting up my Vim workflow to use it. I'm still relatively new to Vim, and mostly what I've written in it is PHP for a website. Now that I'm working with a language where the code is compiled and run, I'm keen to try using the Vim command output, especially while I'm learning and just writing simple programs.

I've added a keybinding so that <C-Enter> runs the command w | !cargo run, saving my current file, then building and running the project with cargo. The command works just fine, but the output isn't quite as easy to read as using a terminal, because I lose the colours that denote errors, warnings, help, etc.

Is there a way to get these colours to show up in the (g)vim command output, to make it as nice to read as from the terminal?

For illustration, here's an example of what it looks like in my terminal:terminal

vs. the output from gvim:enter image description here

  • 2
    Tip: you should consider configuring and using Vim's :make command for your builds. Besides the convenience the output goes to the "quickfix" window which is explicitly designed for edit-compile-edit workflow and easy navigation of source files listed in compiler output. – B Layer Mar 17 at 13:41
  • As for the coloring, the mechanism for displaying colors in terminals is to emit ANSI control sequences which are not recognized by Vim (though there are plugins that add some capability related to this). Vim's mechanism is syntax highlighting. Hopefully, someone answers with a method for applying same to compiler output. – B Layer Mar 17 at 13:55
  • Or, if you have no interest in make and quickfix and other such niceties just read Matt's answer. ;) – B Layer Mar 17 at 14:07
  • 1
    Thanks for the :make tip, I've used some of @D. Ben Knoble's answer to configure :make in a cargo project to run cargo run. – Michael Murphy Mar 17 at 18:53

Should be as simple as

set go+=!

Not applicable to Neovim though. If no success switch to :terminal instead.

  • That did the trick, thanks! For anyone wondering about the details, run help go-!. ! is a guioption that makes external commands be executed in a terminal window, instead of the usual vim output. Still shows up at the bottom of the gvim window though, so it's nice and clean! – Michael Murphy Mar 17 at 18:56
  • A couple of other answers and comments have suggested using :make as a more convenient way of compiling. Sadly this answer doesn't add colour to the output of :make. Any idea how to make that happen? – Michael Murphy Mar 17 at 18:58
  • @MichaelMurphy This is a quickfix list. You don't really want it to be coloured too much. It's about spotting error locations and jumping there quickly. It is enough to have right errorformat (and a few primitive syntax items by default). No need to colorize all fancy ansi escapes. – Matt Mar 17 at 19:43
  • I kind of get what you mean, but I don't think I'll really feel the benefit of the quickfix list until I'm working on something that has a lot of stuff to fix... If this really isn't the intention then fair enough, I'm just surprised that there would be no way of achieving it. – Michael Murphy Mar 18 at 8:04
  • @MichaelMurphy 1) Make sure all apps output color codes even if output is redirected (--color, -fdiagnostics-color or whatever); 2) Write custom syntax for qf filetype to convert ansi escapes to Vim syntax items (google for "vim ansi esc" and such). – Matt Mar 18 at 8:52

For colors

You'll probably have to use :terminal cargo run for colors; the integrated terminal supports all the usual color stuff.

The colors don't show up in !… commands because the output does not go to a TTY, and smart programs correctly disable color when they detect this case.

For Rust

As for working with rust, note that vim ships with strong support for rust (:help rust). That includes compiler/rustc.vim and compiler/cargo.vim, which can be used with :compiler + :make.

I've added a few tweaks in my setup, like my own keywordprg when RustDocFuzzy isn't available, an includeexpr I probably stole from a plugin, and the ability to select the right compiler for make based on the project layout.

  • :terminal cargo run didn't give me any colours, just opened a new buffer with the output, not highlighted. Thanks for all those Rust tips and snippets though. I've set g:cargo_makeprg_params = 'run' in my vimrc so I can use :make to run cargo run. I might take a look at the other things as I become more comfortable with the workflow. – Michael Murphy Mar 17 at 18:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.