I can source a file, e.g. my
: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
After you've done your Visual selection run this:
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.
An alternative implementation which does not stop at the first error in a script:
command! -range=% Exec call execute(getline(<line1>, <line2>), '')
Now in visual mode type
:'<,'>Exec to process the selected lines (
'<,'> is added automatically as usual). Also,
:Exec in normal mode executes the whole buffer, just as
:source %, except the buffer does not need to be saved first.
If needed the mappings, they can be added easily.