I have the following vim.keymap.set:
-- vimscript way:
-- inoremap <expr> <Home> col('.') == 1 ? '<C-O>^' : '<C-O>0'
vim.keymap.set('n', '0',
function()
local pos = (vim.api.nvim_win_get_cursor(0)[2]+1)
vim.cmd('normal! ^')
if pos == (vim.api.nvim_win_get_cursor(0)[2]+1) then
vim.cmd('normal! 0')
end
end, { noremap = true, silent = true, desc = "toggle between start of the line of first non blank", }
)
I want to improve this function to add $
or g_
as a third option, it would be great. Maybe a shorter way of getting the current cursor position. Is vim.fn.getpos('.')
something better than what I am doing?
Another idea is to avoid this mapping when using macros, because the expected behavior of zero in normal mode is jumping to the first character of the line, that’s way I have lost my old vim mapping that I were used to toggle these three options.
Just as a tip, I also have other complicated mappings, still using vim syntax:
map( "n", "<C-l>", [[ (&hls && v:hlsearch ? ':nohls' : ':set hls')."\n" <BAR> redraw<CR>]],
{ silent = true, expr = true, desc = "toggles highlighting" })