Bat: Keep empty lines empty

Created on 28 Jun 2019  路  6Comments  路  Source: sharkdp/bat

It would be nice to be able to use nl to number only non-blank lines with this command:

bat -p --color=always foo.c | nl -bt

However, empty lines in foo.c have escape sequences after passing through bat so that nl adds line numbers to seemingly blank lines.

$ bat --version
bat 0.10.0
feature-request

Most helpful comment

If I understand correctly, you want to keep the syntax highlighting from bat while using nl for non-empty line numbering? I can confirm that it doesn't work as expected for that use case.

I'm not sure how @sharkdp would like to handle it, but I have a workaround with sed that you could use (probably as an alias) in the meantime:

Bash:

bat -pp --color=always [...] | sed $'s,^\(\x1b\\[[0-9;]*[a-zA-Z]\)*$,,' | nl -bt

Fish:

bat -pp --color=always [...] | sed (printf 's,^\(\x1b\\[[0-9;]*[a-zA-Z]\)*$,,') | nl -bt

image

All 6 comments

@jzinn it works with --color=never or --color=auto.

If I understand correctly, you want to keep the syntax highlighting from bat while using nl for non-empty line numbering? I can confirm that it doesn't work as expected for that use case.

I'm not sure how @sharkdp would like to handle it, but I have a workaround with sed that you could use (probably as an alias) in the meantime:

Bash:

bat -pp --color=always [...] | sed $'s,^\(\x1b\\[[0-9;]*[a-zA-Z]\)*$,,' | nl -bt

Fish:

bat -pp --color=always [...] | sed (printf 's,^\(\x1b\\[[0-9;]*[a-zA-Z]\)*$,,') | nl -bt

image

To be honest, I don't think that this is something that we have to support by all means. If there is an easy solution for this, ok.

It鈥檚 marginal for sure, but worth making a issue, I thought, in case it鈥檚 an easy change.

This came about trying to highlight example code from Unix Network Programming, which does not number blank lines.

Closed in #766 by @mbarbar

This has been fixed in bat 0.13.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

gAmUssA picture gAmUssA  路  3Comments

yannallain picture yannallain  路  3Comments

mjlbach picture mjlbach  路  3Comments

niedzielski picture niedzielski  路  3Comments

adamtabrams picture adamtabrams  路  3Comments