1

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:

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

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

Aditya
  • 175

1 Answers1

1

From help mapfile synonym of readarray:

Options:
-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.