Key Takeaways
Ctrl+C and Ctrl+V, which are commonly used to copy and paste in graphical applications, do not work in the Bash shell command prompt.
To copy and paste in the Bash shell command prompt, you can use either Ctrl+Shift+C and Ctrl+Shift+V or right-click with the mouse and choose "Copy" and "Paste" from the context menu. Additionally, you can effortlessly paste the selected text to the cursor's location by pressing the middle mouse button.
The Usual Keyboard Shortcuts to Copy and Paste Won't Work
Looking to perform copy and paste operations on the Bash shell command line? In this guide, we will present you with various techniques, suitable for both keyboard enthusiasts and mouse lovers. These methods can be used regardless of whether you are working on a graphical desktop or a text-based TTY environment.
Copying and pasting text is an essential activity when using a computer. However, when individuals, regardless of their previous experience with Windows or macOS, first start using a Linux computer, they often encounter difficulties when attempting to copy and paste within a terminal window.
In Windows, the process involves using Ctrl+C to copy a selected portion of text and Ctrl+V to paste it. In macOS, the equivalent commands are Command+C for copying and Command+V for pasting. Both operating systems adhere to the convention of using 'C' for copying and 'V' for inserting.
The same keystrokes mentioned above can be used in most Linux graphical applications like gedit or the LibreOffice office suite. For instance, pressing Ctrl+C will copy highlighted text and Ctrl+V will paste it at the cursor. Interestingly, new Linux users often don't realize that these conventions also apply to these applications, as they instinctively use these keystrokes from muscle memory and continue their work.
However, when new users open a terminal window and attempt to copy and paste at the Bash shell command prompt, things change. Ctrl+C and Ctrl+V have already been assigned specific functions long before the concept of copying and pasting was introduced. In fact, these keystrokes were initially adopted back in the time of physical teletypes (TTYs) before graphical shells even existed.
Ctrl+C and Ctrl+V in TTYs
When a TTY functioned as a physical device, the Ctrl+C key combination was designated as a convenient trigger to generate a signal. This signal, known as SIGINT, instructs the ongoing process to terminate. As the terminal window serves as an emulated TTY, it retains and duplicates this specific keystroke combination, along with numerous others. It is important to note that the emulation occurs within the terminal window itself, while the Bash shell is a program that operates within this emulated TTY.
We can easily see the functions that have been allocated to Ctrl+C and Ctrl+V. Suppose you type the following command and press "Enter."
ls -R /
By utilizing the -R (recursive) option, the ls command will initiate the listing of all files and directories, commencing from the root directory. However, after a brief period, it becomes apparent that this is not the desired outcome. Consequently, you terminate the process by pressing Ctrl+C.
Ctrl+C
The ls process has been terminated and the screenshot highlights the visible evidence of the Ctrl+C, displayed as ^C.
The "verbatim insert" function is invoked by using the Ctrl+V key combination. This enables you to input a representation of a key instead of executing its action. To observe this, experiment with the following commands (don't type the commas). For instance, for the first command, press Ctrl+V followed by Enter.
Ctrl+V, Enter
Ctrl+V, PgDn
Ctrl+V, RightArrow
Ctrl+V, Esc
As a side note, you might observe that Enter is denoted as ^M. We previously learned that Ctrl+C is displayed as ^C. It appears that ^ symbolizes Ctrl. Therefore, Ctrl+M likely represents the same as Enter. Can we input Enter by typing Ctrl+M? Test it out in a terminal window. You will witness that it indeed works.
So, plainly we can't expect Ctrl+C and Ctrl+V to perform copying and pasting text when they already have time-honored functions allocated to them. So what can we use?
Ctrl+Shift+C and Ctrl+Shift+V
Ctrl+Shift+C and Ctrl+Shift+V are direct replacements for Ctrl+C and Ctrl+V, making them easily memorable due to their similarity to their counterparts. By simply highlighting text in the terminal window with your mouse, you can conveniently copy that text into a clipboard buffer by using Ctrl+Shift+C.
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 into a graphical application such as gedit. But note, when you're pasting into an application —and not into a terminal window — you must use Ctrl+V.
And you can go the other way too. You can highlight text in gedit and hit Ctrl+C , and then paste it into a terminal window using Ctrl+Shift+V.
The keyboard shortcut Ctrl+Insert has the same function as Ctrl+Shift+C. Similarly, the combination Shift+Insert is equivalent to Ctrl+Shift+V. However, it is important to note that these shortcuts can only be utilized within the confines of the current terminal window.
Using the Mouse: Right-Click
Instead of using keyboard shortcuts, you can utilize the mouse to conveniently copy and paste text in a terminal window. By simply highlighting the desired text with the mouse, you can easily perform the copy and paste actions.
Once you have highlighted some text, right-click with the mouse and select "Copy" from the context menu.
To paste the copied text, right-click with the mouse once more and select "Paste" from the context menu.
The text is entered at the cursor position on the command line. In this case, the relative path is provided incorrectly, causing Bash to be unable to change the directory. The user omitted the "~/" from the beginning of the path. They only typed "~/" and then copied the remaining part of the path from their initial attempt and pasted it into their subsequent command line.
Upon pressing Enter, they will be successfully navigated to the specified directory.
This example demonstrated how to paste into the same terminal window, but you can apply this right-click method to paste into various terminal windows as well. Additionally, you can utilize this technique to paste into graphical applications.
Using the Mouse: Middle-Button
If your mouse has a middle button, you can take advantage of an even quicker method for copying and pasting. Simply press down gently on the scroll-wheel until it clicks, and voila, you have your middle-button.
Select some text in a terminal window and proceed to middle-click. The selected text will be automatically pasted to the current cursor position on the command line, ensuring seamless copy and paste.
Then press your middle-button:
This method enables you to seamlessly paste text from one terminal window or application to another. Simply select the desired text, navigate to the target terminal window or application, and press the middle button.
Old School - No Mouse
What about when you don't have a mouse? If you can't highlight any text, how can you copy and paste it?
Linux servers are often configured without a graphical desktop environment (GDE), which means mouse access is unavailable. Even on a Linux computer with a graphical desktop environment like GNOME and mouse functionality, there are situations where text cannot be highlighted using the mouse.
For instance, switching to one of the additional TTYs can disable mouse usage. In modern distributions, these TTYs are accessed through Ctrl+Alt+F3 (TTY3) to Ctrl+Alt+F6 (TTY6). (To return to the GDE session, use Ctrl+Alt+F2, and to go to the GDE login screen, use Ctrl+Alt+F1.)
Whether you're using a hardware TTY device to connect to a Linux or Unix computer or not, these methods will be applicable to you. It's important to note that this isn't a simple copy and paste operation, but rather a cut, copy, and paste process that can only be performed within the current command line. However, you can utilize the arrow keys to navigate through your command history and locate the specific command line you intend to cut, copy, and paste from.
The keystrokes you can use are:
Ctrl+W: Cut the word before the cursor, and add it to the clipboard buffer.
Ctrl+K will remove the text after the cursor and save it to the clipboard. If the cursor is at the beginning of the line, the entire line will be removed and copied.
Ctrl+U will remove the text before the cursor and save it to the clipboard. If the cursor is at the end of the line, the entire line will be removed and copied.
Ctrl+Y: Paste the last text that was cut and copied.
Let's hop over to TTY3. (Use Ctrl+Alt+F1 to get back to your desktop afterward.)
Ctrl+Alt+F3
tty
We have a file we wish to delete, let's check it is here.
ls -l file_to_delete.txt
If we press the Up-Arrow key, the last command we used from the command history will be executed. Instead of simply editing this line, we will take a slightly lengthier approach to demonstrate the techniques of cut, copy, and paste. In doing so, we will successfully delete the file.
We'll move the cursor to the first letter of the file name, and then press Ctrl+K. This will remove that part of the line and copy the text to the clipboard buffer.
We'll press Backspace until we clear the line.
We'll type in the rm to delete the file.
And now we can hit Ctrl+Y and paste in the remainder of the line.
This completes our command, and we can press Enter to have the file deleted.
You cannot use this type of cut, copy, and paste function between the additional TTYs. For instance, you cannot cut, copy, and paste text between TTY3 and TTY4.
The additional TTYs can be visualized as a row of physical TTYs placed next to each other. It is not possible to perform cut and paste operations between different physical terminals, and the same limitation applies to these emulations.
Copy That, Control
No matter what scenario you encounter while operating a Linux computer, rest assured that you can utilize copy and paste functionality. There are various methods available to you, including some unconventional ones, but the key point is that you have a range of options.
Editor's P/S
As a Gen Z netizen, I find the article on efficiently copying and pasting text in Linux's Bash Shell to be quite informative. The guide provides clear and concise instructions on how to perform these operations using various methods, making it easy for me to understand and implement.
The article also delves into the historical context of the Ctrl+C and Ctrl+V key combinations, which I find fascinating. It's interesting to learn about the origins of these shortcuts and how they have evolved over time. Overall, I think this article is a valuable resource for anyone looking to improve their efficiency and productivity when working in the Linux command line.