EDIT: The below blog is now available as screen recorded video in
Using Command Line Interface (CLI) in Linux is the real beauty when you know how to work faster. I wondered and admired myself in many places while exploring the beauty of ‘Ctrl‘ key. Below are the few of those places.
- Moving across: Ctrl + Side arrows , Ctrl + a and Ctrl + e
- When you typed something very long (e.g. may be a long gcc command) on the terminal/console and all on a sudden you want to edit what you have typed which is in the beginning. Initially i was using the ‘Home’ button which worked very well in Distributions like Ubuntu, but not in Target’s console and Solaris console. I used to move char by char using side arrow, which is again worked only in targets but not on Solaris.
- ‘Ctrl + Side Arrows‘ : One fine day while using side arrow to move around i wrongly pressed ‘Ctrl‘ key. Then i observed that navigation was very fast word by word instead of usual char by char i.e. moving space as delimiter, which provoked me to explore the wonder behind ‘Ctrl‘ key.
- ‘Ctrl + a‘ moved me to the beginning irrespective of my current typing position.
- ‘Ctrl + e‘ moved me to the end irrespective of my current typing position.
- Searching last run commands:
- When i work for the whole day on the same black and white screen, my history of commands will be flooded. Few commands will be repeated often. I use ‘up down arrows’ to quickly recall the commands. But that was time consuming when i ran through 20+ commands i.e. pressing up/down arrows for 20+ time.
- Yes, ‘history‘ command saves all the commands which i had run in the same session. I used pipe (e.g. history | grep -i <command>) to retrieve the commands and copy paste again on the console. I used ‘mouse‘ here to copy. Again this was time consuming.. 😦
- I found this useful way running the commands without using mouse. ‘history‘ command returns both the command and a numeric value (i.e. the number of commands from the beginning of the session). Using ‘!’ (exclamation) followed by the numeric value.
- If multiple commands deals with the same work in your history, there is best way to differentiate among those. Use ‘#‘ hash comments symbol followed by the command (e.g. cat /proc/pid/maps ; # Self maps). By grepping into the comments you will be able to find the exact command.
- Ubuntu like distribution provides another best approach ‘Ctrl + r‘ to search the CLI in reverse order.
- If the above friendly ‘Ctlr + r‘ is not available, you can use ‘!<command>’ to run ‘!<command>:p’ see the preview (e.g. !git:p previews the last run git command. You can use single up arrow to retrieve and run. !git simply runs the last run git command)
- Cleaning up:
- Similar to moving front and back of using side arrows, i was using ‘Backspace’ to delete the typed content in console. ‘Ctrl’ again helped me here.
- ‘Ctrl + u’ undo the whole typed stuff
- ‘Ctrl + w’ undo/delete a word (space as delimiter)
- There was always this friendly copy/paste needed. GUI editor and console in Ubuntu supports ‘Ctrl + c/v’ for copy/paste similar to Windows. But what about in putty, target or Solaris console?
- ‘Ctrl + Insert’: Copies whatever selected (either by Mouse or Cursor)
- ‘Shift + Insert’: Pastes the selected (i.e. copied content from clipboard.
- The beauty here is, this is Platform/OS independent approach for copy/paste even when ‘Ctrl + c/v’ failed to work.
- Suspending and Resuming jobs/process:
- Suspending the foreground process can be done using ‘Ctrl + z’. Suspending puts the process (either terminal or GUI based process) is unusable state.
- Resuming this process again in background ‘using bg command’ and foreground ‘using fg command’
- Suspend doesn’t mean the process is in running state. We have to move to running state either by fg or bg.
- ‘Ctrl + c’ or SIGINT:
- This triggers SIGINT signal and the process will be terminated by default.
- This default action can be changed by catching the signal using sigaction(2) or signal(2)
- Sometime we don’t want to end our process even after closing the console. This can be done using ‘nohup‘ command.
- Complex commands can also be executed using nohup e.g nuhup sh -c ‘complex command’
- We may be watching the console or looking/polling for completion of process. How to get notified when it’s over?
- ‘notify-send’ (Only in Ubuntu): Notifications can be triggered after the completion of your process. e.g bitbake build ; notify-send ‘Build is done’
Following the tips and completing the work faster may not come in One day or few. Yes, its hard for the human brain to follow new neural pathways always, that’s a different story.