4

I've customized the look/feel of my terminal prompt extensively so that it outputs the following (for development work):

== [~/current/path] (git_branch_name) $

I use the == to help identify the prompt lines when I'm looking at a big blog of text.

However, after using this for a few months, I find it's difficult to easily glance at the terminal and know what's what.

I had the idea that indenting all the output would help with that. I know I can change the color as well, but wanted to play with both solutions.

But I have no idea how to indent all output that gets sent to the terminal. MAN pages didn't help me and I couldn't find much on Google.

What I am trying to do

$ some_command_that_outputs_text All lines of output are indented 2 spaces... All lines of output are indented 2 spaces... All lines of output are indented 2 spaces... All lines of output are indented 2 spaces... $ another_terminal_prompt More lines are indented 2 spaces... More lines are indented 2 spaces... More lines are indented 2 spaces... More lines are indented 2 spaces...

Updated: 2014-10-24

Note that I have already customized my color scheme for my terminal as well as the prompt itself. I found that the color scheme wasn't enough for me personally to locate my commands as much of the text itself has similar coloring as my prompt itself.

3
  • If your goal is to make the prompts stand out, putting them in bold might be more effective. If you're using bash, you can add \[\e[1m\] to the beginning of $PS and \[\e[m\] to the end. See gnu.org/software/bash/manual/html_node/Printing-a-Prompt.html Commented Oct 23, 2014 at 21:03
  • @KeithThompson, you are correct. I have already highly customized my entire terminal prompt and color scheme. However, I've found that even that was not enough to easily glance at a mass of text and easily locate the last executed cmd. It's mostly my personal preference, but I do employ both color-coded and now indented. I'll play with the indenting for a while and see if it makes life easier :) Commented Oct 24, 2014 at 12:43
  • Hmm. Personally, I wouldn't want an environment that messes with the output of every command I run (which is not to suggest that you shouldn't be able to). And I wonder what effect it would have on full-screen commands. Commented Oct 24, 2014 at 15:37

2 Answers 2

5

In your current bash you can do the following:

exec 1> >(sed -r 's/^(.*)/  \1/g')

Or use that if your sed implementation does not support the -r flag:

exec 1> >(sed 's/^/ /')

That redirects the standard output file descriptor (stdout) to sed, that adds two newline to every line of the outout. Try it with:

$ ls -l
  total 0
  drwxr-xr-x 2 root root 40 Oct 22 16:35 dir
  -rw-r--r-- 1 root root  0 Oct 22 16:59 file
$
Sign up to request clarification or add additional context in comments.

9 Comments

Nice idea to use exec. sed command can be slightly simpler: sed 's/^/ /' (comment formatting doesn't show 2 spaces in the replacement part)
Is that something I can extract into a .bash_profile and use everywhere? I started playing with the colored prompt and I like it, but I would like to try out indented output. But in order for it to be "useful" I need to indent ALL output everytime I use a terminal. Note I haven't tried this yet, will get a chance to soon.
@DanL you could add that into one of your terminals rc-scripts. For example the ~/.bashrc...
I wasn't able to get exec 1> >(sed -r 's/^(.*)/ \1/g') working inside my .bash_profile. My terminal kept blowing up when it started (I use iTerm2). Also, when I tried just executing the command in a running terminal, it broke: "sed: illegal option --r". This may be a foolish question, but I was under the assumption I could just copy/paste exec 1> >(sed -r 's/^(.*)/ \1/g') into my .bash_profile and it would work...was that a bad assumption?
@DanL your sed implementation does not support the -r flag. Use exec 1> >(sed 's/^/ /') instead.
|
-4

You can use tput to move the cursor to an absolute position.

tput cup x y

Where and x and y are the row and column positions before echo the output.

man tput

For details.

7 Comments

I tried this out and it seemed to be more complicated than it should be; I don't know if I could extract this into a common solution I could use for every terminal.
Really? Complicated? How?For example. tput cup 10 10; echo "This is a test" Puts "This is a test" exactly on row 10 column 10. If that is complicated, then I don't know what is not complicated for what you have in mind.
it seems I have to enter the row/column dimensions every time for each line. What I was looking for was a way to automate this and store it in a .bash_profile, so that every time I open a terminal and output something, it gets indented relative to my cmd prompt so I can easily see where my last commands were executed. The sed solution works very well, but I couldn't get tput cup to work as seamlessly.
@RJ, ...complicated to use it to actually achieve the effect that the question asks for, ie. indenting each and every line of output, even when from a program that writes multiple lines.
So basically since you can't understand the use of tput, nor can use variables to automate the output you down vote? Seems not nice. Don't expect to helped a lot with such attitude.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.