2

Update: It is possible with '<,'>source.

I figured it out right after writing this question, no need for the complicated workaround. I am gonna still let this question here and not delete it, maybe this will help someone out :)

Question

Is it possible to select the VimL part and execute it somehow?

Setup

random.txt file:

this is just a random text file
for example here are my notes
hello world

My VimL:
for i in range(3)
    echo i
endfor

some more useless text
end of this useless file

Note: This is just a text file, not a vim file


My Idea

This is a workaround that works, but isn't too fancy:

'<,'>w! /tmp/%:t.vim | source /tmp/%:t.vim

Explanation:

  • '<,'>: Is the selected text as a range
  • w!: write command with ! to overwrite in case file exists already
  • /tmp/%:t: This is probably the most complicated thing, but here it is broken down:
  • /tmp: dir to write the file into
  • %:t: takes the current filename, but only the tail (/home/user/Downloads/random.txt -> random.txt)
  • .vim: add a .vim extension. Not needed, but makes it more clear
  • source /tmp/%:t.vim: finally source the temporary file


Thank you for your help :)

4
  • If you have a solution better post it like an answer to your own question. If you like it you can validate your own answer such that the question can rest. If other have idea they can post answer next to yours. Commented Jan 19, 2023 at 15:57
  • @VivianDeSmedt Oh I know you, you have commented on one of my other posts. And you are absolutely right, I should do that. From now on I am gonna do that, but do you want to do that for this question? I could then accept the answer immediately and you are gonna get +12 rep. A win-win :)
    – Bog
    Commented Jan 19, 2023 at 16:01
  • I'm fine doing it but you can also accept your own answer after 1 day of delay and I would not get the point I didn't deserved ;-) Commented Jan 19, 2023 at 16:27
  • 1
    Okay your choice haha ^^ In my opinion you deserved it, because you help others out with comments, and comments don't bring you any reputation and it would be deserved to give you some reputation for that... But okay :)
    – Bog
    Commented Jan 19, 2023 at 16:47

1 Answer 1

2

Very simple command that works:

'<,'>source

Explanation:

  • '<,'>: that is the range. See this or :help '<
  • source: that is the ex-command to run VimL / Vimscript

Simple way:

  • Select text you want to run
  • Press : in normal mode
  • Type source or so for short

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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