0

So, I'm trying to find an actual solution to another question on here that doesn't really have one. However, I'm running into a problem with backgrounding/foregrounding behaviour.

I need to background any processes spawned from the shell inside the terminal. So far, I have the following assembled:

function! s:terminal_clear()
   let l:scrollback = &scrollback
   set scrollback=1

   " Background any child-process. (No idea why <C-z> doesn't work.)
   let l:children = systemlist('pgrep -P '.b:terminal_job_pid)
   for pid in l:children[1:]
      call jobstart(['kill', '-STOP', pid])
   endfor

   " Space at the start of the command, plus additional space-return, prevents this from being added
   " to shell history. Requires `setopt histignorespace`.
   call chansend(&channel, " printf '\\n%.0s' {1..200}\<CR> \<CR>")
   call feedkeys("i\<C-l>")

   sleep 1000m

   for pid in l:children
      call jobstart(['kill', '-CONT', pid])
   endfor

   exec "set scrollback=".l:scrollback
endfunction

Unfortunately, the for-loop at the end, calling kill -CONT ... on each backgrounded PID, isn't functioning.

I can resume the suspended processes manually, using fg; but that's a bit of a nonstarter (doing so in the terminal-emulator-in-question, almost no matter what I do, leaves an fg entry in the shell-history — completely defeating all the work I've been doing here in the first place to prevent precisely that.)

Does anybody know why kill -CONT might not work on a process inside Vim's terminal emulator? What am I doing incorrectly, here?

2
  • 2
    Just FYI if you're using Bash and add environment variable HISTIGNORE with value "fg" that command shouldn't be saved in your history. (Multiple commands can be added by separating them with :, e.g. export HISTIGNORE=fg:bg)
    – B Layer
    Commented Aug 24, 2021 at 6:43
  • 1
    This is fantastic news, and will totally work for me! Unfortunately, I'd really like to push this up as a plugin that will work in a wide variety of situations; hopefully someone can come up with a good solution that'll work on other's machines … Commented Aug 24, 2021 at 6:47

1 Answer 1

1

Note that job control in a shell is different from just sending a SIGSTOP signal to the process. It's actually much more about setting terminal settings, such as the controlling process group. So you can't really just foreground a suspended job in the shell by sending the processes in that job a SIGCONT (or, for that matter, any other signal.)

See also:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.