如何在Linux的Bash Shell中复制和粘贴文本

您所在的位置:网站首页 medly怎么复制粘贴 如何在Linux的Bash Shell中复制和粘贴文本

如何在Linux的Bash Shell中复制和粘贴文本

2024-07-11 02:24:20| 来源: 网络整理| 查看: 265

Concept of a Linux terminal full of text on a laptop Fatmawati Achmad Zaenuri/Shutterstock.com 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.

是否要在Bash shell的命令行中复制并粘贴? 我们将向您展示多种技术,无论您偏爱键盘还是鼠标。 无论您是在图形桌面还是在传统的基于文本的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.

复制和粘贴文本是使用计算机的主要部分。 当人们第一次使用Linux计算机时,无论他们来自Windows还是macOS世界,当尝试在终端窗口中进行复制和粘贴时,他们常常会感到困惑。

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.

在Windows中,使用Ctrl+C复制高亮显示的文本部分,并使用Ctrl+V粘贴。 在macOS中,您可以使用Command+C复制它,并使用Command+V粘贴它。 它们遵循C复制和V插入的相同约定。

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.

这些击键在大多数Linux图形应用程序中都可以使用,例如编辑器gedit或LibreOffice office suite。 Ctrl+C复制突出显示的文本,而Ctrl+V粘贴到光标处。 我们刚接触Linux的人甚至可能根本没有考虑过这些约定已被应用到这些应用程序中的事实。 他们使用肌肉记忆中的击键并继续工作。

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.

一旦我们的新手打开了一个终端窗口,并尝试在Bash shell命令提示符下进行复制和粘贴,则所有更改都会发生。 Ctrl+C和Ctrl+V在没有想到复制和粘贴之前就已分配了功能。 实际上,早在电传打字机(TTY)是一种物理事物的发明之前,这些按键就被招募了很久才被发明出来。

TTY中的Ctrl + C和Ctrl + V (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.

当TTY是物理设备时,选择Ctrl+C作为方便的组合键来生成信号。 该信号是SIGINT ,它告诉当前进程终止。 由于终端窗口是模拟的TTY,因此该击键组合(以及许多其他组合)已保留并在模拟中复制。 请注意,是模拟的终端窗口 。 Bash shell是在该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.”

我们可以轻松地看到已分配给Ctrl+C和Ctrl+V 。 假设您键入以下命令,然后按“ 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.

因为我们使用的是-R (递归)选项,所以ls命令将从根目录开始列出每个文件和目录。 片刻之后,您意识到这不是您想要的,因此您可以通过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.

ls进程终止。 屏幕截图中突出显示了Ctrl+C的可见证据。 它显示为^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组合键调用“普通插入”。 这样,您就可以在键入的内容中输入键的表示形式,而不必获得键的效果 。 要查看此信息,请尝试使用以下命令(请勿键入逗号)。 (例如,要尝试第一个,请按Ctrl + V,然后按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.

顺带一提,您可能会注意到Enter由^M表示。 前面我们看到Ctrl+C显示为^C 看来^代表Ctrl 。 因此, Ctrl+M含义可能与Enter相同。 这是否意味着我们可以通过输入Ctrl+M来输入Enter? 在终端窗口中尝试。 您会看到的。

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+C和Ctrl+V在已经分配了具有悠久历史的功能时执行复制和粘贴文本。 那么我们可以使用什么呢?

Ctrl + Shift + C和Ctrl + Shift + V (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.

容易记住,因为它们非常相似, Ctrl+Shift+C和Ctrl+Shift+V是Ctrl+C和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 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 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.

您也可以粘贴到gedit类的图形应用程序中。 但是请注意,当您粘贴到应用程序中而不是粘贴到终端窗口中时,必须使用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.

您也可以选择其他方式。 您可以在gedit突出显示文本,然后Ctrl+C ,然后使用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.

组合键Ctrl+Insert与Ctrl+Shift+C ,组合Shift+Insert与Ctrl+Shift+V 。 需要注意的是,它们只能在同一终端窗口中使用。

使用鼠标:右键单击 (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.

文本粘贴在命令行上光标的位置。 在此示例中,相对路径指定错误,并且Bash无法更改目录。 用户从路径的开头错过了“〜/”。 他们输入了“〜/”,然后复制了上一次尝试的其余路径,并将其粘贴到第二个命令行中。

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

当他们按Enter键时,它们将移至目录。

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.

Linux服务器通常配置为没有图形桌面环境 (GDE),这意味着您无权访问鼠标。 即使在运行图形桌面环境(如GNOME)Linux计算机上,并且确实有鼠标,在某些情况下,您也无法使用鼠标突出显示文本。

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.)

例如,您可能已切换到其他TTY之一。 在现代发行版中,它们位于Ctrl+Alt+F3 (TTY3)到Ctrl+Alt+F6 (TTY6)。 ( Ctrl+Alt+F2将使您返回到GDE会话,而Ctrl+Alt+F1将使您进入GDE的登录屏幕。)

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

也许您甚至在使用真正的硬件TTY设备连接到Linux或Unix计算机。

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 + W :在光标之前剪切单词,并将其添加到剪贴板缓冲区。

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 + K :剪切光标后的部分行,并将其添加到剪贴板缓冲区。 如果光标在该行的开头,它将剪切并复制整行。

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 + U :剪切光标之前的部分行,并将其添加到剪贴板缓冲区。 如果光标在该行的末尾,它将剪切并复制整行。

Ctrl+Y: Paste the last text that was cut and copied.

Ctrl + Y :粘贴剪切和复制的最后一个文本。

Let’s hop over to TTY3. (Use Ctrl+Alt+F1 to get back to your desktop afterward.)

让我们跳到TTY3。 (然后使用Ctrl + Alt + F1返回到桌面。)

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.

我们将光标移到文件名的第一个字母,然后按Ctrl+K 这将删除该行的一部分并将文本复制到剪贴板缓冲区。

The effect of Ctrl+K in a terminal window

We’ll press Backspace until we clear the line.

我们将按Backspace,直到清除该行。

Line cleared with backspace in a terminal window

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

我们将输入rm来删除文件。

rm in a terminal window

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

现在我们可以Ctrl+Y并粘贴到该行的其余部分。

The effect of Ctrl+Y in a terminal window

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

这样就完成了我们的命令,我们可以按Enter键删除文件。

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.

不能在其他TTY之间使用这种类型的剪切,复制和粘贴。 例如,您不能在TTY3和TTY4之间剪切,复制和粘贴。

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.

最好将额外的TTY视为并排放置的一系列物理TTY。 无法在不同的物理终端之间进行剪切和粘贴,并且在这些仿真中也无法这样做。

复制那个,控制 (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.

无论您在使用Linux计算机时遇到什么情况,都有一种复制和粘贴的方法。 您有选择。 其中一些是奇怪的选择,但至少有一些选择。

翻译自: https://www.howtogeek.com/440558/how-to-copy-and-paste-text-at-linuxs-bash-shell/



【本文地址】

公司简介

联系我们

今日新闻


点击排行

实验室常用的仪器、试剂和
说到实验室常用到的东西,主要就分为仪器、试剂和耗
不用再找了,全球10大实验
01、赛默飞世尔科技(热电)Thermo Fisher Scientif
三代水柜的量产巅峰T-72坦
作者:寞寒最近,西边闹腾挺大,本来小寞以为忙完这
通风柜跟实验室通风系统有
说到通风柜跟实验室通风,不少人都纠结二者到底是不
集消毒杀菌、烘干收纳为一
厨房是家里细菌较多的地方,潮湿的环境、没有完全密
实验室设备之全钢实验台如
全钢实验台是实验室家具中较为重要的家具之一,很多

推荐新闻


图片新闻

实验室药品柜的特性有哪些
实验室药品柜是实验室家具的重要组成部分之一,主要
小学科学实验中有哪些教学
计算机 计算器 一般 打孔器 打气筒 仪器车 显微镜
实验室各种仪器原理动图讲
1.紫外分光光谱UV分析原理:吸收紫外光能量,引起分
高中化学常见仪器及实验装
1、可加热仪器:2、计量仪器:(1)仪器A的名称:量
微生物操作主要设备和器具
今天盘点一下微生物操作主要设备和器具,别嫌我啰嗦
浅谈通风柜使用基本常识
 众所周知,通风柜功能中最主要的就是排气功能。在

专题文章

    CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭