What often happens when I work in a shell - zsh - is that I have cded somewhere and I want to have a quick look at the directory. Using ls may produce a lot of output and leaves it in the terminal. Using ls | less is better, however it again leaves traces when I quit less all its output is there. Do you have an idea how I can do something like ls | less but when I quit less, it leaves the terminal screen untouched.

up vote 1 down vote accepted

For me, less behaves as you describe - I need to use less -X to disable this behaviour.

You could try explicitly using the "alternative screen buffer" that many terminals implement.

This is used by editors like vim / nano, so that when quitting, the terminal is restored to how it was before.

Try this:

tput smcup
ls | less
tput rmcup

If tput is unavailable, then try using echo $'\e[?1049h' / echo $'\e[?1049l' respectively.

  • less -X does not change anything in this regards for me. – Yuki Aug 6 at 16:10
  • What about tput? – Attie Aug 6 at 16:20

Since you mention that you are using zsh, you can take advantage of zsh builtin tab-completion. You won't get a "long" ls listing, but you can preview names of directories and files by typing "ls " followed by a tab. You'll see what files/directories you could potentially "ls" in your current working directory, and then you can either ls one specifically, add a "-l filename" to view one in particular in detail, or just hit "Control-u" to clear the line you're on and prepare to enter a different command, and the tab-completion suggestions will disappear.

For instance, when I "ls "[TAB] on one of my VMs, I see:

user@fbsd:~% ls
.cshrc                   .mailrc                  .ssh/
.login                   .profile                 .zshrc
.login_conf              .rhosts                  code/
.mail_aliases            .shrc                    inventory.hw.gather.sh*

When I hit Control-u, the suggestions below my command line disappear.

  • Thank you. that is true, however I want something more generic. That, for example, I could do ls -lah, etc. – Yuki Aug 8 at 12:47

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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