I integrated luacheck in vim with the following simple function:

function! LuaCheck()
  let errors=system("luacheck " . expand("%"))
  cexpr errors
  cwindow 5
autocmd FileType lua nnoremap <buffer> <c-f> :call LuaCheck()<cr>
autocmd FileType lua set efm=%f:%l:%c:\ %m

It is working, but here is a problem: luacheck doesn't have interactive mode, it works only with files. So I have to use expand("%") instead of getline(1, "$"). When I changed something in buffer I need to save it before checking - it is bad practice. I want to create a temporary file, save content of buffer to it, check temporary file and remove it. But I don't know how to create the temporary file in vimscript. Is this possible? Or I need use external tools for it?

2 Answers 2


You can create temporary file name with :h tempname():

let tempname = tempname()
call writefile([ "hello world" ], tempname)
" do something with tempname file
" ...
call delete(tempname)

There are different approaches to this problem. Assuming your Vim has('job') you can do:

let temp = bufadd('noname')
call setbufvar(temp, '&buftype', 'nofile')
call bufload(temp)
let job = job_start('luacheck', #{in_io: 'buffer', in_buf: bufnr(),
    \ out_io: 'buffer', out_buf: temp,
    \ exit_cb: {id, status -> execute(printf('cb %d | bw %d | cw 5', temp, temp))}
    \ })
  • Consider adding err_io: "buffer", err_buf: l:buffer to the job options and simplifying the callback to execute("sb" . temp). Commented Jul 8, 2021 at 22:30

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.