I use Vim for editing DocBook files. I created several functions that manipulate the text in the buffer. For example to build a PDF from the source using an external command. I run these functions asynchronously in a separate Vim terminal buffer. I need to capture the last line of the terminal buffer after the build PDF process finishes because it is the path to the target PDF file. The process is following:

let term_buf_no = s:RunCmdTerm(cmd, 'term_name', 'exit_cb')

The above command runs s:RunCmdTerm function and passes the cmd which is the actual command to be run (building PDF) + a callback function exit_cb

the RunCmdTerm function runs cmd in a terminal buffer like this:

let term_buf_no = term_start(cmd, {'term_name': name, 'term_rows': 10, 'exit_cb': exit_cb})

Everything works fine, but when I want to capture the last line of the terminal buffer inside the exit_cb function with:

let target_dir = getbufline(term_buf_no, '$')[0]

it captures earlier lines from the output but never the last one.

Any ideas?

  • 1
    Do you really need a terminal? If you just want to run the command to extract the last line, you can use systemlist()
    – balki
    Jan 29, 2023 at 20:00

1 Answer 1


The term_wait(150) function before reading the last line of the terminal buffer output solved it :-)

  • 2
    Maybe could you post a very light version of the entire solution such that the problem and its solution could be reproduced? Jan 27, 2023 at 15:50
  • So you were calling exit_cb before the terminal process finish?
    – gildux
    Jan 31, 2023 at 17:26

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.