8

I am looking for a way to make midnight commander share its subshell history with the parent shell. To be more specific, when I start mc, open its subshell using Control+o and subsequently invoke a command in the subshell, this command is not available in the outer shell history after exiting mc. Is there a way to share the history between the two shells? I start mc from bash, if that's relevant. Thanks.

3 Answers 3

9

This could be done with the following command (before starting mc):

export PROMPT_COMMAND="history -a; history -r"

If you would like to make it permanent, then you should add it to your ~/.bashrc. But it will make all shell history global, which is not the best. If you don't want this side effect, then the following line should be added instead of the above one:

alias mc='PROMPT_COMMAND="history -a; history -r" mc; history -r'

Explanation:

PROMPT_COMMAND is executed just before bash displays a prompt. Further reading here.

history should be called with append parameter, and after that with read parameter. Further reading here.

1
4

For some reason, the PROMPT_COMMAND solution in an mc alias, as suggested by user szkj, didn't work correctly for me. While it did preserve commands executed in the MC subshell, commands entered before starting MC were lost after exiting.

Finally, I use the following in ~/.bashrc, without an mc alias. It seems to work fine despite the fact that mc overwrites $PROMPT_COMMAND.

export HISTCONTROL=ignoreboth
shopt -s histappend
export PROMPT_COMMAND="history -a; history -c; history -r; $PROMPT_COMMAND"

This is what I get:

# echo "1: before starting MC. PROMPT_COMMAND=$PROMPT_COMMAND"
1: before starting MC. PROMPT_COMMAND=history -a; history -c; history -r; 

# mc

# echo "2: Inside MC. PROMPT_COMMAND=$PROMPT_COMMAND"
2: Inside MC. PROMPT_COMMAND=pwd>&8;kill -STOP $$

# history | tail -3
 3038 echo "1: before starting MC. PROMPT_COMMAND=$PROMPT_COMMAND"
 3039 echo "2: Inside MC. PROMPT_COMMAND=$PROMPT_COMMAND"
 3040 history | tail -3

# echo "3: Going to exit MC with F10."
3: Going to exit MC with F10.

# echo "4: Back in Bash after MC"
4: Back in Bash after MC

# history | tail -7
 3038 echo "1: before starting MC. PROMPT_COMMAND=$PROMPT_COMMAND"
 3039 echo "2: Inside MC. PROMPT_COMMAND=$PROMPT_COMMAND"
 3040 history | tail -3
 3041 echo "3: Going to exit MC with F10."
 3042 mc
 3043 echo "4: Back in Bash after MC"
 3044 history | tail -7

My system is:

# cat /etc/debian_version 
8.1

# bash --version | head -1
GNU bash, version 4.3.30(1)-release (x86_64-pc-linux-gnu)

# mc --version | head -1
GNU Midnight Commander 4.8.13

# uname -a
Linux afnet 3.16.0-4-amd64 #1 SMP Debian 3.16.7-ckt11-1+deb8u3 (2015-08-04) x86_64 GNU/Linux
1
  • despite the fact that mc overwrites $PROMPT_COMMAND actually it doesn't overwrite since 4.8.14.
    – ALex_hha
    Jun 20, 2016 at 11:56
0

Sadly because my yet low reputation I can not comment the previous post(s) but I really would like to share that it seems for me @mivk 's solution works fine even in now ('22), with ZSH also using the following 'mutation':

export HISTCONTROL=ignoreboth
#shopt -s histappend # For BASH
setopt APPEND_HISTORY # For ZSH
export PROMPT_COMMAND="history -a; history -c; history -r; $PROMPT_COMMAND"

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .