获取 bash 历史记录到 vi

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2512336/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 21:51:57  来源:igfitidea点击:

get bash history to vi

bashvim

提问by ultracrepidarian

When I try to read bash history into vim, I get nothing.

当我尝试将 bash 历史读入 vim 时,我什么也没得到。

:r !history

If I just execute the command, i.e.

如果我只是执行命令,即

:!history

instead of history I get a snapshot of my terminal as it looked before I started vim.

我得到的不是历史,而是我的终端的快照,就像在我启动 vim 之前一样。

How can I read the output of "history" into vim? Reading the contents of .bash_history won't do as I save history with timestamps:

如何将“历史”的输出读入vim?阅读 .bash_history 的内容不会做,因为我用时间戳保存历史记录:

HISTTIMEFORMAT='%Y.%m.%d %R  '

回答by Rob Heiser

From a shell prompt:

从 shell 提示:

history | vim -

回答by intuited

The problem is that that history is only known by the bash shell that started vim. When you do :!historyfrom within vim, you're starting a new bash shell that has its own history, which is empty, which is why you just see the screen as it looked when you started vim: it's outputting all the lines in its history, which is a grand total of zero. This actually is an oversimplification, but anyway you can't get a history of the commands that you typed just before starting vim this way.

问题是该历史记录只有启动 vim 的 bash shell 才知道。当您:!history在 vim 中执行此操作时,您正在启动一个新的 bash shell,它有自己的历史记录,该历史记录是空的,这就是为什么您只看到启动 vim 时的屏幕:它输出其历史记录中的所有行,总计为零。这实际上过于简单化了,但是无论如何您都无法获得在以这种方式启动 vim 之前键入的命令的历史记录。

If you want to get those lines of history without exiting vim, you can suspend vim by pressing CTRL-Zand then write the history to a file using history >history.tmp. Then type fg 1to resume vim: this will tell bash to transfer focus back to its "job number 1", which will normally be vim. The job number is displayed after you hit CTRL-Z:

如果您想在不退出 vim 的情况下获取这些历史记录行,您可以通过按暂停 vim CTRL-Z,然后使用history >history.tmp. 然后输入fg 1以恢复 vim:这将告诉 bash 将焦点转移回它的“作业号 1”,这通常是 vim。按 CTRL-Z 后会显示作业编号:

[1]+  Stopped                 vim

so if there's a number other than 1 in the brackets, then you should do fgfor that number instead. Then (hopefully you know this) when you're back in vim just :tabedit history.tmp, for example, to open the saved history in a new tab.

因此,如果括号中的数字不是 1,那么您应该fg改为使用该数字。然后(希望你知道这一点)当你回到 vim 时:tabedit history.tmp,例如,在新选项卡中打开保存的历史记录。

You'll have timestamps in this output too, but since you're in vim you can easily filter them out with a :substitutecommand. Alternatively you can cut them out using HISTTIMESTAMP='' historyrather than just historywhen writing to the file; this will still output the index of each entry. I guess you can filter that out on its way into the file too, by piping it through sedor cutor one of their crew. But it's really easy to do this from within vim (assuming you know the basics of regular expressions; if not, start with :help :substituteor maybe look for a regex tutorial).

您也将在此输出中包含时间戳,但由于您在 vim 中,因此可以使用:substitute命令轻松过滤掉它们。或者,您可以使用HISTTIMESTAMP='' history而不是仅history在写入文件时将它们删除;这仍然会输出每个条目的索引。我猜你可以筛选出的道路上进入过的文件,通过其管道sedcut或船员之一。但是在 vim 中做到这一点真的很容易(假设您了解正则表达式的基础知识;如果没有,请从:help :substitute或寻找正则表达式教程开始)。

Note that if you read in the lines from ~/.bash_history, you're only getting the history from bash shells which have completed, ie you typed exitand the terminal window closed. So any commands you typed just before starting vim won't be there. You can change the way this works but then you end up with commands from different sessions all jumbled up together in the history.

请注意,如果您阅读 ~/.bash_history 中的行,您只会从已完成的bash shell 中获取历史记录,即您键入exit并关闭终端窗口。所以你在启动 vim 之前输入的任何命令都不会存在。您可以更改其工作方式,但最终来自不同会话的命令都在历史记录中混杂在一起。

回答by theosp

:r!echo "history" | bash -i 2>/dev/null | sed -e 's/\x1b\[.//g'


Explanation:

解释:

The history command only works on interactive shell.

history 命令仅适用于交互式 shell。

The first part:

第一部分:

echo "history" | bash -i 2>/dev/null

Forces interactive shell (and removes lines which aren't output of history).

强制交互式 shell(并删除不是历史输出的行)。

The second part:

第二部分:

sed -e 's/\x1b\[.//g'

Removes escape character the shell might output (happened on my system).

删除 shell 可能输出的转义字符(发生在我的系统上)。

回答by Ildar Shaymurat

You may pre-write the history of current session:
history -w
Then in editor you can get last, say, 20 commands of history:
:r ! tail -20 ~/.bash_history

您可以预先编写当前会话的历史记录:
history -w
然后在编辑器中您可以获得最后的,例如,20 个历史命令:
:r ! tail -20 ~/.bash_history

回答by Paused until further notice.

Try:

尝试:

r !set -o history; HISTFILE=~/.bash_history; history -r; history 10

This will include the timestamps and will not include history that hasn't been saved to the history file.

这将包括时间戳,但不包括尚未保存到历史文件中的历史。