Do you want to copy and paste on the Bash shell command line? We will show you several techniques, whether you prefer the keyboard or the mouse. These work whether you are on a graphical desktop or on a traditional text-based teleprinter.
The usual keyboard shortcuts do not work
Copying and pasting text is an essential part of using a computer. When users first use a Linux computer, whether they come from the Windows world or the MacOS world, they are often confused when they try to copy and paste in a terminal window.
On Windows, you use Ctrl + C to copy a highlighted section of text and Ctrl + V to paste it. In macOS, use Command + C to copy it and Command + V to paste it. They follow the same convention as C to copy and V to insert.
These same keys work in most Linux graphics applications, such as the editor gedit or the LibreOffice office suite.Ctrl + C copies the highlighted text and Ctrl + V pastes it to the cursor. Our newcomer to Linux probably does not even think that these conventions have been applied to these applications. They use muscle memory strikes and continue their work.
Once our newcomer opens a terminal window and tries to copy and paste to the Shell bash Command prompt, that all changes. Ctrl + C and Ctrl + V were allocated functions long before one thought of copying / pasting. In fact, these strikes were recruited long before the invention of graphic shells, at the time when teletypewriter (TTY) was a physical thing.
Ctrl + C and Ctrl + V in ATS
When a teletypewriter was a physical device, Ctrl + C was chosen as a convenient key combination to generate a signal. This signal is SIGINT, which tells the current process to terminate. Since a terminal window is an emulated ATS, this key combination (and many others) has been preserved and replicated in emulation. Note that this is the terminal window that is emulating. The Bash shell is a program that runs in this emulated TTY.
We can easily see the functions that have been assigned to Ctrl + C and Ctrl + V. Suppose you type the following command and press Enter.
ls -R /
As we use the -R (recursive) option, the ls command will start listing all the files and directories from the root directory. After a few moments, you realize that it is not what you want, so you end the process by pressing Ctrl + C.
Ctrl + C
The process is over. The visible proof of Ctrl + C is highlighted in the screen capture. It is displayed as ^ C.
The Ctrl + V key combination calls "text insertion". This allows you to enter a representation of the key in what you type, instead of getting the effect of the key. To see this, try the following commands (do not type the commas). (For example, to try the first, press Ctrl + V, and then press Enter.)
Ctrl + V, Enter
Ctrl + V, PgDn
Ctrl + V, RightArrow
Ctrl + V, Esc
By the way, you may notice that Enter is represented by ^ M. We saw previously that Ctrl + C appeared under the name of ^ C. It would seem that ^ represents Ctrl. So, Ctrl + M probably means the same as Enter. Does this mean that we can enter by pressing Ctrl + M? Try it in a terminal window. You will see that is the case.
Clearly, we can not expect Ctrl + C and Ctrl + V to copy and paste text when they already have respected functions. So what can we use?
RELATED, RELATED, RELATED: How to be more productive in Ubuntu using keyboard shortcuts
Ctrl + Shift + C and Ctrl + Shift + V
Easily remembered because they are similar to their counterparts, Ctrl + Shift + C and Ctrl + Shift + V are direct replacements for Ctrl + C and Ctrl + V.
If you highlight text in the terminal window with your mouse and press Ctrl + Shift + C, you copy that text to a buffer on the clipboard.
Ctrl + Shift + C
You can use Ctrl + Shift + V to paste the copied text into the same terminal window or into another terminal window.
Ctrl + Shift + V
You can also paste in a graphics application such as gedit. However, note that when you paste in an application (not in a terminal window), you must use Ctrl + V.
And you can also go in the other direction. You can highlight text in gedit and press Ctrl + C, then paste it into a terminal window using Ctrl + Shift + V.
The Ctrl + Insert key combination is the same as Ctrl + Shift + C and the Shift + Insert combination is the same as Ctrl + Shift + V. The caveat is that these can only be used in the same terminal window .
Using the mouse: right click
You can use the mouse to copy and paste in a terminal window. You must use the mouse to highlight the text you are going to copy. Why not use it to perform copy-and-paste actions?
Once you have highlighted text, right-click and select "Copy" from the pop-up menu.
To paste the copied text, right-click again and select "Paste" from the context menu.
The text is pasted at the cursor position on the command line. In this example, the relative path has been incorrectly assigned and Bash can not change directories. The user has missed the "~ /" since the beginning of the path. They typed the "~ /" then copied the rest of the way from their previous attempt and pasted it into their second command line.
When they press Enter, they are moved to the directory.
This example shows the collage in the same terminal window, but you can use this right-click technique to paste into different terminal windows. You can also paste into graphics applications using this method.
Using the mouse: central button
There is an even faster way to copy and paste using the mouse, provided your mouse has a central button. If you press the scroll wheel (gently!) And you click on it, you get a central button.
Highlight text in a terminal window, then press the middle button. The highlighted text is pasted at the cursor position on the command line. Copy and paste takes place at the same time.
So, highlight text:
Then press your middle button:
You can use this method to paste between different terminal windows, as well as in graphics applications. Simply highlight the text, go to the previous terminal window or application and press the middle button.
Old School – No mouse
What about when you do not have a mouse? If you can not highlight text, how can you copy and paste it?
Linux servers are often configured without graphical desktop environment (GDE), which means that you do not have access to a mouse. Even on a Linux computer that runs a graphical desktop environment such as GNOME and you have a mouse, there will be cases where you will not be able to use the mouse to highlight text.
For example, you may have switched to one of the additional TTYs. On modern distributions, these are located between Ctrl + Alt + F3 (TTY3) and Ctrl + Alt + F6 (TTY6). (Ctrl + Alt + F2 will take you back to your GDE session and Ctrl + Alt + F1 will take you to your GDE login screen.)
Perhaps you even use a genuine ATS hardware device to connect to a Linux or Unix computer.
In either case, these techniques will work for you. What we must remember here is that it is not a question of copying and pasting, but of cutting, copying and pasting, and that you can only cut and copy and copy. From the current command line. Of course, you can use the arrow keys to browse your history to find the command line from which you want to cut, copy, and paste.
The keystrokes you can use are:
Ctrl + W: Cut the word before the cursor and add it to the clipboard buffer.
Ctrl + K: Cuts the part of the line after the cursor and adds it to the buffer of the clipboard. If the cursor is at the beginning of the line, it will cut and copy the entire line.
Ctrl + U: Cut the portion of the line before the cursor and add it to the clipboard buffer. If the cursor is at the end of the line, it will cut and copy the entire line.
Ctrl + Y: Pastes the last text cut and copied.
Now let's go to TTY3. (Use Ctrl + Alt + F1 to return to your desktop afterwards.)
Ctrl + Alt + F3
We have a file that we want to delete, check that it is here.
ls -l file_to_delete.txt
If we use the up arrow key, we will call the last command we used outside the command history. We could just edit this line, but the object is to show the cut, copy and paste it, so we will accomplish our task of deleting the file slightly longer.
We will move the cursor to the first letter of the file name, then press Ctrl + K. This will remove that part of the line and copy the text to the clipboard buffer.
We will press Backspace until we erase the line.
We will type in the rm to delete the file.
And now we can press Ctrl + Y and paste the rest of the line.
This completes our order and we can press Enter to delete the file.
This type of cut, copy and paste can not be used between additional TTYs. You can not cut, copy and paste between TTY3 and TTY4, for example.
It is better to think of additional ATS as a line of physical AYA juxtaposed. There is no way to cut and paste between different physical terminals, and there is no way to do it in these emulations.
Copy this, control
Whatever the situation you are in when you use a Linux computer, there is a way to copy and paste. You have options. Some of them are strange options, but at least there are some.