How can I execute the current line as Vim EX commands?

后端 未结 8 1564
渐次进展
渐次进展 2021-02-02 11:17

Say I\'m editing my _vimrc file and I\'ve just added a couple of lines, for instance a new key mapping. I don\'t want to reload the whole file (:so %

8条回答
  •  野趣味
    野趣味 (楼主)
    2021-02-02 11:32

    If you're doing a lot of experimenting (trying things out that you might want to add to your vimrc, I assume?) it might help to do so in a scratch file like experimental.vim so you aren't just relying on your history to know what you're trying out. Now that you have these great mappings, it will be easy to rerun things from experimental or vimrc without sourcing the whole file.

    Also (sorry, I can't comment on answers yet, it seems), I tried this mapping of Peter's:

    vnoremap es :exec join(getline("'<","'>"),'')
    

    This works in most cases, but it fails specifically on function definitions.

    function! TestMe()
      echo "Yay!"
    endfunction
    

    This mapping joins the lines into a single string, separated by and then execs them.

    I'm not entirely sure why, but if I try to do that with a function definition in normal mode:

    :exec 'function! TestMe()|  echo "Yay!"|endfunction'
    -> E488: Trailing characters
    

    After some testing, I've found that it will work with newline separators instead:

    :exec "function! TestMe()\n  echo 'Yay!'\nendfunction"
    :call TestMe()
    -> Yay!
    

    So, I've changed my mapping to this:

    vnoremap es :exec join(getline("'<","'>"),"\n")
    

    I suppose there is a vim or ex reason why the method doesn't work on functions (maybe even some setting I have on?), and I'm curious to hear what it is if someone knows.

提交回复
热议问题