Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

vim

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>
like image 379
Hubro Avatar asked Sep 03 '25 14:09

Hubro


2 Answers

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.

like image 106
Thomas Möbius Avatar answered Sep 05 '25 08:09

Thomas Möbius


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>
like image 37
Anton Kovalenko Avatar answered Sep 05 '25 09:09

Anton Kovalenko