Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get bash history to vi

Tags:

bash

vim

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

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

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:

HISTTIMEFORMAT='%Y.%m.%d %R  '
like image 259
ultracrepidarian Avatar asked Mar 25 '10 00:03

ultracrepidarian


People also ask

How do I get Bash history?

The command is simply called history, but can also be accessed by looking at your . bash_history in your home folder. By default, the history command will show you the last five hundred commands you have entered.

Where is Bash history file?

For each user, this file resides at the same location: ~/. bash_history . Typically, this file keeps track of the user's last 500 commands.

How do I view vi history?

In vi you hit esc once and then k or n will move you backward or forward through the commands. For emacs and gmacs, use control-p or control-n each time. To view more of your history, type "history", and a list of your most recent commands will be displayed along with numbers.


2 Answers

From a shell prompt:

history | vim -
like image 154
Rob Heiser Avatar answered Nov 09 '22 04:11

Rob Heiser


The problem is that that history is only known by the bash shell that started vim. When you do :!history from 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.

If you want to get those lines of history without exiting vim, you can suspend vim by pressing CTRL-Z and then write the history to a file using history >history.tmp. Then type fg 1 to 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:

[1]+  Stopped                 vim

so if there's a number other than 1 in the brackets, then you should do fg for 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.

You'll have timestamps in this output too, but since you're in vim you can easily filter them out with a :substitute command. Alternatively you can cut them out using HISTTIMESTAMP='' history rather than just history when 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 sed or cut or 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 :substitute or maybe look for a regex tutorial).

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 exit and 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.

like image 21
intuited Avatar answered Nov 09 '22 03:11

intuited