How can I execute the current line as Vim EX comma

2020-05-20 04:36发布

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 %) since that will reset a lot of temporary stuff I'm experimenting with. I just want to run the two lines that I'm currently working on.

I'm having no luck trying to copy/paste the lines into the command buffer, since I can't use the put command in there. Is there any way I could run the current line (or current selection) as EX commands?


Summary:

After Anton Kovalenko's answer and Peter Rincker's comment I ended up with these key maps, which either executes the current line, or the current selected lines if in visual mode:

" Execute current line or current selection as Vim EX commands.
nnoremap <F2> :exe getline(".")<CR>
vnoremap <F2> :<C-w>exe join(getline("'<","'>"),'<Bar>')<CR>

标签: vim
7条回答
\"骚年 ilove
2楼-- · 2020-05-20 05:17

To execute the current line as an ex command, you may also use:

yy:@"

This will yank the current line to the "-register and execute it. I don't think it is too much typing.

查看更多
Evening l夕情丶
3楼-- · 2020-05-20 05:21

The accepted answer doesn't handle continuation sections. Also, surprisingly, the bar isn't needed, newlines are fine. This will work, first yanking the text into register x:

vno <c-x> "xy:exe substitute(@x,"\n\\",'','g')<cr>

As someone has already mentioned, the only exception are commands that "eat up" newlines. Eg, executing the above mapping on:

:sign define piet text=>> texthl=Search 
:exe ":sign place 2 line=23 name=piet file=" . expand("%:p")

will cause vim to to think that the user is trying to define textl as "Search\n:exe ":sign place... etc.

查看更多
甜甜的少女心
4楼-- · 2020-05-20 05:22

I don't want to reload the whole file (:so %) since that will reset a lot of temporary stuff I'm experimenting. I just want to run the two lines that I'm currently working on.

If you want to execute a command because you want to refine it before committing it to _.vimrc, then you should launch a Command Line Window for Ex-mode commands with q:.

At launch the Command Line Window is buffered with the contents of the command line history. It is a normal Vim window the contents of which can be edited as any text buffer with the exception of pressing on any line which executes the command on that line. It is very useful when you want to slightly change a long, complex command you wrote earlier and re-run it.

To launch a 'Command Line Window' for search strings press q/.

查看更多
时光不老,我们不散
5楼-- · 2020-05-20 05:23

For that purpose, I have defined the following commands and mappings:

":[range]Execute    Execute text lines as ex commands.
"                   Handles |line-continuation|.
" The same can be achieved via "zyy@z (or yy@" through the unnamed register);
" but there, the ex command must be preceded by a colon (i.e. :ex)
command! -bar -range Execute silent <line1>,<line2>yank z | let @z = substitute(@z, '\n\s*\\', '', 'g') | @z

" [count]<Leader>e  Execute current [count] line(s) as ex commands, then
" {Visual}<Leader>e jump to the following line (to allow speedy sequential
"                   execution of multiple lines).
nnoremap <silent> <Leader>e :Execute<Bar>execute 'normal! ' . v:count1 . 'j'<CR>
xnoremap <silent> <Leader>e :Execute<Bar>execute 'normal! ' . v:count1 . 'j'<CR>
查看更多
一夜七次
6楼-- · 2020-05-20 05:30

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 <Leader>es :<c-u>exec join(getline("'<","'>"),'<BAR>')<CR>

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 <BAR> 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 <Leader>es :<c-u>exec join(getline("'<","'>"),"\n")<CR>

I suppose there is a vim or ex reason why the <BAR> 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.

查看更多
▲ chillily
7楼-- · 2020-05-20 05:40

Executing the line under cursor as an Ex command:

:execute getline(".")

Convenient enough for 2 lines. (I'd figure out something for doing it with regions, but I'm not a vim user). And for currently selected region, the following seems to do the job:

:execute getreg("*")

As commented by Peter Rincker, this mapping can be used for executing the currently selected lines:

:vnoremap <f2> :<c-u>exe join(getline("'<","'>"),'<bar>')<cr>
查看更多
登录 后发表回答