7

I can source a file, e.g. my vimrc with :so % or :so <filename>. But what do I do when I need to source only part of a file. I want some way to visually select a portion of the file with :h v and then source it. Kinda like emacs's M-x eval-region

  • 1
    Doesn't work in all cases, but :@* – Mass Apr 5 at 14:33
8

After you've done your Visual selection run this:

y:@"<CR>

y copies the selection to the unnamed register (") since we didn't explicitly name a register. Then :@" executes the contents of that register as Ex commands.

See help :@ and https://stackoverflow.com/questions/20262519/vim-how-to-source-a-part-of-the-buffer

  • So, you're saying I should just copy and paste it in my command-line window? That's... easier than I thought. – klaus Apr 5 at 14:59
  • You're not pasting to the command line, which would entail something like reading the data and storing it in an input buffer. You're just telling vim to process the data from an existing memory allocation (the register store) which is a lot less overhead. – B Layer May 13 at 9:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.