Gavin Black
Gavin Black

Reputation: 136

Vim, how to scroll to bottom of a named buffer

I have a vim-script which splits output to a new window, using the following command:

below split +view foo

I've been trying to find a way from an arbitrary buffer to scroll to the bottom of foo, or a setting to keep it defaulted to showing the bottom lines of the buffer.

I'm doing most of this inside of a python block of vim script. So I have something like:

python << endpython
import vim
import time
import thread
import sys

def myfunction(string,sleeptime,*args):
    outpWindow = vim.current.window
    while 1:
        outpWindow.buffer.append("BAR")
        #vim.command("SCROLL TO BOTTOM OF foo")
        time.sleep(sleeptime) #sleep for a specified amount of time.

vim.command('below split +view foo')
thread.start_new_thread(myfunction,("Thread No:1",2))
endpython

And need to find something to put in for vim.command("SCROLL TO BOTTOM of foo") line

Upvotes: 0

Views: 1242

Answers (2)

pbx101
pbx101

Reputation: 101

Just for the record, I wanted to set the cursor of a different window without switching to it. Lucily in Neovim there is quite a comprehensive API, the functions are starting with nvim_.... I solved it more or less in this way in vimscript:

let l:bufname = "foo"
let l:lnum = getbufinfo(l:bufname)[0]['linecount']
let l:win_id = bufwinid(l:bufname)
call nvim_win_set_cursor(l:win_id, [l:lnum, 0])

of course if you want to act on the current window, you can just use the setpos() function available in all vim versions.

Upvotes: 0

Luc Hermitte
Luc Hermitte

Reputation: 32926

I usually record the new buffer number (let b_tgt = bufnr('%')), and more precisely its windows number. Then, in a try-finally block I record the current window number (let w_orig = bufwinnr('%')), jump to the window where I need to do something (:exe w_tgt.' wincmd w'), do the thing (:normal! G in your case), and jump back (in the :finally clause) to the current window (at the beginning of the action -> :exe w_orig.' wincmd w').

Now if other buffers may have been opened, or closed, we need to look for the target window each time as its number may have changed. This is done with bufwinnr(b_tgt).

Upvotes: 4

Related Questions