Skip to content

[Feature]: Restore/retain cursor position when swapping buffers between windows #225

@scienceplease

Description

@scienceplease

Similar Issues

  • Before filing, I have searched for similar issues.

Description

When swapping buffers between windows where { move_cursor = true } it would be nice if the cursor position in each respective buffer is preserved/restored after the swap. As an example, I can restore the cursor position in the active buffer after the swap via the following.

local function swap(direction)
    return function()
        local cursor = vim.api.nvim_win_get_cursor(0)
        require("smart-splits")['swap_buf_' .. direction]({ move_cursor = true })
        vim.api.nvim_win_set_cursor(0, cursor)     
    end
end

vim.keymap.set('n', '<leader><leader>h', swap('left'))
vim.keymap.set('n', '<leader><leader>j', swap('down'))
vim.keymap.set('n', '<leader><leader>k', swap('up'))
vim.keymap.set('n', '<leader><leader>l', swap('right'))

Though with this approach I cannot restore the cursor in the buffer that I am swapping with because getting direction-based window ids is internal to the plugin. An alternative to making this behavior the default would be to expose API functions that allow user code to query for window ids that are left/down/up/right to a given window id. The latter would also enable users to implement custom functionality and not require changes to the plugin itself.

Metadata

Metadata

Assignees

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions