Keyboard shortcut
Result
Ctrl
+
a
Moves the cursor to the beginning of the line
Ctrl
+
e
Moves the cursor to the end of the line
Ctrl
+
l
Clears the screen
Ctrl
+
k
Deletes characters from the cursor to the end of the line
Ctrl
+
u
Deletes everything you've typed on that line (also works to clear
text while entering a password)
Chapter 6
[
145
]
Going a bit further into the command history, we can also press
Ctrl
+
r
on the shell
to initiate a search. After pressing these keys, we can start typing a command, and
we'll get a preview of a command that matches what we're typing, which will be
narrowed down further as we type more characters of it. This is one of those things
that is hard for me to describe, and screenshots certainly don't help here, so go ahead
and just give it a shot. For example, press
Ctrl
+
r
and then start typing
sudo apt
. The
last time you used that command should appear, and you can press
Ctrl
+
r
again,
and again, and again to see additional examples of commands that you've typed in
the past that contain those characters. When you get efficient with this, it's actually
faster than the
history
command, but it takes a bit to get used to this.
Another fun trick is editing a command you've previously typed in a text editor. I
know this sounds strange, but hear me out. Assume you pressed the up arrow, you
have a very long command, and you just want to edit part of it without having to
execute the entire thing, perhaps a command like this:
sudo apt update && sudo apt install apache2
Let's assume you want to install
nginx
instead of
apache2
, but the rest of the
command is right. If you hold
Ctrl
and then press
x
followed by
e
, the command
will open in a text editor. There, you can change the command. Once you're done
making your changes, the command will execute once you save the file. Admittedly,
this is usually only useful when you have a very long command and you need to just
change part of it. It's also a little weird, but so are computers.
Did you notice the two
&
symbols in the previous command? This is another useful
trick; you can actually chain commands together. In the previous example command,
we're telling the shell to execute
sudo apt update
. Next, we're telling the shell to
then execute
sudo apt install apache2
. The double ampersand is known as the
logical
AND
operator, so the second command is run if the first was successful. If the
first command was successful, the second command will execute right after. Another
method to chain commands is this:
sudo apt update; sudo apt install apache2
The difference with the semicolon is that we're telling the shell to execute the second
command
regardless
of whether the first command was successful. You may then
be wondering, what constitutes success on the shell? An obvious answer to this
question might be "it's successful if there are no error messages." While that's true,
the shell utilizes exit codes to programmatically attribute success or failure. You
can see the exit code of a command by typing this immediately after the previous
command finishes:
echo $?
Boosting Your Command-line Efficiency
Do'stlaringiz bilan baham: |