So I routinely push applications to background and switch among them. I realized that fg doesn't have a tab complete defined.

So I thought I can put together a tab complete for fg quickly with the suggestions coming from the jobs command. I didn't check if something like this exists online already, cause this seemed like a fun project to build. Here's my code so far:

  readarray COMPREPLY < <(jobs -l)
  for i in "${!COMPREPLY[@]}"; do
      printf -v pad %*s -$COLUMNS "${COMPREPLY[i]}"

The problem is that at the end of each suggestion there's a stray linefeed character(^j) showing up eg:

%1 53967 Stopped vim^J

%2 54257 Stopped python^J

%3 54499 Stopped (signal) nano^J

%4- 42270 Stopped vim ~/.bashrc^J

%5+ 47434 Stopped vim ~/.bashrc^J

  • 1
    Change readarray to readarray -t. – Isaac Aug 12 '18 at 6:24

From help mapfile synonym of readarray:

-t Remove a trailing DELIM from each line read (default newline)

Add that option to your readarray call to trim the trailing new lines off.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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