Linewise jump that preserves current column #222
-
I've used the provided linewise snippet for a long time and was using default line numbers too. I finally decided to try relative numbers to be able to jump straight up and down in normal/visual modes. I also wonder if that snippet can be modified to achieve the same result. Currently, it always sends the cursor to the first column, which is really annoying when you want to select a block or some other precise movement (you would have to jump/move to the correct column to "fix" that). |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
We actually jump to each line, to get the value of the last column, and choose either the original column, or the last one on the given line (if the original is beyond that). Finally, we restore the view. -- Get targets.
local saved_view = vim.fn.winsaveview() -- <<
local curcol = vim.fn.col('.') -- <<
local targets = {}
local lnum = wininfo.topline
while lnum <= wininfo.botline do
local fold_end = vim.fn.foldclosedend(lnum)
-- Skip folded ranges.
if fold_end ~= -1 then
lnum = fold_end + 1
else
if (lnum < cur_line - skip_range) or (lnum > cur_line + skip_range) then
vim.fn.cursor(lnum, 0) -- <<
local col = (curcol < vim.fn.col('$')) and curcol or math.max(1, vim.fn.col('$') - 1) -- <<
table.insert(targets, { pos = { lnum, col } }) -- <<
end
lnum = lnum + 1
end
end
vim.fn.winrestview(saved_view) -- << This doesn't handle multibyte characters properly though, but can be tweaked further. I'm thinking about adding a |
Beta Was this translation helpful? Give feedback.
We actually jump to each line, to get the value of the last column, and choose either the original column, or the last one on the given line (if the original is beyond that). Finally, we restore the view.