Vim Tips Wiki
(Move categories to tip template)
(Change <tt> to <code>, perhaps also minor tweak.)
 
(6 intermediate revisions by 4 users not shown)
Line 4: Line 4:
 
|previous=1228
 
|previous=1228
 
|next=1230
 
|next=1230
|created=May 12, 2006
+
|created=2006
 
|complexity=intermediate
 
|complexity=intermediate
 
|author=DO
 
|author=DO
Line 16: Line 16:
 
So, it is reasonable to make a mapping:
 
So, it is reasonable to make a mapping:
 
<pre>
 
<pre>
noremap &lt;silent&gt;&lt;buffer&gt; &lt;F9&gt; :exec 'source '.bufname('%')&lt;CR&gt;
+
noremap <silent><buffer> <F9> :exec 'source '.bufname('%')<CR>
 
</pre>
 
</pre>
   
You may to place this line into file <tt>''{runtimepath}''/ftplugin/vim.vim</tt>, to use this mapping for Vim files only.
+
You may to place this line into file <code>''{runtimepath}''/ftplugin/vim.vim</code>, to use this mapping for Vim files only.
   
 
==Comments==
 
==Comments==
Line 28: Line 28:
   
 
----
 
----
  +
Neither of the above works unless you save the file first. I use
  +
<pre>
  +
nmap <C-A> :w<CR>:so %<CR>
  +
</pre>
  +
  +
----
  +
When developing a function it is sometime useful to source only a part of a file. The following function dumps a range in a file and source it:
  +
<pre>
  +
function! SourceRange() range
  +
let tmpsofile = tempname()
  +
call writefile(getline(a:firstline, a:lastline), l:tmpsofile)
  +
execute "source " . l:tmpsofile
  +
call delete(l:tmpsofile)
  +
endfunction
  +
command! -range Source <line1>,<line2>call SourceRange()
  +
</pre>
  +
  +
Then, for sourcing a selection:
  +
<pre>
  +
:'<,'>Source
  +
</pre>
  +
  +
Or, for sourcing the whole buffer:
  +
<pre>
  +
:%Source
  +
</pre>
  +
  +
  +
----
  +
Alternatively, and leveraging more of the existing functionality, just (y)ank the range and execute it with
  +
<pre>
  +
:@"
  +
</pre>

Latest revision as of 06:14, 13 July 2012

Tip 1229 Printable Monobook Previous Next

created 2006 · complexity intermediate · author DO · version 6.0


When you edit Vim script you often need to make a small change, then test some function, then make some another small change and so on. It is not convenient to restart Vim every time, and it is not convenient to run it from Ex command line.

So, it is reasonable to make a mapping:

noremap <silent><buffer> <F9> :exec 'source '.bufname('%')<CR>

You may to place this line into file {runtimepath}/ftplugin/vim.vim, to use this mapping for Vim files only.

Comments[]

It is quite convenient to run it from a command line:

:so %

Neither of the above works unless you save the file first. I use

nmap <C-A> :w<CR>:so %<CR>

When developing a function it is sometime useful to source only a part of a file. The following function dumps a range in a file and source it:

function! SourceRange() range
  let tmpsofile = tempname()
  call writefile(getline(a:firstline, a:lastline), l:tmpsofile)
  execute "source " . l:tmpsofile
  call delete(l:tmpsofile)
endfunction
command! -range Source <line1>,<line2>call SourceRange()

Then, for sourcing a selection:

:'<,'>Source

Or, for sourcing the whole buffer:

:%Source



Alternatively, and leveraging more of the existing functionality, just (y)ank the range and execute it with

:@"