Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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).

share|improve this question
    
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 6 at 16:10
2  
@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 6 at 16:12
    
@polym ^ whatever he said! –  Debanjan Basu Aug 6 at 16:36

3 Answers 3

up vote 5 down vote accepted

If you're familiar with vim, this is probably the best option for you. You can enable horizontal scroll bind by adding it to 'scrollopt':

set scrollopt+=hor

So with vim -u NONE, you get the desired behavior with:

:set scrollopt+=hor
:set nowrap
:1split
:windo set scrollbind

You may want to adjust 'sidescroll' and 'sidescrolloff' to change how many columns are skipped and how far from the edge skipping starts respectively.

share|improve this answer

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).

share|improve this answer
    
Dude! That is godly! Sorry, couldn't control myself. Could you direct me to webpages or commands or variables to implement the following functions? –  Debanjan Basu Aug 6 at 16:42
    
1. exit with q 2. scroll down with up and down keys –  Debanjan Basu Aug 6 at 16:43
    
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 6 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 6 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 6 at 17:03

Try this (you'll need to install multitail):

multitail -du -t "$(head -n 1 filename)" filename

or, for headers longer than one line:

multitail -wh 2 -l "head -n 2 filename" filename

If you want to follow command output instead of a file:

multitail -wh 2 -l "command | head -n 2" -l command

or use -t as appropriate. Note that you may need to use unbuffer so your command output appears immediately.

You can use -D to disable the display of status lines for the -wh forms (it would defeat the purpose of the -t form).

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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