23

Suppose I have some large datafile, which overflow the screen in both vertical and horizontal direction. How can I browse this file, while the header-lines stay on the screen?

For the moment, I am using less -S, so that I can nicely scroll my file horizontally and vertically. However, when scrolling down, the header lines obviously disappear. Is there a way to keep these using less?

An alternative is using vim in split-screen mode with :set nowrap. However, now if I scroll horizontally, the top window doesn't scroll in the same way (:windo set scrollbind only works for vertical scrolling as far as I know).

Bernhard
  • 12,272
  • What do you mean by header stays visible, while the data keeps scrolling? Do you mean that you only want the first line with the column names keeps the same? Is it only one file with data and does this data keep changing and therefore you only want to see the changed rows? Do you only want to see the first N rows or last N rows? – polym Aug 06 '14 at 16:10
  • 4
    @polym: less or tail -f that behave exactly like they do normally, except that the first line shown on screen would always be the header line. Like websites (or Excel) with a fixed header but scrolling body. – Mat Aug 06 '14 at 16:12
  • @polym ^ whatever he said! – Debanjan Basu Aug 06 '14 at 16:36
  • 1
    @polym, Think of how you can freeze a horizontal and vertical view in a spreadsheet. Columns frozen stay in place while scrolling. – user208145 Mar 11 '18 at 15:58
  • Oh boy, that asks for an extension to less, like a Freeze Pane point. For example --freeze-pane 10,2 would keep 1 line of column headers and 10 columns row header. Horizontal and vertical scrolling would preserve the row and column headers respectively. That would be really cool to use for a psql pager (http://merlinmoncure.blogspot.com/2007/10/better-psql-with-less.html) – Gunther Schadow Jan 17 '20 at 19:19

7 Answers7

20

On terminals that support setting the scrolling region:

tailf() ( # args: <file> [<number-of-header-lines>]
  trap 'tput csr 0 "$((LINES-1))"' INT
  tput csr "$((1+${2-1}))" "$((LINES-1))"
  tput clear
  {
    head -n"${2-1}"
    printf "%${COLUMNS}s\n" "" | tr ' ' =
    tail -n "$((LINES-1-${2-1}))" -f
  } < "$1"
)

(assumes a shell like zsh or bash that sets the $COLUMNS and $LINES variables based on the size of the terminal).

  • is it possible to piggyback on less itself without using head and tail to redraw the screen each time? I still <3 your solution btw. I am not marking it correct yet because you seem to be editing it and I don't want you to get complacent; complacency is a creativity-killer, I am told! :) – Debanjan Basu Aug 06 '14 at 16:47
  • @DebanjanBasu you can use less -X in place of tail -n ... above but the "up" key deletes the header there. – Stéphane Chazelas Aug 06 '14 at 16:49
  • Like I said, if you have any better ideas just point me to that, or better still please tweak your answer further. I had to mark it correct because you have answered the question that I initially asked. The idea is to have a less environment sandwiched between a header and a footer, to make it a more general tool. – Debanjan Basu Aug 06 '14 at 17:03
  • @DebanjanBasu, you may want to wait a couple of days before accepting the answer to encourage people to post alternatives. – Stéphane Chazelas Aug 06 '14 at 17:14