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).
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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}sn" "" | 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).
Method 2
If you’re familiar with vim, this is probably the best option for you. You can enable horizontal-scroll-bind-only by changing '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.
Method 3
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).
Method 4
Thor’s answer didn’t mention disabling the vertical “scrollopt”, which makes both windows scroll vertically. So for me the complete solution is pasting this into vim:
:set scrollopt+=hor :set scrollopt-=ver :set nowrap :1split :windo set scrollbind
Method 5
This is as far as I got with tmux:
#!/bin/bash
tmux new-session -s main -n 'w1' -d
tmux send-keys -t main:w1.0 "tail -f <(head -n1 $1)" C-j
tmux split-window -v
tmux resize-pane -t 0 -y 2
tmux send-keys -t 1 "tail -n+2 $1|less -S" C-j
tmux attach -t main
It’s an extension of user80519’s answer for tmux window splitting. Save as hless.sh, make it executable and use it like
hless.sh file
Method 6
If it does not have to be a text-mode solution, you could use yad to display a list – actually a GTK+ dialog – with named columns. Maybe it has to read the whole file before you can browse, though.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0