jbyuki / venn.nvim

Draw ASCII diagrams in Neovim
MIT License
919 stars 20 forks source link

Usability question: shift leftwards text rightwards until end of selection instead of rightwards text getting shifting leftwards on deletion #12

Closed matu3ba closed 1 year ago

matu3ba commented 2 years ago

How can I delete a letter or selection such that the text on the right side remains identical (ie a right shift of the left side by 1 character):

|_1_2_3 123| to |_1_2_3123| or |_1_2_3 123| to |_1_2_3123|

This is a very common operation during drawing, when one wants to insert text leftwards from a drawing or edit it.

jbyuki commented 2 years ago

I'm not aware of any key combinations which archieves this in Neovim/vim. It reminds of the gravity flag available in nvim_buf_set_extmark but it's unrelated.

I would do it in two operations : delete column, insert columns at the front left.

arsham commented 2 years ago

Have a look at this plugin.

matu3ba commented 2 years ago

@arsham The plugin looks cool, but does not offer "deletion rightshift the text". However, I will create an issue to discuss the feature.

matu3ba commented 2 years ago

@jbyuki

If you want and find it useful, you can add (kindly hacked together provided by booperlv)

function _G.RightAlignDelete(register, count)
  local prev_pos = {vim.fn.line("."), vim.fn.col(".")}
  vim.cmd('normal!'..count..'"'..register..'x'..count..'I ')
  vim.fn.cursor(unpack(prev_pos))
end
api.nvim_set_keymap(
  'n', 'gd', ":lua RightAlignDelete(vim.v.register, vim.v.count1)<CR>", {silent=true}
)

function _G.RightAlignDeleteVirtual(register, count)
  local line_start = vim.fn.line("'<")
  local line_end = vim.fn.line("'>")

  vim.cmd('execute "normal! g`<\\<C-v>g`>"')

  vim.cmd('normal!'..count..'"'..register..'x')
  vim.cmd(line_start..','..line_end..'normal!'..count..'I ')

  vim.cmd('execute "normal! g`<\\<C-v>g`>"')
end
api.nvim_set_keymap(
  'x', 'gd', ":<C-u>lua RightAlignDeleteVirtual(vim.v.register, vim.v.count1)<CR>", {silent=true}
)

to the README somewhere. I will close this issue once I have tested, if it works as expected (visual and block mode at least).

jbyuki commented 2 years ago

Sure, I'm not against putting it in the README if it comes in handy for other people. Just open a PR and I will process it. Also it could be nice to have a screencast which shows the action like the other keybinding.

Note: With more context, I understand the usage. Maybe this or this could be interesting.