kana / vim-submode

Vim plugin: Create your own submodes
http://www.vim.org/scripts/script.php?script_id=2467
217 stars 24 forks source link

Mappings stop working once I hit the end/beginning of the file #35

Open hasufell opened 3 years ago

hasufell commented 3 years ago

Config is

let g:submode_always_show_submode = 1
call submode#enter_with('nav', 'n', '', '<C-up>')
call submode#map('nav', 'n', '', 'e', '6j')
call submode#map('nav', 'n', '', 'o', '6k')
call submode#map('nav', 'n', '', 'n', 'B')
call submode#map('nav', 'n', '', 'i', 'W')

I can enter the submode successfully and navigate. Once the cursor hits the end or beginning of the file (or rather: tries to move past it), the mappings stop working, but I'm still inside the submode.

hasufell commented 3 years ago

It appears vim is treating it as an error when you try to jump beyond the file boundaries, e.g.:

nnoremap j 3j:echomsg "FOO"<cr>

Will not echo FOO when you're on the last line and pressing j.

As such, a workaround is this:

call submode#map('nav', 'n', 's', 'e', ':normal! 6j<cr>')
call submode#map('nav', 'n', 's', 'o', ':normal! 6k<cr>')
novasenco commented 3 years ago

My guess is that this is happening because j will "fail" if it cannot be moved. This is most noticeable in macros and mappings.

For example, if you do :let @q = "G3j:echo 'DONE'\<cr>" and then do @q, it will always (1) jump to the end of the file, (2) attempt to jump down three lines (this will fail because you're already at the end). Thus, the rest of the sequence is not executed because 3j fails.


This is how vim has always behaved, but since you will most likely find this disagreeable, then you can certainly create a function and map to that function. Then use setcurpos() and getcurpos() and line('$') appropriately,

Also! (Bonus) <cmd> can be handy: nnoremap <leader>T <cmd>echo 'test'<cr>


I see you've also made acquaintance with romainl on freenode/#vim :) don't mind him.