Things on this page are fragmentary and immature notes/thoughts of the author. Please read with your own judgement!
Seeking Help
The best way to seek for help is surely to use a search engine. However, there are serveral terminal command that can help you quickly.
-
whatis
briefly describes what a command does. For example,$ whatis man man (7) - macros to format man pages man (1) - an interface to the on-line reference manuals
-
whereis
locate the binary, source, and manual page files for a command. -
apropos
searches description of commands and find these associated with the job you specify. For example,$ apropos gzip gzip (1) - compress or expand files zforce (1) - force a '.gz' extension on all gzip files
-
man
display help documentation of commands. Note that you display the description ofman
itself byman man
When display contents of a file in terminal, you can use
/
to search for strings that match the one you specify. When searching, you can usen/N
to jump to the next/previous match.
Retrieving History Commands
-
!!
or!:-1
stands for the last command.!!
runs last command andsudo !!
runs the last command withsudo
permission. -
!n
(where \(n\) is a natural number) stands for the \(n^{th}\) command in the command history. -
!-n
stands for the last but \(n-1\) command in the command history, -
!foo
stands for the most recent command starting withfoo
.
Manipulating Commands
-
You can use the suffix
:-
to get rid of the last parameter of the retrieved command. For example,!!:-
stands for the last command without the last parameter. -
^foo^bar^
stands for the last command with the first occurence offoo
replaced bybar
. The last^
can be omitted if no more manipulation on the command is neede. (Note that this trick only works for the last command.) -
You can use the suffix
:s/foo/bar/
to replace the first occurrence offoo
withbar
in a retrieved command. For example,!!:s/foo/bar/
stands for the last command with the first occurrence offoo
replaced bybar
. The last/
can be omitted if no more manipulation on the command is needed. -
You can use the suffix
:gs/foo/bar/
to replace all occurrences offoo
withbar
in a retrieved command. For example,!!:gs/foo/bar/
stands for the last command with all occurences offoo
replaced bybar
. The last/
can be omitted if no more manipulation on the command is needed. -
You can add vanilla strings before and/or after a retrieved command. For example,
-
sudo !foo
runs the most recent command starting withfoo
withsudo
permissions. -
!n:gs/foo/bar/ | less
runs the \(n^{th}\) command in the history with all occurrences offoo
replaced bybar
and displays the result inless
mode. -
sudo ^foo^bar^ | less
orsudo !!:s/foo/bar/ | less
runs last command withsudo
permissions and displays the results inless
mode.
-
Previeawing Commands
You can preview (rather than run) a retrived and manipulated (using string substitution) command
by adding the suffix :p
to the command.
In the above commands,
if you add suffix :p
(e.g, !!:p
),
then the corresponding command is printed to the console without being executed.
Notice that the previewed command will be added to the command history,
so you can press "Arrow Up" key to retrieve it.
^foo^bar^:p
!n:gs/foo/bar/:p
Retrieve Parameter of Commands
-
!$
stands for the last parameter of last command. -
$_
stands for the last parameter of last command. -
!:0
stands for the Linux command in the last command. -
!:3-5
stands for the third to fifth parameters of the last command. If the first argument index is 0, then it can be omitted. For example,!:0-2
can be simplified as!:-2
. If the "TO" argument index is omited, then the last argument is not included. For example,!:3-
means the third argument to the last-but-1 argument. Specially,!:-
stands for last command without the last argument. -
<ESC> .
orALT + .
retrieves the last parameter of any previous command. Pressed once, it retrives the last parameter of the last command; pressed twice, it retrives the last parameter of the 2nd last command, and so on and so forth.
Retrieve Output of Commands
There is no direct way to retrieve the output of the last command. However, there are several indirect ways to do this.
-
Use substitution. For example,
v=`ls`
makesv
a variable containing the result of last command. -
Assign result to a variable. For example,
v=$(ls)
makesv
a variable contining the result of last command. -
Use
$(!!)
to retrieve the result of last command. Similarly, one can use$(!-2)
to retrieve the result of the 2nd last command.
Terminal
- CTRL+X E editor command in an editor
See useful shortcuts at Arch Wiki