How to get to a specific point in terminal?

By TheBllindGuy07, 17 December, 2024

Forum
macOS and Mac Apps

Hey so maybe I need to understand how the marks work in the terminal app on mac.
Basically I ended up getting a huge error message when I was doing something. Is there a way to get the cursor (including especially that of VO) to just after the initial command I have typed and so just before whatever output I get so I can read it from the beginning?
Thanks a lot!

Options

Comments

By TheBllindGuy07 on Wednesday, December 18, 2024 - 06:36

Scrolling with page up/down or the 3-finger gestures and dragging the finger on the trackpad is a good temporary workaround. Like the beginning of the error was on page 3/10! :)

By Brian on Wednesday, December 18, 2024 - 06:36

You can also save a print out of the terminal window to a dot TXT file, usually saves in your documents folder. I think it's just command plus S, if I am not mistaken.

By Blade Runner on Wednesday, December 18, 2024 - 06:36

First, make sure you are working in a tab rather than a window. For reasons I haven't figured out yet, vo seems to work better in a tab.
For large outputs, it's best to pipe or redirect it to a file or into a pager such as less. Sence you know that the output is sent to the error stream (stderr), you can just repeat the command in question with the following added to the command line: 2>errors. That will direct the output from stderr to a text file called errors that you can read at your leasure.
It's worth noting that the vo command to go to visiblal beginning vo-shift-home seems to be broken at the monment. It jumps vo to the vary beginning rather than the visible. So, to get to the beginning of an open file in the terminal window, you will need to first vo-shift-home, move vo forward a bit with vo-right, then find the beginning with vo-shift-end.

By TheBllindGuy07 on Wednesday, December 18, 2024 - 06:36

Thank you. Oh I thought that vo-shift-home wasn't behaving normally too. Will send yet another feedback to the golden apple! :)
Edit: FB16116977

By João Santos on Wednesday, December 18, 2024 - 10:36

I actually use the vanilla MacOS Terminal a lot, so here are a few tips that I can share from the top of my head:

The first thing I recommend is to download TextMate. Even if you don't use it for coding due to its lack of support for language servers, it's still useful to use as a dumb pager or just pipe long output to, and in my experience it's fully accessible. TextMate has a command-line utility called mate, installed from its Preferences window, that can be used to open files in or pipe output to the editor from terminal, and there's even a remote script called rmate that can be used to do the same from remote systems.

With TextMate installed and properly configured, reading long output is just a matter of using shell redirection to pipe it to the aforementioned mate or rmate utilities as mentioned earlier by Blade Runner. For example if you want to fetch and display the HTML content of the front page of this site in Terminal you'd type something like the following:

curl 'https://www.applevis.com/'

However the above would produce a very long output and even some diagnostic messages. Since curl sends diagnostics to stderr (file descriptor 2) and normal output to stdout (file descriptor 1), you can get just the normal output sent to TextMate and leave the diagnostics in terminal using just a pipe as follows:

curl 'https://www.applevis.com/' | mate

Or you could even silence the diagnostic messages by redirecting them to /dev/null while still getting the output in TextMate as follows:

curl 'https://www.applevis.com/' 2>/dev/null | mate

You could even get both the output and errors sent to TextMate as follows:

curl 'https://www.applevis.com/' 2>&1 | mate

In some situations, like when compiling a lot of code, you might want to follow the progress slowly unfold in Terminal and still get the final result in TextMate, and that is also both possible and easy to accomplish using the standard tee utility.

The following example pipes the output through head to limit it to 10 lines, and then through perl to throttle the output to a single line per second for demonstration purposes, so it's a little more convoluted, but what matters here is the redirection through tee that also sends the output to terminal right before redirecting to the mate utility:

curl 'https://www.applevis.com/' 2>&1 | head | perl -pe 'select STDOUT; $| = 1; sleep 1' | tee /dev/tty | mate

My second recommendation is to set the EDITOR environment variable to make programs like git use TextMate when they need to invoke an editor, by adding the following line to your ~/.zprofile file:

export EDITOR='mate -w'

My third recommendation is to set the PAGER environment variable. The purpose of that variable, and other similarly named variables with utility-specific prefixes, is to automatically pipe long output to another utility that can be used to make it easier to read it. By default almost every program uses either the less or more utilities, but I have mine set to call the aforementioned mate utility to display the content in a TextMate window after stripping off all the terminal controls that do not make sense in plain text.

At the moment I have the following two distinct PAGER-related definitions in my ~/.zprofile file, which I strongly recommend studying before actually using because I'm just a random Internet person that you should not trust by default:

export PAGER='perl -pe '\''s/\e\[[\x30-\x3f]*[\x20-\x2f]*[\x40-\x7e]//g'\'' | col -xb | mate'
export GH_PAGER='sh -- command eval perl -pe '\''s/\\e\\[\[\\x30-\\x3f\]\*\[\\x20-\\x2f\]\*\[\\x40-\\x7e\]//g'\'' | col -xb | mate'

The PAGER declaration is for all applications in general, whereas the GH_PAGER is a hack that I devised to work around a problem in the GitHub command-line client where it behaves in a non-standard way by not invoking a shell to interpret the default PAGER commands. The regular expression used in the Perl invocation follows my interpretation of the ANSI CSI terminal escape sequences referenced at Wikipedia, and the col utility is a standard POSIX tool that removes or reinterprets all the remaining controls that my regular expression doesn't strip.