The following python code uses ansi escape sequences to clear the terminal screen and change the text color to red. It then prints "Hello from Vim."

print ('\033c\033[31m Hello from Vim.')

In Vim I use the command :!python3 % and everything works as expected. It clears the screen and prints in red. However, when I load the same code into Gvim and use the same command I get :

?c?[31m Hello from Vim.

It neither clears the screen nor prints in red. If from Gvim I split to the terminal with :term and then enter python3 tester.py, then code works correctly.

So my question is : Why does the command :!python3 % not work the same in Gvim as it does in Vim when it comes to ansi escape codes and what can I do to correct that?

Thank you for your assistance.

Edit: Additionally when I run this python script in Gvim :

i = input ('test: ')

and try to use the Backspace key to change the input, I get ^H printed out instead of the cursor backing over the input.

  • Somewhat related: vi.stackexchange.com/questions/24645/…, the behavior I see in GVim matches the one I see in :w !python3 % on the terminal. Since GVim doesn't have an alternate screen, I guess that's how it can handle output from an external program... – filbranden Apr 22 at 17:56
  • Odd—I would have expected this to just print the text, given that gvim isn't (and doesn't emulate, AFAIK) xterm. It would be odd to me if gVim was just an xterm-emulator inside an OS window (though then again, maybe that makes sense...) – D. Ben Knoble Apr 22 at 18:56
  • 1
    Those are terminal ansi escape codes, that means they work only in the terminal, not in a gui. And even that is not guaranteed to work, e.g. the default Windows console does not support those. So do not rely on those codes to be universally valid. – Christian Brabandt Apr 24 at 20:22

Your Answer

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

Browse other questions tagged or ask your own question.