Hack and / - Lightning Hacks Strike Twice

Are four hacks better than one? This month, check out four short Linux tips that will save you time and effort (or just rotate your X display like a top).
Rotate Your Screen Around and Around

My laptop doubles as a tablet, and even though I don't use the tablet mode very often, when I do use it, I like to be able to rotate the screen around to portrait mode and back. Now, dock applications exist that can do this with a few clicks, and I always could just try to remember the right xrandr commands, but instead, I wrote a little script that I then bound to one of the hardware buttons on my laptop display. Each time I press the button, it runs the script and rotates the screen another 90 degrees.

The key to the script is to keep track of your current orientation. When xrandr rotates, it rotates only left, right, inverted or normal, so if you already are rotated to the left and rotate left again, it won't change. To accomplish this, I just write the current orientation to a temporary file. Listing 1 shows the full script.

Notice in Listing 1 that I also added an echo piped to osd_cat. This is optional and just displays the current orientation on my screen. If you want to use this, be sure you have the osd_cat utility (it's included with the xosd-bin package in Debian and Ubuntu). The way the script is set up, it will run through each of the orientations in order before it goes back to normal. Because the temporary file will be deleted any time the machine reboots, I made sure to set the default mode to rotate 90 degrees.

Kyle Rankin is a Senior Systems Administrator in the San Francisco Bay Area and the author of a number of books, including Knoppix Hacks and Ubuntu Hacks for O'Reilly Media. He is currently the president of the North Bay Linux Users' Group.


Kyle Rankin is a director of engineering operations in the San Francisco Bay Area, the author of a number of books including DevOps Troubleshooting and The Official Ubuntu Server Book, and is a columnist for Linux Journal.


Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

dual head ?

marco69v's picture

I know that in ubuntu is now allowed to enable dualhead monitor and that probably it is done with xrandr. What about it on other distro ? on fedora there is something related in desktop configuration but it seem to not work

Help Stomp out Control-C programming

schelcj's picture

two things about this article; first use ssh-copy-id to setup authorized keys on remote hosts.
second, the copy-pasted code in your orientation script drove me nuts so i rewrote it.



function set_orientation() {
  local orientation="$1"

  if [ "$orientation" -ne "270" ]; then
    xrandr --auto

  xrandr --output LVDS --rotate $orientation

  record_orienation "$orientation"

function record_orientation() {
  echo "$1" > $ORIENTATION_FILE

function get_orientation() {
  echo "$(cat $ORIENTATION_FILE)"

function set_osd() {
  local message="$1"

  osd_cat --shadow=2 --align=center \
          --pos=bottom --color=green \
          --delay=2 --font=lucidasanstypewriter-bold-24 \
          --offset 40 < "$(cat $message)"

case "$(get_orientation)"; in
    set_orientation "180"
    set_osd "180"
    set_orientation "270"
    set_osd "270"
    set_orientation "0"
    set_osd "Normal"
    set_orientation "90"
    set_osd "90"

exit 0

Some problems here

zbeekman's picture

I believe you cannot call a function before defining it. i.e. record_orientation should be defined above set orientation.

I don't think you can pass '0' '90' '180' or '270' as arguments to xrandr

Also I think the xrandr syntax is outdated for ubuntu 9.04 users in both this rewrite and the original lightning hack.

Line 34: case statement: remove the ';' before 'in'

schelcj: Did you test this before posting it?


White Paper
Linux Management with Red Hat Satellite: Measuring Business Impact and ROI

Linux has become a key foundation for supporting today's rapidly growing IT environments. Linux is being used to deploy business applications and databases, trading on its reputation as a low-cost operating environment. For many IT organizations, Linux is a mainstay for deploying Web servers and has evolved from handling basic file, print, and utility workloads to running mission-critical applications and databases, physically, virtually, and in the cloud. As Linux grows in importance in terms of value to the business, managing Linux environments to high standards of service quality — availability, security, and performance — becomes an essential requirement for business success.

Learn More

Sponsored by Red Hat

White Paper
Private PaaS for the Agile Enterprise

If you already use virtualized infrastructure, you are well on your way to leveraging the power of the cloud. Virtualization offers the promise of limitless resources, but how do you manage that scalability when your DevOps team doesn’t scale? In today’s hypercompetitive markets, fast results can make a difference between leading the pack vs. obsolescence. Organizations need more benefits from cloud computing than just raw resources. They need agility, flexibility, convenience, ROI, and control.

Stackato private Platform-as-a-Service technology from ActiveState extends your private cloud infrastructure by creating a private PaaS to provide on-demand availability, flexibility, control, and ultimately, faster time-to-market for your enterprise.

Learn More

Sponsored by ActiveState