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.
-
whatisbriefly 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 -
whereislocate the binary, source, and manual page files for a command. -
apropossearches 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 -
mandisplay help documentation of commands. Note that you display the description ofmanitself byman manWhen 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/Nto jump to the next/previous match.
Retrieving History Commands
It is suggested that you search shell command history using fzf.
For more discussions,
please refer to
Editing Shell Commands Using Vim
.
-
!!or!:-1stands for the last command.!!runs last command andsudo !!runs the last command withsudopermission. -
!n(where \(n\) is a natural number) stands for the \(n^{th}\) command in the command history. -
!-nstands for the last but \(n-1\) command in the command history, -
!foostands for the most recent command starting withfoo. -
historyprints out all historical commands (with numbers). -
history -s commandsaves the specified command to the in-memory history. -
You can also retrieving history shell commands using the built-in command
fc. Please refer to Fix Shell Command Using Fc for more discussion.
Manipulating Commands
It is suggested that you use ctrl+x ctrl+e or fzf.history
to edit shell commands.
For more discussions,
please refer to
Editing Shell Commands Using Vim
.
-
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 offooreplaced 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 offoowithbarin a retrieved command. For example,!!:s/foo/bar/stands for the last command with the first occurrence offooreplaced 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 offoowithbarin a retrieved command. For example,!!:gs/foo/bar/stands for the last command with all occurences offooreplaced 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 !fooruns the most recent command starting withfoowithsudopermissions. -
!n:gs/foo/bar/ | lessruns the \(n^{th}\) command in the history with all occurrences offooreplaced bybarand displays the result inlessmode. -
sudo ^foo^bar^ | lessorsudo !!:s/foo/bar/ | lessruns last command withsudopermissions and displays the results inlessmode.
-
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
It is suggested that you use ctrl+x ctrl+e or fzf.history
to edit shell commands.
For more discussions,
please refer to
Editing Shell Commands Using Vim
.
-
!$stands for the last parameter of last command. -
$_stands for the last parameter of last command. -
!:0stands for the Linux command in the last command. -
!:3-5stands 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-2can 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`makesva variable containing the result of last command. -
Assign result to a variable. For example,
v=$(ls)makesva 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.