Diamond Notes

Just another WordPress weblog

Paginating Your Results

Many times your results are longer than your screen.  You end up scrolling up the terminal screen.  It can be a pain.  One way to solve this problem was shown to me some months ago in a session and it kind of slid right by me (maybe I should have scrolled up the screen :))

I just came across it again and thought I would document it for both me and anyone who hasn’t heard of this. It is quite useful.  All you have to do is this:

mysql> \P less
PAGER set to ‘less’

Then, when your results hit the bottom of the screen, you will get a colon prompt and it will wait for you to hit any key.  Once you hit the end the prompt changes to (END).  Hitting the ‘q’ key will return you to the normal mysql> prompt.  It can be very useful.  Enjoy!!

6 Comments so far

  1. ryan February 25th, 2008 11:34 am

    Thank you, that’s wonderful!

  2. joe February 25th, 2008 2:50 pm

    is there a way to set it so that I don’t have to type “\P less” at the start of every mysql session? (Is there something like a .mysql conf file?)

    Thanks!

  3. admin February 25th, 2008 3:16 pm

    Not that I am aware of. However, fyi, there is a .my.cnf file that you can create in your home directory for things such as username and password.

  4. Joe Izenman February 25th, 2008 5:23 pm

    And in case you want to switch back and forth, “\P stdout” will set the output back to normal. Simply \P will work as well, since stdout is the default.

    Looking at the documentation, there’s actually a lot of interesting stuff one can do. Since you are essentially passing it unix commands (like \P cat) you can add in command line options and output redirects. For example “\P cat > output.txt” sends all the output to a text file (in the directory you were in when you started mysql).

    Full documentation is here: http://dev.mysql.com/doc/refman/5.0/en/mysql-commands.html

  5. Jan Kneschke February 26th, 2008 4:17 am

    Put this in your ~/.my.cnf

    [mysql]
    pager=less

  6. admin February 26th, 2008 7:14 am

    Perfect Jan. Thanks.

Leave a reply