DarkMatter in Cyberspace
  • Home
  • Categories
  • Tags
  • Archives

Command Line Shortcuts in Linux Shell


Shortcuts in bash

  • C-d: exit from the shell or a command (like ftp);

  • C-c: cancel current command;

  • C-p or M-p: previous command, same as up arrow key;

  • C-n or M-n: next command, same as down arrow key;

  • C-r: allows you to search through the previous commands;

  • C-o: Execute the current item in the history list and advance to the next one. Useful when you are trying to re-execute a sequence of commands in the history list. For example, run "ps", "pwd", "ls" one by one, then use C-p back to "ps", then press C-o, you can see the effect;

  • M-f/b: Move cursor forward/backward one word on the current line;

  • !!: last command, scenario: use "sudo !!" after "vi /etc/passwd" for permission denied;

  • !: Repeat last history list item starting with string, for example: "!ssh" will be expanded to the last ssh command you used, which is very useful when use a set of commands repeatedly;

  • !?: Repeat last history list item containing string. For example: "!?49" could be expanded to "ssh 10.0.2.49";

  • !: is the number of the result of "history" command, for example:

    history|grep scp|sort|uniq

list all history command contains "scp", sort them and remove duplicate command. Then use ! to run this command;

  • !\(: arguments of last command, scenario: "mkdir a/b/c/d/e", then use "cd !\)" to go to subfolder e;

  • ^old^new: replace "old" with "new" in last command;

  • history|grep erlang; !2217: use history to get the command number, use "!" to run this command;

Ref:

  • Chapter 8: Advanced Keyboard Tricks in "The Linux Command Line" by William E. Shotts;

  • http://lifehacker.com/5743814/become-a-command-line-ninja-with-these-time+saving-shortcuts

Command Line Editing

Move Cursor

  • M-f or C-right arrow key: Move forward one word;

  • M-b or C-left arrow key: Move backward one word;

  • C-a: Move to the beginning of the line;

  • C-e: Move to the end of the line;

  • C-f: Move forward a character, same as right arrow key;

  • C-b: Move backward a character, same as left arrow key;

  • C-l: same as command "clear";

  • C-t: exchange the character at the cursor location with the one preceding it;

  • M-t: exchange the word at the cursor location with the one preceding it;

Modifying Text

  • C-u: clears the current command line;

  • M-d: kill text from cursor to the end of the current word;

  • M-Backspace or C-w: kill text from cursor to the beginning of the current word, or the previous word if cursor is at the beginning of a word;

  • C-y: Yank text from the kill-ring and insert it at the cursor location;

  • C-k: deletes the line from the position of the cursor to the end of the line;

A use case: I want to run "scp file1 user@10.0.2.47:/home/user/warez/" and "scp file1 user@10.0.2.51:/home/user/warez/" one by one, after the first command is executed, I can run the 2nd in this way:

C-p M-b M-b M-b C-w 51:/<Enter>

where M-b move the cursor one word backward, C-w delete the word before cursor.

Completion

Beside the path name completion, you can use "Tab" key to complete:

  • Host name: after a "@" sign, the available hosts are defined in /etc/hosts;

  • Variable name: after a dollar sign "$";

  • User name: after a tilde sign "~";

Define alias in zsh

In this case we want to build a subversion client command-line utility, especially for browse purpose.

alias sl="svn ls"
alias -g 59="http://10.0.2.59:8118/svn/gcp"

Now "sl gcp" equals to "svn sl http://10.0.2.59:8118/svn/gcp".

The restriction of zsh alias is that there must be a blank between aliases, regular or global. So if you want to define a shortcut "sshec" for saving keyboard input of "ssh -i /home/lichao/Documents/lichao-key-pair-us-west-1.pem ec2-user@10.0.2.33", you can't define "alias sshec='ssh -i /home/lichao/Documents/lichao-key-pair-us-west-1.pem ec2-user@'", because there is no blank between "@" and "10.0.2.33". When you input "sshec10.0.2.33", you get a unknown command error. You can use [Application Finder -> Custom Actions] in Mint to define this kind of shortcuts.

Ref: zsh aliases



Published

Sep 17, 2013

Last Updated

Sep 17, 2013

Category

Tech

Tags

  • command line 14
  • linux 158
  • shell 46

Contact

  • Powered by Pelican. Theme: Elegant by Talha Mansoor