problem in vim snippets

39 views
Skip to first unread message

Rudra Banerjee

unread,
Nov 4, 2015, 6:15:51 AM11/4/15
to vim_use
I am using templates for expanding few text, eg.

!File=[:EVAL:]expand('%')[:END:]
!Author=
!Created=[:EVAL:]strftime('%c')[:END:]
!Last Modified=[:EVAL:]strftime('%c')[:END:]
Program [:EVAL:]expand('%:t:r')[:END:]
Implicit None
<++>
End Program [:EVAL:]expand('%:t:r')[:END:]


And I am calling this as:

"let s:plugin_dir = $HOME.'/.vim/bundle/vimf90/'
let s:plugin_dir=filter(split(&rtp, ','), 'v:val =~ "/vimf90"')[0]
let s:templatedir=s:plugin_dir . '/templates/'
function! Prog(arg)
execute 'r ' . s:templatedir . a:arg . '.txt'
%substitute#\[:EVAL:\]\(.\{-\}\)\[:END:\]#\=eval(submatch(1))#ge
endfunction


So, I am expecting to have the snippet called. But what I am getting is:

!****<= A blank line at top
!File=i.f90
!Author=
!Created=Wed 04 Nov 2015 10:54:29 CET
!Last Modified=Wed 04 Nov 2015 10:54:29 CET
Program i
Implicit None
<++>
0 !****<= Dont know why this "0" is coming
End Program i
So, Though this is called properly, I am getting few crap line. Can you kindly help me cleaning them?

Rudra Banerjee

unread,
Nov 4, 2015, 9:16:55 AM11/4/15
to vim_use
Update:
I found the source of error, but not the solution. This is not in the function or template.
It is actually in my map to call the function.
in insertmode, the code is called using:
inoremap `prg <C-R>=Prog("prg")<cr><CR><Esc>gg=G<C-j>

and this is inserting the 0, I dont know why.
If I do,

nnoremap `prg :call Prog("prg")<cr><CR><Esc>gg=G<C-j>

everything is fine.
Kindly help.

Ben Fritz

unread,
Nov 4, 2015, 10:49:41 AM11/4/15
to vim_use

By using <C-R>= in insert mode, you are telling Vim, "insert the result of the expression I give you, into the buffer".

Your function has no return value, so I assume the default return value is zero. Thus Vim inserts zero.

Try making your function return an empty string (""), or stick with methods that don't use the expression register (an expression mapping, perhaps, or just move the <Esc> forward to the beginning of the mapping and use the same method as the normal mode mapping).

Reply all
Reply to author
Forward
0 new messages