3

I'm trying to make a couple of empty lines right before first line of running commands in terminal and also another one after last line (for always even running any command), because when I edit PS1 I only can add empty line before first line but it's gone when I run some commands if it's have long result, i.e: running update which cause ignore added empty line.

some thing like this(until running update):

enter image description here

When I maximized Terminal window I can have this last empty line, then I want that also in normal window. Empty line before first line isn't important but having last empty is very important for me. How can I handle the result of running commands? if I achieve this then I able to have last empty line. please!

  • To what have you edited your PS1 to have such a behaviour? – Gremlin Sep 24 '14 at 17:38
1
+50

I preserve previous answer for myself, and add one more depending on our discussion.

This is not perfect solution, but I think we can improve it together:

#!/bin/bash

new_line(){
    echo -e "";
}

n=0  # line counter
H=$(stty size | cut -d" " -f1)  # this is height of current terminal
# uncomment next line if you want to add empty line after (height - 1) lines
# H=$((H - 1))
new_line
eval $@ |
while IFS= read -r line
do
    echo $line;
    n=$(($n+1));
    if !((n % H)); then # every H lines it is true
        new_line
    fi;
done
new_line

Create this script with any name, in my case it is test.sh, make it executable.

Now run this script and path command that you want as arguments

$ ./test.sh sudo apt-get update

Update

Final version of script that worked for @KasiyA, but not work for me:

#!/bin/bash 
eval $@ | 
  while IFS= read -r line
  do 
    echo $line
  done
| improve this answer | |
  • I just use this line #!/bin/bash eval $@ | while IFS= read -r line;do echo $line;done and it worked :) – αғsнιη Sep 24 '14 at 19:45
  • I tested it out in gnome terminal - didn't work – Sergiy Kolodyazhnyy Sep 25 '14 at 5:44
  • @c0rp Yes. I ran both scripts, then ls couple of times to fill the screen. I'll try again in a second. I'm messing with my bashrc file right now and fonts, so bear with me – Sergiy Kolodyazhnyy Sep 25 '14 at 6:32
  • 1
    @c0rp, Oh, I probably missed the point: the script is supposed to keep at least one last line clear while the command is running, not redraw the screen completely is that right ? – Sergiy Kolodyazhnyy Sep 25 '14 at 6:39
  • @c0rp, nope, with script or without script, I get last line of text flashing as if it was cursor. I don't know if i had that before , but i don't think the script does anything here. I ran sudo apt-get install libreoffice -s with both script and without. Same effect – Sergiy Kolodyazhnyy Sep 25 '14 at 6:44
0

If you want exactly blank line try this:

Open terminal and create this two functions, then execute trap:

$ preexec () { echo -e ""; }
$ preexec_invoke_exec () {
    [ -n "$COMP_LINE" ] && return  # do nothing if completing
    preexec
}
$ trap 'preexec_invoke_exec' DEBUG

Now try to execute any command and see results

Be sure that help trap says:

If a SIGNAL_SPEC is DEBUG, ARG is executed before every simple command.

If it works as you wish, add this functions and trap to your .bashrc

Also you may want to change preexec function to this preexec () { echo "-------------------------------"; }, because it is looks more distinguished.


For more information read this:

  1. Wrapping bash commands

  2. Bash hook before execute command

| improve this answer | |
0

Best solution: I found a very interesting command stty. From man stty: stty - change and print terminal line settings.

In tty1 I ran this: sudo stty rows 20. Now my tty screen is limited to 20 rows, which allows for 20 rows on the screen. It works with sudo apt-get install libreoffice -s, and any other command.

Now, it doesn't seem to work with gnome-terminal or xterm, but right now I'm working on solution. Probably, have to change something in .bashrc file.

A minimalistic solution to the problem would be to use less or more or pg. For example , if i simulate installing libreoffice,

sudo apt-get install libreoffice -s | less

That way you have last line replaced with : , and still be able to run the command. The only draw pack is that you have to press enter, to skip to the next page in case of pg or next line in case of less/more.

Another solution is this to use head or tail command. This will work well if you don't care about the whole output, but only first and ending part of it. Just as the name suggests, these allow you to see either top(head) of the output or end (tail) of the output. For example,

 sudo apt-get install libreoffice -s | head -n20;

Which will give you first 20 lines of the output (default is 10, from what i found out). To get last 20 lines same thing with tail:

sudo apt-get install libreoffice -s | tail -n20;

The problem with these solutions, though, is that if you are running some interactive command with long output, this will probably prevent you from interacting with that commmand, so keep this in mind.

| improve this answer | |
  • no,not worked. I only see the first 20 line not 20 last line – αғsнιη Sep 25 '14 at 7:38
  • @KasiyA, head -n20 is for 20 first lines, tail -n20 is for last 20. Sorry. Let me edit in just a sec – Sergiy Kolodyazhnyy Sep 25 '14 at 7:41
  • I know that but still not work. with head I just see 20 first lines and with tail I just see nothing until the result comes to last 20 lines. I don't want to cut the result I want to see every time last lines with new line at the end. also @c0rp's answer [last script] worked for me. – αғsнιη Sep 25 '14 at 7:51
  • @KasiyA, yes I mentioned in my answer that head and tail are used to cut the output in case you only need to see last or first values. Also, have you tried stty command ? Like I said in the answer, it worked for tty, but I'm still searching for a way for this to work in xterm. – Sergiy Kolodyazhnyy Sep 25 '14 at 7:53
  • I also tried with sudo stty rows 20, this is not make a new line at the end. it just limit command result in 20 rows. – αғsнιη Sep 25 '14 at 7:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.