How to map a sequence in vim conditionally to run external programs without printing the else clause

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-24 01:40:14

问题


How can I map a sequence in vim conditionally to run any of two external programs in such way that the screen is not cleared to show the else clause?

For example:

:nmap <c-l> :if filereadable('Makefile')<CR>!make<CR>else<CR>!ls<CR>endif<CR>

ctrl+m executes make but then clears the screen and prints the following at the bottom of it:

:  else
:  !ls
:  endif
Press ENTER or type command to continue

回答1:


You can use an expression mapping (:help map-expr)

:nnoremap <expr> <c-m> filereadable('Makefile') ? ':make<CR>' : ':!ls<CR>'

Notes:

  • You should use :noremap; it makes the mapping immune to remapping and recursion.
  • <C-m> is the same as <CR>; there's currently no way to distinguish the two; better use different keys. See this answer for more information.



回答2:


you need map <expr>

e.g.:

nnoremap <expr> <c-t> line('.')>=6? ':!ls<cr>' : ':!seq 10<cr>'

in your example:

:nnoremap <expr> <c-m> filereadable('Makefile') ? ':make<CR>' : ':!ls<CR>'

for detail info:

:h :map-<expr> 

note that, if you map <c-m>, the Enter will follow that mapping too. better use another key combination, unless you intend to do so.



来源:https://stackoverflow.com/questions/15781615/how-to-map-a-sequence-in-vim-conditionally-to-run-external-programs-without-prin

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!