0

As title. How to move the cursor with vim command :...? I want to make a toggle that achieves this behavior but I didn't find anywhere this is explained :(

1

Any of

  • :normal! ^ (or 0, or $)
  • the functions setpos, cursor, or setcursorcharpos

should do it.

OTOH, on the : line you need things like <C-b> and <C-e> (see :help cmdline-editing), or press <C-f> and use normal motions.

0

Update:

This ternary version for 0 ^ $ is much better. Please call me UX-magician :)

vim.api.nvim_set_keymap('n', '0', '<cmd>lua toggle_zero_goto_line_beginning()<CR>', NOREF_NOERR_TRUNC)
local __goto_line_beginning_value = 0
function _G.toggle_zero_goto_line_beginning()
  if (__goto_line_beginning_value == 0) then
    vim.cmd("exe 'normal! 0'")
    __goto_line_beginning_value = 1
  elseif (__goto_line_beginning_value == 1) then
    vim.cmd("exe 'normal! ^'")
    __goto_line_beginning_value = 2
  else
    vim.cmd("exe 'normal! $'")
    __goto_line_beginning_value = 0
  end
end

Old one

I just tried-n-error in Vim and turn it into neovim version:

vim.api.nvim_set_keymap('n', '0', '<cmd>lua toggle_zero_goto_line_beginning()<CR>', NOREF_NOERR_TRUNC)
local __at_beginning = false
function _G.toggle_zero_goto_line_beginning()
  if (not __at_beginning) then
    vim.cmd("exe 'normal! ^'")
    __at_beginning = true
  else
    vim.cmd("exe 'normal! $'")
    __at_beginning = false
  end
end

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.