In C programming language, it's typical to log a message with filename and line number of the source code file the message is coming from:

#define log(fmt, ...)  printf("%s:%d: " fmt "\n", __file__, __LINE__, ##__VA_ARGS__);

I would want to do that in vimscript.

function! log(msg) abort
    echom previous_vimscript_name().':'.previous_vimscript_line().': '.msg

How would I do that? echom only prints the message, I would want to print where it comes from.

1 Answer 1


Found it! You can use expand('<sfile>') and expand('<sflnum>') see h sfile for all the expanded stuff you can use. But instead use:

function! log(msg) abort
    echom expand('<stack>').': '.msg

stack will give you the whole nice looking stack:

/home/kamil/.config/nvim/init.vim[6]../home/kamil/.vimrc[464]..function kc#log: Hello world

I settled for the following, to also remove function kc#log from the output:

echom substitute(expand("<sfile>"), '\.\.[^.]*$', "", "").": ".a:str

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.