3

I have the following lines in my .bashrc:

# C-Z shortcut can be used to take a suspended job to fg
stty susp undef
bind '"\C-z":" fg >/dev/null 2>&1 \015"'

The purpose of this was to make the binding C-Z better suspend Vim (as it already does as default within Vim), and then use C-Z in the bash command line interface to get back to Vim immediately.

This seems to be working perfectly except for one thing:

me:~$ vim ~/.bashrc

[1]+  Stopped                 vim ~/.bashrc
me:~$  fg >/dev/null 2>&1
me:~$  fg >/dev/null 2>&1
me:~$  fg >/dev/null 2>&1
me:~$  fg >/dev/null 2>&1
me:~$  fg >/dev/null 2>&1
me:~$

fg >/dev/null 2>&1 shows up every time. Is there any way to make this binding "silent" so that no output is shown at all? Many guides use zsh or fish to achieve this kind of behaviour, but I wish to stick to bash.

ruohola
  • 21,987
  • 6
  • 62
  • 97
herophant
  • 642
  • 7
  • 16
  • @romainl `>/dev/null 2>&1` prevents any output to the terminal, since I want this to be as seamless as possible. The space before `fg` prevents bash from keeping this command in its history. Again, I want this operation to be as "invisible" possible. – herophant Oct 01 '20 at 13:42
  • 1
    This seems a bad idea... `fg` can easily go just fine in your muscle memory so that you are no slower than hitting ctrl-z. – Enlico Oct 15 '20 at 11:43

1 Answers1

3

This should work for you:

stty susp undef
bind -x '"\C-z":"fg >/dev/null 2>&1"'

Keep in mind though that this might not be such a good idea, since it'll prevent you from suspending most other programs.

ruohola
  • 21,987
  • 6
  • 62
  • 97
  • How is this different from my setup? You have removed the `>/dev/null 2>&1` and `\015` from my bind command.. (makes sense because you added the `-x`) But because this does not help make this command invisible, it does not answer my question. The `$ fg` and `[1]+ Stopped vim` still show up on the terminal.. – herophant Oct 15 '20 at 19:25
  • @herophant, the `fg` does not shop up in the terminal, nor in the command history. Of course the `Stopped vim` shows up, you cannot get rid of that. – ruohola Oct 15 '20 at 19:38
  • I think maybe my question is a bit confusing.. Please put in my 2 lines in your `.bashrc` and see for yourself that `Stopped vim` is no longer showing up in the terminal because I redirect all output to `/dev/null`. However, `$ fg >/dev/null 2>&1` still shows up on my terminal. My question is asking how I can get rid of `fg >/dev/null 2>&1` from showing up on the terminal. – herophant Oct 15 '20 at 19:46
  • @herophant Edited my answer, still couldn't get away from the first `[1]+ Stopped vim`, but no output comes after that. – ruohola Oct 16 '20 at 05:15
  • This does not answer my question. `fg >/dev/null 2>&1` still shows up on my screen. This is also equivalent to the set-up that I have already stated in my question (you have replaced the `\015` character with the `-x` option, but they are equivalent). – herophant Oct 16 '20 at 18:04
  • 1
    @herophant What are you on about? The commands are NOT equivalent. Something is wrong with your setup. See this demo: https://streamable.com/jk8qnv – ruohola Oct 16 '20 at 18:27
  • That is so weird. I swear I tried it many times with both and it gave the exact same behaviour. But thank you, it works. – herophant Oct 16 '20 at 18:51