If I exec less somefile in my terminal via bash, the text is no longer present when I exit the pager – which is really nice.
However, if I do the same from a ruby script, the paged text remains in the terminal’s text buffer:
exec 'less somefile'
How can I get the same behavior, where the text is no longer leaving gunk in the terminal? I have no idea how less pulls this off.
Well, to be perfectly honest, my
LESSenvironment variable accidentally containedX, which, as John Zwinck mentioned, preventslessfrom clearing the screen.For the curious, I did come across an article with some interesting information regarding this behavior. Apparently, terminals can implement the ability to save/restore the screen. This is often referred to “alternate screens”. To issue the necessary terminal codes, you can use
tput smcupandtput rmcup. It’s trivial to use this from Ruby where one might want to restore screen: