There are many questions for this actually, but none of them seem to work for my specific needs.
I am using tmux and therefore my bash window is pretty small ($COLUMNS = 45). This means that pretty much any output is going to get wrapped at its maximum width, which can look pretty gross because long words are getting wrapped in the middle.
While many solutions (e.g. fold -w 30 -s $FILE) works fine with files, I can't seem to pipe output from running a program like python or nodejs. For readability's sake, I'd like to have their outputs wrapped at whitespace.
So for example, when I intentionally generate an error with nodejs asdgasgsdg by passing a non-existent file, I want the output to be:
Error: Cannot find module '/home/User/
Documents/cwd/asdgasdg'
at Function.Module._resolveFilename (
module.js:326:15)
at Function.Module._load (module.js:277:
25)
at Function.Module.runMain (module.js:442
:10)
at startup (node.js:136:18)
at node.js:966:3
Instead of:
Error: Cannot find module '/home/User/Docume
nts/cwd/asdgasdg'
at Function.Module._resolveFilename (modu
le.js:326:15)
...
I'd like a simple command that can force this kind of wrapping. I was thinking about something like nodejs non-existent-file.js | fold -w 45, which doesn't work.
I've also tried piping the output to a file then opening the file with fold, but I couldn't get nodejs to pipe its error message to output.txt. I don't want to manually force the script to log to a file but instead force the terminal to do the job. (i.e. I want a working equivalent of nodejs non-existent-file.js > output.txt; fold -w 45 -s output.txt. When I tried it, output.txt was empty.)
Is this possible?
nodejs non-existent-file.js 2> output.txt; fold -w 45 -s output.txtand also see mywiki.wooledge.org/BashFAQ/047 on how to redirect stderr to pipe