31

I have a vim script I'm developing in my current buffer and I want to execute it. Is there a simple way to do it?

After a long search I have only found two related ways, but not exactly what I'm looking for:

  • a) "source" command - but to use it I first need to save the content to file and then "source" it back, which doesn't look simple

  • b) "call" command - but I don't want to call my function, I want to execute the whole file, which defines several functions and has some code outside of functions at all

kenorb
  • 155,785
  • 88
  • 678
  • 743
lithuak
  • 6,028
  • 9
  • 42
  • 54

2 Answers2

42

Use:

:%y"                      (alternatively: ggyG)
:@"

It will copy whole buffer into default register and then run it.

See :help :y and :help :@ (and also :help range maybe).

Benoit
  • 76,634
  • 23
  • 210
  • 236
  • 2
    @izhak: You are welcome. If you want to know more see [this answer](http://stackoverflow.com/questions/3997078/how-do-you-paste-text-into-a-vim-colon-command/3997110#3997110) I made to another question. – Benoit Mar 16 '11 at 14:18
  • The answer given is a complicated way to do this. The command listed as "alternate" doesn't work. See my answer on this page for a way to do it in a single, simpler command. – jm3 Jul 29 '13 at 23:11
  • 2
    @jm3 Why do you say the alternate command doesn't work? It yanks the whole buffer into the " register, right? –  Oct 27 '15 at 09:48
  • is there anyway to create some sort of alias for this? – Enzo Dtz Jan 30 '22 at 02:46
4

An autocmd to source the file whenever you save might help:

autocmd BufWritePost <buffer> source %
Raimondi
  • 5,163
  • 31
  • 39