In normal mode, I'd like to press a key that behaves like ^ (move the cursor to the first non-blank character of the current line), unless the cursor is already there, in which case it should behave like $ (move the cursor to the last character of the line).

  • 3
    What did you try? Also note that ^ and $ are not symmetrical.
    – romainl
    Aug 7, 2023 at 20:19

2 Answers 2


Here's the VimScript equivalent to ax ax's answer (using Vim 9's {})

command EorBofL {
  const start = col('.')
  normal! ^
  if col('.') isnot# start
    normal! $
nnoremap <silent> KEY :EorBofL<enter>

Additionally, if you will never write :EorBofL yourself, might as well make it an (autoload) function instead of a command.

  • Thanks for the tip, although it's not clear to me what's the advantage of an autoload function vs a command. (The n there is normal mode, being [KEY] the actual key to bind).
    – ax ax
    Sep 14, 2023 at 14:04
  • Personally i would use autoload here. Commands i generally reserve for “cant be done otherwise” or “i would type this, not map it” @axax
    – D. Ben Knoble
    Sep 14, 2023 at 18:02

Since I couldn't find a way to do it, I wrote a function (in nvim). Never done any scripting in vim, so chances are something is slight off or completely wrong, but it seems to work.

vim.api.nvim_create_user_command('EorBofL', function()
  local init_col = vim.fn.col('.')
  vim.cmd('norm! ^')
  local current_col = vim.fn.col('.')
  if init_col == current_col then
    vim.cmd('norm! $')
end, {})
vim.api.nvim_set_keymap('n', [KEY], ':EorBofL<CR>', { noremap = true, silent = true })

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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