9

I want to display a file in Neovim with ansi terminal escape codes interpreted.

I created a file which contains ansi escape codes (only colors for now, we can think about others as a bonus):

echo '\e[31mred\e[m other \e[32mgreen\e[m end' > file

Now if I open this file in neovim I can see and edit the escape chars but the colors are not displayed: nvim file

nvim plain

I know about the AnsiEsc plugin and it can do the job pretty good: nvim file +AnsiEsc

ansiesc

but it misses the point about my color scheme and it is an external plugin. Also it uses syntax groups and these are limited in number and have to be defined manually by the maintainer. I am not sure if it even can interpret other stuff besides color escape codes. (Edit: I have since reimplemented an advanced version of this approach inside Nvimpager in lua. It generates the syntax groups dynamically by parsing the input file and can handle many terminal attributes (italics, bold, underline, ...) in addition to colors. But it suffers from one restriction that is also present in AnsiEsc: It can not know the correct 24bit color of the default terminal colors, so like AnsiEsc it displayes them slightly of. The code is here.)

Obviously the internal :terminal should solve these restrictions, but if I use :terminal cat file to view the file it adds extra text at the end and also adds extra empty lines to fill the window: nvim '+term cat file'

nvim terminal

The :terminal of plain vim does a better job in this regard: vim +'term ++curwin cat file'

vim terminal

It doesn't print extra text at the end of the command output (no message and no empty lines). But then again I want to use neovim :(

Question: Can something as close as possible as the vim version be achieved with neovim's :terminal?

EDIT: I since came half a step further: If I use nvim '+term tail -f -n +1 file' the process never terminates (tail -f) and hence the indicater that neovim normally prints is gone. I did not see a problem with the running tail process in my use case but it would obviously be nicer if the process was finished. That's why it is only half a step. The additional lines at the end are still there:

enter image description here

1 Answer 1

1

Quite frankly, what you're trying to accomplish may not be the most efficient use of different utilities. See, a text editor should in general be used to edit raw text, while other utilities, notably pagers such as less(1), should be used to view text while optionally applying text decorations, such as the escape sequences that introduce colors.

In other words, you may consider using less -R.

6
  • I would suggest to edit the answer to be more based on fact. 'Wrong' seems to vague for me :-) Commented Aug 9, 2023 at 5:19
  • Thanks for the suggestion. I'll see how to rephrase my answer a bit.
    – dsimic
    Commented Aug 9, 2023 at 6:10
  • 1
    Looking good enough now? :)
    – dsimic
    Commented Aug 9, 2023 at 6:36
  • Much better to me ;-) Thanks :-) Commented Aug 9, 2023 at 9:24
  • 1
    Great, thanks once again for your suggestion. :) I sometimes forget that some things may be wrong for some people, but also just fine for other people. :)
    – dsimic
    Commented Aug 9, 2023 at 12:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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