How to get to a specific point in terminal?

By TheBlindGuy07, 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 TheBlindGuy07 on Saturday, December 21, 2024 - 07:42

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 Saturday, December 21, 2024 - 07:42

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 Saturday, December 21, 2024 - 07:42

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 TheBlindGuy07 on Saturday, December 21, 2024 - 07:42

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 Saturday, December 21, 2024 - 07:42

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.

By mr grieves on Tuesday, January 7, 2025 - 07:42

Thanks for these tips - they look really useful. I had no idea that TextMate had a command line tool nor about the pager variable. Those exports didn't work for me but I will look at it a bit more closely when I have some more time free. But thank you very much for sharing.

You are clearly on another level to me. When I want to grab lines from the terminal I just interact with shell, move to the start, press VO+Enter to start selecting, then move to the end and press VO+enter then just copy to clipboard and paste elsewhere. Or Cmd+A, Cmd+C to copy the whole thing. But this feels a bit amateur in comparison.

By João Santos on Tuesday, January 7, 2025 - 07:42

Forgot to mention that, after adding content to ~/.zprofile you need to restart your terminal session, or if you don't want to do that, just type:

source ~/.zprofile

Which will cause the file to be executed in the current shell process, adding the new variables to the environment.

Another thing that I forgot to mention is that I am assuming that people are using zsh, which has been the default shell on MacOS for quite some time. If you are using bash, for example, the correct file is either ~/.bash_profile or just ~/.profile. Other than that the commands are exactly the same, as I try to remain as standards-compliant as possible.

To test whether the PAGER environment variable is correctly set, try running something that uses it, like man. For example, the following will open the manual page for zsh in TextMate if the environment is properly set:

man zsh

If the manual page ends up displayed on the terminal, that means that less is still the default pager, so typing lower case Q should return you to the command-prompt.

Finally, some commands do not work properly when redirected to tee, as they might assume that, since the standard output is not a terminal, they can buffer everything and dump it all at once when their buffer is full. Perl itself is notorious for this default behavior, which is why my output throttling example was rather convoluted.

By Sebby on Tuesday, January 7, 2025 - 07:42

Don't forget these tools of the trade on macOS. pbcopy puts whatever's sent to standard input onto the pasteboard in text form; pbpaste writes the pasteboard in text form to stdout. Together you can use them to make text available in the GUI or CLI without having to spawn additional subprocesses (I've done this, for instance, to edit root-owned configuration files using TextEdit). See also the "open" command, for launching GUI apps.

UNIX is wicked. And that's a big part of the reason why it would be hard to give up macOS.

By TheBlindGuy07 on Tuesday, January 7, 2025 - 07:42

I use these whenever I have the need since I first discovered both by pure accident with gpt an year ago and god WOW do I love them! :)
PS: I *do* love linux but if vim can't work for me because VO needs more than an m2 pro...

By Jason White on Tuesday, January 7, 2025 - 07:42

There's also the script command, which runs a subshell and records the entire interactive session in that shell, saving it to a file.
I just checked, and it's in /usr/bin/script, so you don't even need to use HomeBrew to install it.

For those who use Linux, note that the clipboard commands are different. The script command is available under Linux as well. Due to the BSD heritage of macOS, many of the standard tools are subtly different from the Linux versions in the command line options they support. I think you can install the GNU versions via HomeBrew, but I haven't needed to.

By João Santos on Tuesday, January 7, 2025 - 07:42

Was totally unaware of the existence of pbcopy and pbpaste, which might be quite useful to copy things like commit hashes, public keys, API tokens, and probably many other things that I'm not thinking about right now, but I'm sure I'll come up with lots more use cases as the need arises in the future. For example using pbpaste between backticks in a shell command to get whatever is in the paste board inserted at that location seems quite interesting. Really good stuff there! Thanks!

Another tip for AppleScript fans, osascript makes it possible to run scripts from the command line like this:

jps@teal ~ % osascript -e 'tell application "Music" to get [name , artist, album] of current track'
Time Goes By, Nitro Fun, Time Goes By - Single