To create a keyboard shortcut for putting your laptop to sleep/suspend mode with lockscreen on return: Copy the below script into a file
#!/bin/bash gnome-screensaver-command --lock && dbus-send --system --print-reply \ --dest="org.freedesktop.UPower" \ /org/freedesktop/UPower \ org.freedesktop.UPower.Suspend exit 0
Before naming the file, check if a command of that name doesn’t already exist. you can do this by the which command
Name the file to the command you wanna use. I use compsleep.
With the terminal, reach the file, and convert it into an executable:
chmod +x compsleep
now copy this file into the bin folder, as done at the end of this post.
Check the command in the terminal by writing the filename and pressing enter. If this works, proceed to next step.
system settings >> (Keyboard Shortcuts)/(Keyboard>>shortcuts)>>+ or add new shortcut.
Enter name of file we created above in the command text box. Set a shortcut for it.
[Update:] custom shortcuts with Alt don’t work from 12.04 onward. It’s a bug.
[Update:2 (August 2016)] The above mentioned command doesn’t work on the newer systems. A simple replacement to that is this:
#!/bin/bash systemctl suspend exit 0