A very common thing to do with GUI-based systems is cutting and pasting text. You'll select text, often with the mouse, and either use the right mouse button to copy and paste, or hopefully you've found the good old CTRL+c and CTRL+v (for Windows, Command key for macOS). As we explained before and reminded you of two paragraphs ago, CTRL+c under Linux is definitely not a copy, but a cancel. Similarly, CTRL+v will most likely not paste text either. So, how then, under Linux, do we copy and paste?
First of all, if you're using SSH and a Terminal emulator from within a GUI desktop, you have the right mouse button available to accomplish this (or, if you're feeling really fancy, pressing the middle mouse button often defaults to paste as well!). You can select text from somewhere on the internet, for example, copy it, and paste it into your Terminal emulator with either button. However, we always strive to optimize our processes, and as soon as you need to grab the mouse, you're losing valuable time. For text you have already copied, there is (for most Terminal emulators!) a shortcut to paste: SHIFT+insert. Just so you know, this paste shortcut is not limited to Linux or most Terminal emulators: it seems to be pretty universal, working on Windows and Linux with GUIs as well. Personally, we have replaced CTRL+v almost completely with SHIFT+insert for our pasting needs.
Obviously, if we can paste in this manner, there must also be a similar way for copying. This is very similar: instead of SHIFT+insert, copying can be done with CTRL+insert. Again, this is not limited to Linux or Terminals: it works just fine on Windows as well. For those of us working with Linux and Windows, replacing CTRL+c and CTRL+v with CTRL+insert and SHIFT+insert ensures that we are always properly copying and pasting, no matter what environment we're working in. Personally, we use Linux at home but Windows at work, which means our time is spent about 50/50 between the operating systems: trust us, it is very nice to have shortcuts that always work!
Now, the method above still sort of relies on having a mouse. Most of the time (think more than 95%, depending on your job) this will be the case, but sometimes you will simply not have a mouse (when connected directly to a Terminal of a server in a data center, for example). Fortunately for us, there are three shortcuts that work in Bash and will allow us to cut and paste directly on the command line:
- CTRL+w: Cut the word before the cursor
- CTRL+u: Cut everything on the line before the cursor
- CTRL+y: Paste everything that was cut (using the two commands above, not the general OS clipboard!)
Besides being able to cut and paste, CTRL+w is also great to remove a single whole word from the command line. Look at the following example:
reader@ubuntu:~$ sudo cat /etc/passwd # Wrong file, we meant /etc/shadow!
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
<SNIPPED>
# Up-arrow
reader@ubuntu:~$ sudo cat /etc/passwd
# CTRL+w
reader@ubuntu:~$ sudo cat # Ready to type /etc/shadow here.
Something that tends to happen is giving an incorrect final argument to a command. If you want to revise this real quickly, a simple up-arrow followed by a CTRL+w will place the previous command minus the final argument back on your Terminal. Now, you just have to give it the correct argument to run it again. Alternatively, you could either:
- Retype the whole command
- Scroll, copy and paste using the mouse
- Up-arrow followed by a number of backspaces
In our experience, a double keystroke is always faster than all other possible solutions. Only if the last argument was a single character would using up-arrow and backspace be equally fast, which is kind of a stretch.
Now, in the previous example, we did not actually only remove the final argument, we actually cut it. When you cut an argument, it gives you the ability to paste it back again. As stated, this is a Bash-specific clipboard which is not tied to the system clipboard; while you might think that paste is always done with SHIFT+insert, in this case we use CTRL+y for the Bash-specific clipboard. The best example to show this is with a full line cut, using CTRL+u:
root@ubuntu:~# systemctl restart network-online.target # Did not press ENTER yet.
# Forgot to edit a file before restart, CTRL+u to cut the whole line.
root@ubuntu:~# vim /etc/sysctl.conf # Make the change.
# CTRL+y: paste the cut line again.
root@ubuntu:~# systemctl restart network-online.target # Now we press ENTER.
This is, for us, a typical scenario in which we are one step ahead of ourselves. We have already typed a command we need to execute, but before we press ENTER we realize we forgot to do something that needs to be done before our current command can succeed. In this scenario, we use CTRL+u to cut the entire command, continue with the prerequisite command, and when we're ready we paste the line with CTRL+y again. Again, you might think this will not happen to you, but you might be surprised at how often you will encounter this precise pattern.