vim-slime icon indicating copy to clipboard operation
vim-slime copied to clipboard

Restore cursor position also with `SlimeRegionSend`

Open fredrikekre opened this issue 2 years ago • 6 comments

When using vim-slime to execute script-like code I often select large portion of the code in visual mode and sending that with SlimeRegionSend. However, when back in normal mode my cursor ends up at the beginning of the previous visual block and I have to navigate down again to wherever I stopped before sending the block. It would be very convenient if the option slime_preserve_curpos also applied for SlimeRegionSend. Thanks!

fredrikekre avatar Dec 20 '22 12:12 fredrikekre

Hi @fredrikekre

You can check the issues and pull requests … similar ideas have been proposed (for example)

If that's not what you meant, we can discuss alternatives.

If you have an example of the behavior you would be prefer, send me

  • a text sample
  • where the cursor started
  • what binding was invoked
  • where the cursor ended up

Thanks

jpalardy avatar Dec 21 '22 03:12 jpalardy

Thanks for the reference, I don't know why I didn't find that in my search.

The suggestion in https://github.com/jpalardy/vim-slime/pull/353#issuecomment-1324571606 is pretty close to what I had in mind, although that is annoying if you select your visual block "backwards", e.g. moving up.

What I had in mind is to have the cursor "stay", meaning that I would like the cursor to end up at the same position after SendRegion as it does when simply Escaping the same visual mode selection. I hope this explanation make sense, I can construct a concrete example if not.

When selecting multiple paragraphs, perhaps even multiple screen-sizes, to send it is IMO pretty disorienting when the file scrolls back to the beginning of the region and you have to navigate back down again. This is what I expected slime_preserve_curpos to mean when I found that option, and it is already how it SendParagraph works with that option.

fredrikekre avatar Dec 22 '22 08:12 fredrikekre

Tricky…

One possible workaround, use o (:help v_o) to toggle the cursor position before sending the region (esp. in the case of selecting backwards). And then using '> ?

I don't think that covers all your use cases though 🤔

  • if you select down, '> will take you back to the bottom of the selection
  • if you select up, the cursor lands at the top (where you were?)

ctrl-o can also help

...

if that doesn't work, send me an example — maybe I still don't understand the before-and-after

jpalardy avatar Dec 22 '22 16:12 jpalardy

Hi! I have a little workaround that moves cursor to the line below the visual block. You can easily modify it to make the cursor stays at the last line of your visual block. This works for backward selecting as well.

vnoremap <silent> <C-A-CR> :<C-u>silent execute("'<,'>SlimeSend") \| let lnum=line("'>") \| execute("silent normal! " . (lnum+1) . "G")<CR>

or use <Plug>SlimeRegionSend still

vnoremap <silent> <C-A-CR> :<C-u>silent execute("normal! \<Plug>SlimeRegionSend") \| let lnum=line("'>") \| execute("silent normal! " . (lnum+1) . "G")<CR>

clpan avatar Mar 10 '23 10:03 clpan

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar May 21 '23 20:05 stale[bot]

Was reminded of this issue and I just wanted to post my solution for this problem in Neovim in case someone is curious:

local function slime_send_region()
    local keys = ":<C-u>call slime#send_op(visualmode(), 1)<CR>"
    local mode = "x"
    vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes(keys, true, true, true), mode, true)
    return
end

vim.keymap.set(
    "x", "<S-CR>",
    function()
        local loc = vim.api.nvim_win_get_cursor(0)
        slime_send_region()
        vim.api.nvim_win_set_cursor(0, loc)
    end
)

fredrikekre avatar Aug 05 '24 06:08 fredrikekre