Alias HEAD for automatic smart output

alias head='head -n $((${LINES:-`tput lines 2>/dev/null||echo -n 12`} - 2))'
Run the alias command, then issue ps aux | head and resize your terminal window (putty/console/hyperterm/xterm/etc) then issue the same command and you'll understand. ${LINES:-`tput lines 2>/dev/null||echo -n 12`} Insructs the shell that if LINES is not set or null to use the output from `tput lines` ( ncurses based terminal access ) to get the number of lines in your terminal. But furthermore, in case that doesn't work either, it will default to using the deafault of 12 (-2 = 10). The default for HEAD is to output the first 10 lines, this alias changes the default to output the first x lines instead, where x is the number of lines currently displayed on your terminal - 2. The -2 is there so that the top line displayed is the command you ran that used HEAD, ie the prompt. Depending on whether your PS1 and/or PROMPT_COMMAND output more than 1 line (mine is 3) you will want to increase from -2. So with my prompt being the following, I need -7, or - 5 if I only want to display the commandline at the top. ( http://www.askapache.com/linux-unix/bash-power-prompt.html ) 275MB/748MB [7995:7993 - 0:186] 06:26:49 Thu Apr 08 [askapache@n1-backbone5:/dev/pts/0 +1] ~ In most shells the LINES variable is created automatically at login and updated when the terminal is resized (28 linux, 23/20 others for SIGWINCH) to contain the number of vertical lines that can fit in your terminal window. Because the alias doesn't hard-code the current LINES but relys on the $LINES variable, this is a dynamic alias that will always work on a tty device.
Sample Output
[7995:7993 - 0:2497] [askapache@n1-backbone5:pts/0 +1] ~ $ lsof -a -d 1 -d 0-4 -w|head
COMMAND   PID USER   FD   TYPE DEVICE  SIZE       NODE NAME
bash     7995 askapache    0u   CHR  136,0                2 /dev/pts/0
bash     7995 askapache    1u   CHR  136,0                2 /dev/pts/0
bash     7995 askapache    2u   CHR  136,0                2 /dev/pts/0
[7995:7993 - 0:2497] [askapache@n1-backbone5:pts/0 +1] ~ $

[7995:7993 - 0:2497] [askapache@n1-backbone5:pts/0 +1] ~ $ lsof -a -d 1 -d 0-4 -w|head
COMMAND   PID USER   FD   TYPE DEVICE  SIZE       NODE NAME
bash     7995 askapache    0u   CHR  136,0                2 /dev/pts/0
bash     7995 askapache    1u   CHR  136,0                2 /dev/pts/0
bash     7995 askapache    2u   CHR  136,0                2 /dev/pts/0
bash     9305 askapache    0u   CHR  136,2                4 /dev/pts/2
bash     9305 askapache    1u   CHR  136,2                4 /dev/pts/2
bash     9305 askapache    2u   CHR  136,2                4 /dev/pts/2
lsof    17475 askapache    0u   CHR  136,0                2 /dev/pts/0
lsof    17475 askapache    1w  FIFO    0,6       1056477120 pipe
lsof    17475 askapache    2u   CHR  136,0                2 /dev/pts/0
lsof    17475 askapache    3r   DIR   0,96     0          1 /proc
lsof    17475 askapache    4r   DIR   0,96     0 1078132745 /proc/17475/fd
[7995:7993 - 0:2497] [askapache@n1-backbone5:pts/0 +1] ~ $

These Might Interest You

  • Run the alias command, then issue ps aux | tail and resize your terminal window (putty/console/hyperterm/xterm/etc) then issue the same command and you'll understand. ${LINES:-`tput lines 2>/dev/null||echo -n 12`} Insructs the shell that if LINES is not set or null to use the output from `tput lines` ( ncurses based terminal access ) to get the number of lines in your terminal. But furthermore, in case that doesn't work either, it will default to using the default of 80. The default for TAIL is to output the last 10 lines, this alias changes the default to output the last x lines instead, where x is the number of lines currently displayed on your terminal - 7. The -7 is there so that the top line displayed is the command you ran that used TAIL, ie the prompt. Depending on whether your PS1 and/or PROMPT_COMMAND output more than 1 line (mine is 3) you will want to increase from -2. So with my prompt being the following, I need -7, or - 5 if I only want to display the commandline at the top. ( http://www.askapache.com/linux/bash-power-prompt.html ) 275MB/748MB [7995:7993 - 0:186] 06:26:49 Thu Apr 08 [askapache@n1-backbone5:/dev/pts/0 +1] ~ In most shells the LINES variable is created automatically at login and updated when the terminal is resized (28 linux, 23/20 others for SIGWINCH) to contain the number of vertical lines that can fit in your terminal window. Because the alias doesn't hard-code the current LINES but relys on the $LINES variable, this is a dynamic alias that will always work on a tty device. Show Sample Output


    2
    alias tail='tail -n $((${LINES:-`tput lines 2>/dev/null||echo -n 80`} - 7))'
    AskApache · 2012-03-22 02:44:11 2
  • tail() { thbin="/usr/bin/tail"; if [ "${1:0:1}" != "-" ]; then fc=$(($#==0?1:$#)); lpf="$((($LINES - 3 - 2 * $fc) / $fc))"; lpf="$(($lpf<1?2:$lpf))"; [ $fc -eq 1 ] && $thbin -n $lpf "$@" | /usr/bin/fold -w $COLUMNS | $thbin -n $lpf || $thbin -n $lpf "$@"; else $thbin "$@"; fi; unset lpf fc thbin; } This is a function that implements an improved version of tail. It tries to limit the number of lines so that the screen is filled completely. It works with pipes, single and multiple files. If you add different options to tail, they will overwrite the settings from the function. It doesn't work very well when too many files (with wrapped lines) are specified. Its optimised for my three-line prompt. It also works for head. Just s/tail/head/g Don't set 'thbin="tail"', this might lead to a forkbomb.


    -1
    tail() { thbin="/usr/bin/tail"; if [ "${1:0:1}" != "-" ]; then fc=$(($#==0?1:$#)); lpf="$((($LINES - 3 - 2 * $fc) / $fc))"; lpf="$(($lpf<1?2:$lpf))"; [ $fc -eq 1 ] && $thbin -n $lpf "$@" | /usr/bin/fold -w $COLUMNS | $thbin -n $lpf || $thbin -n $lpf...
    fpunktk · 2012-03-23 19:00:30 0
  • In Bash, when defining an alias, one usually loses the completion related to the function used in that alias (that completion is usually defined in /etc/bash_completion using the complete builtin). It's easy to reuse the work done for that completion in order to have smart completion for our alias. That's what is done by this command line (that's only an example but it may be very easy to reuse). Note 1 : You can use given command line in a loop "for old in apt-get apt-cache" if you want to define aliases like that for many commands. Note 2 : You can put the output of the command directly in your .bashrc file (after the ". /etc/bash_completion") to always have the alias and its completion Show Sample Output


    4
    old='apt-get'; new="su-${old}"; command="sudo ${old}"; alias "${new}=${command}"; $( complete | sed -n "s/${old}$/${new}/p" ); alias ${new}; complete -p ${new}
    Josay · 2009-08-10 00:15:05 0
  • Print a git log (in reverse order) giving a reference relative to HEAD. HEAD (the current revision) can also be referred to as HEAD~0 The previous revision is HEAD~1 then HEAD~2 etc. . Add line numbers to the git output, starting at zero: ... | nl -v0 | ... . Insert the string 'HEAD~' before the number using sed: ... | sed 's/^ \+/&HEAD~/' . Thanks to bartonski for the idea :-) Show Sample Output


    1
    git log --oneline | nl -v0 | sed 's/^ \+/&HEAD~/'
    flatcap · 2015-11-23 21:35:57 0

What Others Think

This is great. Also the -n option still works.
houghi · 427 weeks and 5 days ago
You could also use this with "tail".
jmcantrell · 427 weeks and 4 days ago
In some cases, LINES might not be set so it's a good idea to provide a default. This will make it the same as the non-alias version: alias head='head -n $((${LINES:-12}-2))'
dennisw · 427 weeks and 3 days ago
Also, for some reason even with just a one-line prompt, I need -4 for head and -5 for tail in order to leave the previous prompt at the top of the screen.
dennisw · 427 weeks and 3 days ago
@dennisw Are you head-ing something that is wrapping lines?
vgm64 · 427 weeks and 2 days ago
Bingo! +10 internets for you, -40 for me! (I'll return now to my tiddlywinks.)
dennisw · 427 weeks and 2 days ago
Thats a great idea dennisw, I've since updated the code to reflect your 'default reversion' tip.. plugs 1 more attempt to find the lines using tput first... Yes this works exactly the same way for tail, I loves it!
AskApache · 427 weeks and 1 day ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands



Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: