在 Python interactive shell 时,我可以将之前的命令复制到剪贴板吗?

When in Python interactive shell, can I copy the previous command to the clipboard?

交互式 shell 中是否有将最后一个表达式复制到剪贴板的命令?

我知道有 _ 命令,它会重复最后一个表达式的求值,例如

>>>  " ".join(['a', 'b', 'c'])
'a b c'
>>> _
'a b c'

但我正在寻找的是将 " ".join(['a', 'b', 'c']) 复制到剪贴板的命令。有这种东西吗?

您可以使用终端仿真器的工具来做到这一点。

Python 本身没有这样的功能 -- 但可以为 IPython 添加用户提供的魔术命令 -- here's one for Linux and MacOS.

使用 ipython 终端..

$ ipython

Python 3.6.6 |Anaconda, Inc.| (default, Jun 28 2018, 11:07:29) 
Type 'copyright', 'credits' or 'license' for more information
IPython 6.5.0 -- An enhanced Interactive Python. Type '?' for help.



In [1]: " ".join(['a', 'b', 'c'])
Out[1]: 'a b c'

In [2]: _
Out[2]: 'a b c'

In [3]: _1
Out[3]: 'a b c'

In [4]: __
Out[4]: 'a b c'

In [5]: _
Out[5]: 'a b c'

In [6]: _1
Out[6]: 'a b c'