bat icon indicating copy to clipboard operation
bat copied to clipboard

Add `bat --wrap word`

Open oficsu opened this issue 1 year ago • 4 comments

The current --wrap character behavior

echo Lorem ipsum dolor sit a met,   \
     consectetur adipiscing elit.  \
     Nullam venenatis nec est     \
     eu tristique                \
| bat --decorations=never       \
    --paging=never             \
    --language bash           \
    --theme Nord             \
    --color=always          \
    --terminal-width 11    \
    --wrap character      \
    -

image

Suggestion

I suggest a new word mode for the --wrap option, which will split lines only by spaces (except in cases where a word is too long for a single line). These spaces should then be stripped

An example of how it should look with --wrap word:

image

oficsu avatar Sep 01 '24 14:09 oficsu

Issue #2751 is related and this comment also provides a shell workaround.

einfachIrgendwer0815 avatar Sep 02 '24 13:09 einfachIrgendwer0815

@einfachIrgendwer0815, unfortunately, this workaround will break syntax highlighting, which goes completely against the initial purpose of using bat for syntax highlighting:

echo Lorem ipsum dolor sit a met,   \
     consectetur adipiscing elit.  \
     Nullam venenatis nec est     \
     eu tristique                \
| bat -p                        \
| fold -w 12 -s                \
| bat --decorations=never     \
    --paging=never           \
    --language bash         \
    --theme Nord           \
    --color=always        \
    -

image

oficsu avatar Sep 02 '24 14:09 oficsu

Ok, I just have found a better workaround that works in my case:

echo Lorem ipsum dolor sit a met,   \
     consectetur adipiscing elit.  \
     Nullam venenatis nec est     \
     eu tristique                \
| fold -w 12 -s                 \
`# replace newlines by unique` \
`# invisible character that ` \
`# is ignored by syntax    ` \
`# highlighting           ` \
| sed -zE "s/\n/\x2062/g"  \
| bat --decorations=never \
    --paging=never       \
    --language bash     \
    --theme Nord       \
    --color=always    \
    -                \
`# revert it back ` \
| sed -zE "s/\x2062/\n/g"

image

oficsu avatar Sep 02 '24 15:09 oficsu

would love to see this feature implemented as well. using bat to make man pages more readable is great but not having an option to soft wrap or word wrap makes it less readable than it otherwise could be. less has this feature but of course, once the man pages get passed through bat for man syntax highlighting, it ignores/remove this formatting.

gospelranger avatar Apr 16 '25 22:04 gospelranger