Directly on the Ubuntu terminal, I can extract the results from top regarding a specific PID with:

top -p <pid> -n1 | awk '/<pid>/'

Where <pid> should be substituted with a PID number.

When I am inside gVim 8.1, I try doing the same with:

:call system("top -p <pid> -n1 | awk '\/<pid>\/'")

I use the backslashes to make the forward-slashes be interpreted correctly as the / character. However, to my surprise, I get the error:

TERM environment variable not set

I found that to be odd, since things like :call system("ps") work perfectly.

If I try the same in terminal Vim 8.1, I get:

top: failed tty get^@

But again all sorts of variations of :call system("ps") work well.

Why would that be the case and how could I make it work such that I retrieve the results of system("top -p <pid> -n1 | awk '\/<pid>\/'") inside a variable like:

let myvariable = system("top -p <pid> -n1 | awk '\/<pid>\/'")

1 Answer 1


top is a interactive program, so I'm not surprised it has problems when called in a non interactive shell without a terminal. Loosely speaking this is like

$ top -p 7783 -n1 </dev/null | awk "/7783/"
top: failed tty get

To use top like you want, add the option -b for "batch mode".

From the man page:

-b Batch-mode operation
Starts top in Batch mode, which could be useful for sending output from top to other programs or to a file. In this mode, top will not accept input and runs until the iterations limit you've set with the `-n' command-line option or until killed.


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.