I want a similar behavior as less vim nano etc. uses.

Is this possible to achieve with bash?

4 Answers 4


If your terminal emulator supports it, you can do this using the same mechanism used by less, vim, et al, namely the ti and te termcap capabilities (smcup and rmcup in the more modern terminfo). These switch from the primary to the secondary screen, and back to the primary, respectively.

If your system has the tput command, you can use:

tput smcup # switch to secondary screen
tput rmcup # switch back to primary screen

Otherwise, for xterm, and probably for most other terminal emulators, this:

printf '\e[7\e[?47h'

should switch to the secondary screen, and this:

printf '\e[2J\e[?47l\e8'

should switch back to the primary (and put the cursor back where it was).

tcsh has a built-in echotc command, so you could do:

tcsh -c 'echotc ti'
tcsh -c 'echotc te'

(or just use echotc directly if you happen to be using tcsh).

  • 1
    tput smcup and tput rmcup seems to do the job.
    – Tyilo
    Commented Jan 1, 2012 at 13:37
  • 2
    If you’re going to hard-code the codes instead of using tput, try the modern 1049 instead of 47. This was introduced by Xterm and is supported by some other terminal emulators. It clears the alternate screen when switching to it, and leaves the contents in place when switching back (so you can view/copy the contents after the full-screen program has exited).
    – Chris Page
    Commented Jan 5, 2012 at 9:01
  • 2
    For robustness, if your scripting language supports it, arrange to switch to the main display even if your program exits prematurely. In a Bash script, for example, you could install a signal handler with trap 'tput rmcup' EXIT.
    – Chris Page
    Commented Jan 5, 2012 at 9:05

This is called the “alternate screen” (and as the top google hits show, you are unusual in liking this feature). Some terminals support two screen buffers, one used by full-screen applications, the other used by line-oriented applications. Bash doesn't know anything about them. If a command simply emits output to the terminal, you can use terminal control sequences to switch to the secondary screen and back (these commands are documented in xterm ctlseqs).

printf '\e[?47h'; mycommand; printf '\e[?47l'

Note that there is no scrollback for the alternate screen, so you'll only see the last screenful of output. If that's a problem, just do the simple thing:

mycommand | less 

There are only two screen buffers. If you want more, use a proper screen multiplexer such as Screen or Tmux.

  • I'm surprised to see how disliked it is. I really like being able to run an editor and still see my command history when I'm done. Commented Dec 30, 2011 at 8:51
  • There will be blank lines left after doing this in my term: cl.ly/Cz4N cl.ly/Cznp
    – Tyilo
    Commented Jan 1, 2012 at 13:35

Bash knows nothing about screens (it can run without being seen, in fact). You can redirect both standard output and standard error to any file or pipe, though.


None of the current answers seemed to work on my machine, but I found out about the command named screen, which seems to do the trick for me.

  • Not quite the same thing, but if it suits your purposes ... What terminal emulator are hou using? I think xterm has an option to (en|dis)able those control sequences; others likely do as well. Commented Dec 31, 2011 at 3:53
  • I'm using iTerm 2 for OS X.
    – Tyilo
    Commented Dec 31, 2011 at 3:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .