tags:

views:

443

answers:

6

I'd like to have a blank line after my bash prompt and before the output on my Mac. It should look like this would:

echo; ls

Can I add a newline to my bash prompt and then go back up one line to wait for user input? Is there something obvious I'm missing?

+3  A: 

To my knowledge this is not possible unless you delve into more low-level stuff like full-screen emulators like curses.

Paul D. Eden
+3  A: 

This is a bit of a stab in the dark, but you may be able to use VT102 terminal codes to control the cursor without having to use Curses. The relevant VT102 commands that you'd be interested in all consist of sending ESC, then [, then the specific command parameters.

For instance, to move the cursor up one line, one needs to output:

ESC  [    1    A
0x1B 0x5B 0x31 0x41

Be warned that the VT102 documentation generally uses octal, so keep an ascii table handy if you're using hex.

All of this advice is given without having tested it -- I don't know if VT102 commands can be embedded into your bash prompt, but I thought it might be worth a shot.

Edit: Yeah -- looks like a lot of people use VT102 formatting codes in their bash prompts. To translate my above example into something Bash would recognize, putting:

\e[1A

into your prompt should move the cursor up one line.

HanClinto
You can go up and down all you want in the prompt, but by the time it settles down for input, the first output is going to be on the line immediately below the cursor which was previously on the line where the end of the prompt was and where the user typed a command. So there will be no blank line before the output.
Dennis Williamson
A: 

I believe (but haven't tried) if you put '\n\b' in the prompt string it would do that.

warren
+1  A: 

This is very possible. If your bash has C-v set as the readline quoted-insert command, you can simply add the following to your ~/.inputrc:

RETURN: "\C-v\n\C-v\n\n"

This wil make bash (readline, actually) insert two verbatim newlines before a regular interpreted newline. By default, only one is inserted, which is what causes output to start on the line after the prompt.

You can test if C-v is set to quoted-insert by typing it in bash (that's Ctrl+V) followed by e.g. an up arrow. This should print ^[[A or something similar. If it doesn't, you can bind it in ~/.inputrc too:

C-v: quoted-insert
RETURN: "\C-v\n\C-v\n\n"

~/.inputrc can be created if it doesn't exist. The changes will not take effect in running bashes unless you issue a readline re-read-init-file command (by default on C-x C-r). Be careful though. If you do something wrong, enter will no longer issue commands, and fixing your mistake could prove to be difficult. If you should do something wrong, C-o will by default also accept the line.

Adding a newline followed by moving the cursor back to the regular prompt (like you described) is possible, but will not have the effect you intend. The newline you inserted would simply be overwritten by the application output, since you moved the cursor back in front of it.

Pianosaurus
This doesn't work if the cursor isn't at the end of the line. Try: `RETURN: "\C-e\C-v\n\C-v\n\n"` which sends the cursor to the end of the line then does the extra newlines. However, an extra newline is always printed, even if you hit enter at an empty prompt
Dennis Williamson
A: 

In general, if you want to find out the codes to do anything a terminal can do, read the terminfo man page.

In this case, the cursor up one line code can be determined by:

tput cuu1

If you redirect the tput output to a file, you can see what control characters are used.

Bash also supports the PROMPT_COMMAND variable, allowing you to run arbitrary commands before each prompt is issued.

Jerry Penner
A: 

This works:

trap echo DEBUG

It doesn't add an extra newline if you hit return at an empty prompt.

Dennis Williamson