0

ctrl-z is the short cut for bg,what's for fg?

Anyone knows this?

3 Answers 3

10

crtl+z sends the suspend signal (SIGTSTP). It doesn't place the process in the background.

5
  • 1
    it works as bg while in vim,so I want another short cut to go back to vim instead of typing fg..
    – linux
    Jun 24, 2011 at 2:33
  • 8
    Nope, wrong again. Vim is being suspended; that is not the same as running in the background. A suspended job is just that, one that isn't running. bg runs the job, but stdin and stdout are no longer attached to the console. fg attaches a background or suspended job to the console again.
    – Chris S
    Jun 24, 2011 at 2:34
  • 1
    is there a short cut to attach a suspended job to the console again?
    – linux
    Jun 24, 2011 at 2:38
  • 1
    Not that I am aware of, though various distributions tend to do whatever they please.
    – Chris S
    Jun 24, 2011 at 2:45
  • +1 to @ChrisS for this observation. Some distributions/versions of terminal applications will also allow the end user to set their own custom keyboard commands through a GUI or setting menu. For example Terminal.app on OS X will allow some customization under Terminal->Preferences->Keyboard.
    – Matt
    Jul 13, 2015 at 21:13
6

You can define your own in $HOME/.inputrc...

# ALT+Z
"\M-z": "fg\n"

...or by adding bind -x's to your .profile/.bashrc.

bind -x '"\M-z"':"fg" # ALT+Z

For more information, see the READLINE section of the bash manpage.

2
  • just tried,seems not working.
    – linux
    Jun 24, 2011 at 4:34
  • Which method did you try? Did you reload the relevant files? (e.g., logging out/back in; starting a new shell; running . .bashrc or . .profile; or pressing CTRL+x CTRL+r for .inputrc if default keybindings haven't been changed) If so, do you get any errors?
    – Kanji
    Jun 24, 2011 at 20:36
5

The goal is, I suppose, to exit vim, do some shell stuff and reenter vim as fast as possible. I got quite fast by using crtl+z to exit vim and crtl+z again to reenter.

Exit works well without further ado.

To make the reentry work I undefed the standard crtl+z behaviour (susp) and mapped it to fg followed by the Enter key (\015).

These are the relevant lines from my .bashrc.

stty susp undef
bind '"\C-z":"fg\015"'

Works like a charm on OS X 10.10 and Ubuntu Linux 12.04 LTS. Probably should work on other Unices, too.

See also this Github Repo for a nice looking solution using iTerm2 and these vim productivity hints for a native zsh implementation (hint VII) and other general awesomeness.

4
  • Wait until you discover :!... Jul 13, 2015 at 21:32
  • 1
    Yeah, I know about :!. It's just better suited to one off commands and not for longer sessions in the shell. Of course, one could also use a tmux split and a vim session side by side... Jul 14, 2015 at 18:55
  • And I have multiple Konsoles open. There's more than one way... Jul 14, 2015 at 18:59
  • 2
    So I didn´t undef susp, and did bind -x '"\C-a":"fg"' and this works great. ctrl-a is now 'anti-ctrl-z', so I can quickly switch between terminal and vim, and only one arrow up to repeat last command to test the updated script or code. Oct 2, 2015 at 8:29

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