How to Copy and Paste Text at Linux’s Bash Shell

Concept of a Linux terminal full of text on a laptop
Fatmawati Achmad Zaenuri/Shutterstock.com

Want to copy and paste at the command line of the Bash shell? We’ll show you multiple techniques whether you favor the keyboard or the mouse. These work whether you’re at a graphical desktop or at a traditional text-based TTY.

The Usual Keyboard Shortcuts Won’t Work

Copying and pasting text is a staple part of using a computer. When people use a Linux computer for the first few times, whether they come from the Windows or the macOS worlds, they are often confounded when trying to copy and paste within a terminal window.

In Windows, you use Ctrl+C to copy a section of highlighted text and Ctrl+V to paste it. In macOS, you use Command+C to copy it and Command+V to paste it. They follow the same convention of C to copy and V to insert.

Those very same keystrokes work in most Linux graphical applications, such as the editor gedit or the LibreOffice office suite.Ctrl+C copies highlighted text and Ctrl+V pastes it at the cursor. Our newcomer to Linux probably doesn’t even think about the fact that these conventions have been carried through to these applications. They use the keystrokes from muscle memory and get on with their work.

Once our newcomer opens a terminal window and tries to copy and paste at the Bash shell command prompt, that all changes. Ctrl+C and Ctrl+V were allocated functions long before copying and pasting were ever thought of.  In fact, those keystrokes were recruited a long time before graphical shells were invented, back when a teletype (TTY) was a physical thing.

Ctrl+C and Ctrl+V in TTYs

When a TTY was a physical device, Ctrl+C was chosen as a handy key combination to generate a signal. That signal is SIGINT, which tells the current process to terminate. Because a terminal window is an emulated TTY, that keystroke combination (and many others) have been preserved and replicated in the emulation. Note that it is the terminal window that is the emulation. The Bash shell is a program running in that 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 /

ls -R / in a terminal window

Because we’re using the -R (recursive) option the ls command will start to list every file and directory, starting from the root directory. After a couple of moments, you realize this is not what you wanted, so you terminate the process by hitting Ctrl+C.

Ctrl+C

Ctrl+C in a terminal window

The ls process is terminated. The visible evidence of the Ctrl+C is highlighted in the screenshot. It is displayed as ^C.

The Ctrl+V key combination invokes “verbatim insert.” This allows you to enter a representation of a key into what you’re typing, instead of getting the effect of the key. To see this, try the following commands (don’t type the commas). (For example, to try the first one, press Ctrl+V and then press Enter.)

Ctrl+V, Enter
Ctrl+V, PgDn
Ctrl+V, RightArrow
Ctrl+V, Esc

Ctrl+V, Enter in a terminal window

As a quick aside, you may notice that Enter is represented by ^M . We saw earlier that  Ctrl+C showed up as ^C. It would seem that ^ represents Ctrl. So Ctrl+M probably means the same as Enter. Does that mean we can enter Enter by typing Ctrl+M? Try it in a terminal window. You’ll see that it does.

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?

RELATED: How to Be More Productive in Ubuntu Using Keyboard Shortcuts

Ctrl+Shift+C and Ctrl+Shift+V

Easily remembered because they are very 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 hit Ctrl+Shift+C you’ll copy that text into a clipboard buffer.

Ctrl+Shift+C

Ctrl+Shift+C in a terminal window

You can use Ctrl+Shift+V to paste the copied text into the same terminal window, or into another terminal window.

Ctrl+Shift+V

Ctrl+Shift+V in a terminal window

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.

Pasting from a terminal window into the gedit editor

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.

Copying from the gedit editor and pasting into a terminal window

The key combination Ctrl+Insert is the same as Ctrl+Shift+C , and the combination Shift+Insert is the same as Ctrl+Shift+V. The caveat here is that these can only be used within the same terminal window.

Using the Mouse: Right-Click

You can use the mouse to copy and paste in a terminal window. You have to use the mouse to highlight the text you’re going to copy, so why not use it to perform the copy and paste actions?

Once you have highlighted some text, right-click with the mouse and select “Copy” from the context menu.

terminal window with context menu and copy highlighted

To paste the copied text, right-click with the mouse once more and select “Paste” from the context menu.

terminal window with context menu and paste selected

The text is pasted at the position of the cursor on the command line. In this example, the relative path has been given incorrectly, and Bash cannot change directory. The user missed the “~/” from the start of the path. They’ve typed the “~/” and then copied the remainder of the path from their previous attempt and pasted it into their second command line.

When they hit Enter, they are moved to the directory.

terminal window with a changed directory achieved through copying and pasting

This example showed pasting into the same terminal window, but you can use this right-click technique to paste into different terminal windows. You can also paste into graphical applications using this method.

Using the Mouse: Middle-Button

There’s an even faster way to copy and paste using the mouse, as long as your mouse has a middle button. If you press down on your scroll-wheel (gently!) and it clicks, you’ve got a middle-button.

Highlight some text in a terminal window and then press your middle-button. The highlighted text is pasted to the cursor position on the command line. The copy and paste happen at the same time.

So, highlight some text:

terminal window with some text highlighted

Then press your middle-button:

terminal window with highlighted text pasted on the command line

You can use this method to paste between different terminals windows, and into graphical applications too. Just highlight the text, move to the other terminal window or application, and press your 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 you don’t have access to a mouse. Even on a Linux computer that is running a graphical desktop environment like GNOME and you do have a mouse, there will be instances when you can’t use the mouse to highlight text.

For example, you might have swapped over to one of the additional TTYs. On modern distributions, these are located at Ctrl+Alt+F3 (TTY3) through to Ctrl+Alt+F6 (TTY6). (Ctrl+Alt+F2 will return you to your GDE session and Ctrl+Alt+F1 will take you to the log in screen of your GDE.)

Perhaps you are even using a genuine hardware TTY device to connect to a Linux or Unix computer.

Either way, these techniques will work for you. The point to remember here is that this isn’t copy and paste, it is cut, copy, and paste, and you can only cut and copy from the current command line. Of course, you can use the arrow keys to scroll through your history to find the command line you wish 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: Cut the part of the line after the cursor, and add it to the clipboard buffer. If the cursor is at the start of the line, it will cut and copy the entire line.
  • Ctrl+U: Cut the part 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: 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

tty3 in a terminal window

We have a file we wish to delete, let’s check it is here.

ls -l file_to_delete.txt

ls -l file_to_delete.txt in a terminal window

If we use the Up-Arrow key, we’ll call the last command we used out of the command history. We could just edit this line, but the object is to demonstrate the cut, copy and paste, so we’ll accomplish our task of deleting the file in a slightly protracted fashion.

Retrieved command from command history in a terminal window

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.

The effect of Ctrl+K in a terminal window

We’ll press Backspace until we clear the line.

Line cleared with backspace in a terminal window

We’ll type in the rm to delete the file.

rm in a terminal window

And now we can hit Ctrl+Y and paste in the remainder of the line.

The effect of Ctrl+Y in a terminal window

This completes our command, and we can press Enter to have the file deleted.

File deleted with no error message in a terminal window

This type of cut, copy, and paste cannot be used between the additional TTYs. You cannot cut, copy, and paste between TTY3 and TTY4, for example.

The additional TTYs are best thought of as a line of physical TTYs sitting side by side. There is no way to cut and paste between the different physical terminals, and there is no way to do so in these emulations.

Copy That, Control

Whatever situation you find yourself in when using a Linux computer, there’ll be a way to copy and paste. You have options. Some of them are strange options, but at least there are options.





Source link

Leave a comment

Your email address will not be published. Required fields are marked *