Tags

, , , , , , , ,

Although this blog serves as my personal notebook, however, since I’m putting this on the internet, I should humbly cite the reference here first. Most of the tricks are from the above reference, arranged according to my need. These are mostly Mac OSX terminal specific line-editing commands, but  should work in any other terminal running bash, as well.

[NB: If you don’t have a META key, then ESC may be used to replace it: e.g., to get the same functionality as META+t, first hit ESC then the letter t. In my mac, I did not have a META key by default. However, I’ve just found a solution (Google is your friend!) — go to terminal preferences, then select the keyboard tab and check “Use option as meta key”]

Cursor movement

CTRL+a
Move cursor to beginning of the line.
CTRL+e
Move cursor to the end.
CTRL+f
Move forward one character. Identical to .
CTRL+b
Move backward one character. Identical to .
META+f
Move forward one word.
META+b
Move backward one word.
CTRL+x CTRL+x
Mark current location in line and jump to beginning of line or second mark if defined. Repeat to jump to between both marks.

Cutting

CTRL+k
Delete everything from under the cursor to the end of the line. (I think of this as killing the rest of my line.)
CTRL+u
Delete everything from under the cursor the beginning of the line.
CTRL+w
Delete from under the cursor to the beginning of the word.

CTRL+d

Forward delete.
CTRL+h
Backspace.

Pasting/Inserting/Undoing

CTRL+y
Paste the most previously-deleted string. Basically a sort of command-line editting “undo.”
CTRL+_
Incremental undo.
META+r
Undo all the changes
CTRL+v
Insert next character verbatim. This is how you escape control sequences. For instance, to literally insert
^[, press ESC.
CTRL+j
Carriage return. Identical to hitting the return key.
CTRL+m
Newline. Identical to return.

Swapping

CTRL+t
Transpose (swap) the two characters before the cursor with one another.
META+t
Transpose (swap) the two words before the cursor with one another.

Changing case

META+c
Capitalize word under cursor and move to next word.
META+u
Uppercase word under cursor and move to next word.
META+l
Lowercase word under cursor and move to next word.

Auto-completion

META+.
Insert last word from previous command after cursor.
TAB
Auto-completes file, folder, and program names.
META+?
List the possible completions
CTRL+x /
List the possible filename completions
META+/
Attempt filename completion
CTRL+x ~
List the possible variable completions
META+~
Attempt username completion
CTRL+x $
List the possible variable completions
META+$
Attempt variable completion
CTRL+x @
List the possible hostname completion
META+@
Attempt hostname completion
CTRL+x !
List the possible command completions
META+!
Attempt command completion
META+TAB
Attempt completion from previous commands in the history list
CTRL+p
Recall previous command executed. Identical to .

Various

CTRL+z
Stop the current process and send it to the background.
CTRL+c
Send an SIG_HUP to the current process.
CTRL+d
Send an end-of-file special character to the current process. Doing this at the command line is identical to closing your terminal window.
CTRL+l
Repaint screen.

Keyboard macros (link)

CTRL+x (
Start a macro
CTRL+x )
End macro
CTRL+x e
Execute last macro.

Using vi inbuilt
However, the sweetest of all is if you set the vim inbuilt (either in ~/.bashrc or in the command line), you can actually use the most of the functionalities of vi in the command line
set -o vi
It’s in the insert mode by default; in order to go to the command mode, hit ESC!
You may also want to add
bind -m vi-command -r 'v'
to your ~/.bashrc so that you are not taken to a blank file every time you hit v in the command mode.

You may add the following in ~/.inputrc so that vi is always accessible
set editing-mode vi

History. If you’re using vi inbuilt you may also want to know how to browse through history: that’s easy too — just press j (to go up) and k (to go down) in history in command mode, just like regular vi.

File/directory name completion. Type the beginning characters of the file, hit escape followed by \ (backslash). However, the backslash will take you to insert the next character if it cannot uniquely identify the file.

Here's the link for the commandline vi completion tricks.

NB: TAB-completion works fine in the insert mode.

Advertisements