24

I'm writing a script which shows the git log for a directory when I cd into it.

Such a log can be overwhelming, containing hundreds of lines. So far I have been limiting that to a hard-coded 30 lines with (... | head -n 30), cool on the big screen at Work, too big at home. I would prefer the log to take up about half the (vertical) screen on any terminal, e.g. Gnome at work, iTerm2 at home. I do not use screen or tmux. But would like to learn them.

How do I find the number of vertical lines available in a terminal from command line?

0

2 Answers 2

35

Terminal parameters are stored as $LINES and $COLUMNS variables.

Additionally you can use special term-operation programm, for example tput:

tput lines # outputs the number of lines of the present terminal window. tput cols # outputs the number of columns of the present terminal window. 
6
  • 11
    LINES and COLUMNS are only set by some shells. bash sets them, but only for an interactive shell (and it does not export them). Commented Feb 10, 2015 at 18:00
  • 1
    The environment vars don't exist in my tcsh. Also, I wonder if the system would reset them if you resize the terminal window. Both tput and stty do work for me, so I learned something new :-) Commented Feb 10, 2015 at 18:40
  • 1
    @jamesqf bash documentation says "Automatically set upon receipt of a SIGWINCH." so, yes, it does change them if you resize the terminal window. Commented Feb 11, 2015 at 3:30
  • For autoset above vaiable the following options is responsible shopt -s checkwinsize which added in my /etc/bash.bashrc file with comment: # check the window size after each command and, if necessary, update the values of LINES and COLUMNS. Commented Feb 11, 2015 at 12:20
  • That command returns a number but I can zoom into the terminal (ctrl - shfit - +) and the number doesn't change. But number of lines visible on the terminal definitely does. I'm using linux mint, xfce, bash. Commented Oct 3, 2021 at 17:50
11

This command should give you the number of lines on the terminal:

stty size | cut '-d ' -f1 

Some systems might not implement stty size so you might need something like this instead:

stty -a | tr \; \\012 | grep rows | tr -d ' rows' 
5
  • 1
    This seems very OS-specific. Can you compile the list of systems where each incantation works? Commented Feb 10, 2015 at 13:52
  • 4
    I was afraid that the first one might be OS specific, which is why I added the second one. Indeed after a little bit of Googling I see that the first one probably won't work on Solaris. The second one should be very portable. Honestly, @Costas' tput lines seems like the better answer and I have upvoted it. I didn't know about it before. Commented Feb 10, 2015 at 13:57
  • 1
    I'd like to offer substitute long pipe with stty -a by something like grep -Po 'rows \K[^;]*' or sed -n 's/.*rows \([^;]*\).*/\1/p' Commented Feb 10, 2015 at 18:48
  • 1
    @Costas actually, frustratingly, neither my command nor either one of yours worked on MacOS, so I had to come up with yet a different command (edited). Go figure: MacOS emits "24 rows" instead of "rows 24"! I still think tput lines is way better, and I think it's pretty portable! Commented Feb 11, 2015 at 3:28
  • Second solution doesn't work on my system. First line gives me a number but I can zoom into the terminal (ctrl - shfit - +) and the number doesn't change. But number of lines visible on the terminal definitely does. I'm using linux mint, xfce, bash. Commented Oct 3, 2021 at 17:48

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.