防止在vim中重复使用hjkl移动键
由于我经常不使用vim提供的优秀的动作和文本对象(自
“Holding down ‘j’ is a vim anti-pattern,”以来),我希望vim可以帮助我训练使用这些,而不是连续使用hjkl。
当我开始使用vim时,我很烦,我没有使用hjkl移动,而是使用箭头键。提醒我不要这样做,我重新点了箭头键,让自己不要使用它 – 我知道使用主排导航将是一个更好的长期计划,所以我删除了正确的强化,我会得到工作箭头键。 map <left> <nop> map <right> <nop> # I quickly removed nop's for up and down because using # the mouse wheel to scroll is sometimes useful 我不再需要我的.vimrc中的这些映射,因为它的效果非常好,我很快就切换了,而不用有意识的去做。以类似的方式,我现在也想切断我重复使用基本的移动键hjkl。我在想像这样: let g:last_mov_key = 'none' let g:times_mov_key_repeated = 0 function! MovementKey(key) if (g:last_mov_key == a:key) let g:times_mov_key_repeated = g:times_mov_key_repeated + 1 else let g:last_mov_key = a:key let g:times_mov_key_repeated = 0 endif if g:times_mov_key_repeated > 3 echo "Negative Reinforcement!" endif endfunction noremap j :call MovementKey('j')<CR>gj noremap k :call MovementKey('k')<CR>gk noremap h :call MovementKey('h')<CR>h noremap l :call MovementKey('l')<CR>l 但是这在视觉模式中突破,我想象在其他一些情况下,在某些情况下使用vim命令行来改变状态。我如何限制自己不得不使用更复杂的动议? 编辑:问题编辑后的前两个答案为清晰,段落重新排序。我想要一些vim的帮助,超越罗曼的“0级”。到目前为止,答案建议我不要浪费我的时间,但如果我们假设我是学习技术的粉丝,我改变我的习惯,改变我的环境来改变激励机制?在这个模型中,我想完成一个任务,比如滚动页面,我会或多或少地随机尝试组合键,直到我完成这个任务。我脑中的多巴胺信号传导等将加强最终达到此结果的作用。我可以专注于记住不要使用hjkl,或者我可以专注于我原本想要做的任务,编辑文本,而没有真正考虑到它会发现自己使用更有效的编辑技术。
我想提出一个更符合OP的思维方式的解决方案。
我也决定阻止某些动议,如果不在之前的一个计数是一个好主意,如here所讨论的。我也尝试了一个简单的直接重新映射,但是这在OP的描述中在许多情况下都无效。 但是我终于想出了一个使用key maps that returned an expression的方法: function! DisableIfNonCounted(move) range if v:count return a:move else " You can make this do something annoying like: " echoerr "Count required!" " sleep 2 return "" endif endfunction function! SetDisablingOfBasicMotionsIfNonCounted(on) let keys_to_disable = get(g:,"keys_to_disable_if_not_preceded_by_count",["j","k","l","h","gj","gk"]) if a:on for key in keys_to_disable execute "noremap <expr> <silent> " . key . " DisableIfNonCounted('" . key . "')" endfor let g:keys_to_disable_if_not_preceded_by_count = keys_to_disable let g:is_non_counted_basic_motions_disabled = 1 else for key in keys_to_disable try execute "unmap " . key catch /E31:/ endtry endfor let g:is_non_counted_basic_motions_disabled = 0 endif endfunction function! ToggleDisablingOfBasicMotionsIfNonCounted() let is_disabled = get(g:,"is_non_counted_basic_motions_disabled",0) if is_disabled call SetDisablingOfBasicMotionsIfNonCounted(0) else call SetDisablingOfBasicMotionsIfNonCounted(1) endif endfunction command! ToggleDisablingOfNonCountedBasicMotions :call ToggleDisablingOfBasicMotionsIfNonCounted() command! DisableNonCountedBasicMotions :call SetDisablingOfBasicMotionsIfNonCounted(1) command! EnableNonCountedBasicMotions :call SetDisablingOfBasicMotionsIfNonCounted(0) DisableNonCountedBasicMotions 请注意,代码是为了方便而包含在内,但是我也会查看gist以查看是否有更新/修复。 你可以放宽水平运动的约束,或者 let g:keys_to_disable_if_not_preceded_by_count = ["j","k"] 另外,如gist所述,您可能会发现在?/ .vimrc以及上面的代码中添加类似的东西非常有用。 set number if has('autocmd') augroup vimrc_linenumbering autocmd! autocmd WinLeave * if &number | set norelativenumber | endif autocmd BufWinEnter * if &number | set relativenumber | endif autocmd VimEnter * if &number | set relativenumber | endif augroup END 或者安装插件线vim-numbers。 即使这个问题(OP)对另一种方法的满意度有所回答,我在这里添加了解决方案,以防任何人在寻找不同的东西。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |