Linux tips and tricks
You probably know key combinations like Ctrl-C or Ctrl-Z. You'd be surprised to learn that there are quite a few more useful key combinations that you probably haven't heard of. Here are a few to keep in mind when working at the console (note: most of these should also work under Konsole or other GUI console applications):
-
Ctrl-C (usually) sends the kill signal, which terminates a process abruptly.
-
Ctrl-Z sends the stop signal, which stops process execution immediately, without terminating. Use “fg” or “bg” after that to restart the process of send it to the background. Ctrl-Y is an interesting variant, which stops process execution only when the process tries to read user input from the console.
-
Ctrl-S is the scroll lock: when you have too much output scrolling quickly, you can use Ctrl-S to stop process output. You can then use Shift-PageUp/PageDown to scroll up and down and read it. Ctrl-Q resumes. Technically speaking, Ctrl-S and Ctrl-Q send the XON/XOFF terminal commands, that may be ignored in some cases. The Bash shell translates Ctrl-S to “search”, so it depends on where you are working.
-
Ctrl-L clears the screen and positions the prompt at the top left corner. Use this if your screen has become garbled.
-
Ctrl-A and Ctrl-E take you to the beginning and the end of the line, respectively. Home and End also work, but not always.
-
Alt-F and Alt-B take you to the next or previous word, very useful if you're typing a long sequence of commands or a command with many arguments.
-
Tab is probably the most useful key when using the shell. Tab will do filename completion, but also hostname completion, variable name completion and other useful things.
There are a few key combinations that work under the X-windows server, regardless of whether you are under KDE or Gnome (or any other environment, for that matter):
-
Ctrl-Alt-F1/F2... : switch to the corresponding character virtual console. This is meaningful only if you are running under the appropriate init level, otherwise you won't find a login prompt. You will need Alt-F7 to get back to X-windows (this depends on the kernel settings for the number of virtual consoles, could also be Alt-F9 or Alt-F11, for example).
-
Ctrl-Alt-Backspace : Kill the X-server
-
Ctrl-Alt- + or - : Cycle between resolutions, according to the xf86config file. You need to have multiple resolutions defined in your config file for this to work.
You need to enable the magic SysRq key during compilation of the kernel. Some distributions enable it by default. Otherwise you can simply compile the kernel yourself (easier than it seems, trust me) after enabling it under “Kernel Hacking”.
Once you have the SysRq key enabled, the following keystrokes will work (more can be found in the kernel documentation) :
-
Alt-SysRq-S: emergency sync, tries to write all unsaved buffers
-
Alt-SysRq-U: emergency remount read only for all file systems
-
Alt-SysRq-E: send the terminate (15) signal to all processes except init
-
Alt-SysRq-I: send the kill (9) signal to all processes except init
-
Alt-SysRq-B: emergency IMMEDIATE reboot (unsafe, like pressing the reset button!)
-
Alt-SysRq-K: emergency kill of all programs under the virtual console
When the system crashes, emergency sync, remount and reboot are a convenient way to ensure a relatively safe system reboot. The following sequence best emulates a normal software reboot : Alt-SysRq-S, 5 sec pause, Alt-SysRq-E, 5 sec pause, Alt-SysRq-S, 5 sec pause, Alt-SysRq-I, Alt-SysRq-S, 5 sec pause, Alt-SysRq-U, Alt-SysRq-B (the pauses and multiple syncs are a bit excessive, but better safe than sorry). If a console crashes really bad, emergency kill may remedy the situation and get you back to a login prompt.
Note that these commands are immediate and potentially unsafe, so only use as a last resort before doing a real hardware reset or something equally drastic. Also, bear in mind that the magic sysrq key can be used as a denial-of-service attack by malicious hackers/crackers, so don't enable it in servers that are particularly vulnerable and exposed to such threats.
Quitting Vim/ViI remember playing with various linux commands and applications back in the days when I was beginning to use linux. One of the most annoying ways to get stuck was inside the Vi/Vim editor, that captures the Ctrl-C and Ctrl-Z keystrokes. Although I'm sure there are several readers who think that Vi is the best thing since sliced bread, most people should just know how to exit, in case they accidentally get trapped inside it. Simply type “:q!”, without the quotation marks, of course.
Conclusion
So many things, so little time. Although the examples given above may at first seem to be daunting, I think that every linux user should at least try to use them. After a while, these make life seem so much easier that it will be definitely worth it. That being said, if you only had to know ONE key, Tab would be it. Trust me on this...