Warm tip: This article is reproduced from stackoverflow.com, please click
vim

How to enforce vim specific shell history

发布于 2020-04-11 22:39:30

Is there any way to have the shell history specific only to a certain instance of vim. For example, observe these sequence of events in both vim shell (:sh) and another terminal

   Vim (:sh)                  |    Terminal
------------------------------------------------
1  pdflatex foo.tex           |
2                             |   echo hello
3                             |   echo world  

the command history for both the Terminal and Vim is

1  pdflatex foo.tex
2  echo hello
3  echo world  

Is there any way so the command history for vim is

1  pdflatex foo.tex

and the Terminal is

1  echo hello
2  echo world  

I ask because I just want to hit the up arrow to execute pdflatex foo.tex, I don't want to go through every other command as well. Also, it would be nice if this vim specific history could be saved in the vim session file.

Questioner
puk
Viewed
65
David Brown 2012-01-08 19:54

Bash stores it's history in ~/.bash_history by default. This can be changed with the HISTFILE environmental variable. So if you want to use a separate history file when you start bash in vim you'll have to set a different value to HISTFILE. You can do this by creating a special config file for bash to use when run from within vim. So first make a file ~/.vimbashrc with this one line in it

export HISTFILE=~/.vim_bash_history

This will tell bash to use ~/vim_bash_history as your history file. You could also add more if you want any other specific settings for shells started from vim. Then in your .vimrc add the line

set shell=/bin/bash\ --init-file\ ~/.vimbashrc

The shell setting in vim tells it what command to execute when using :! commands and the :shell command. The --init-file flag tells bash to load the specified config file (it will be loaded after loading your normal .bashrc). So now when you run the command :sh it will run bash with this config file which will set it to use a different history file.