Vim Recall繁體中文

Call function when current function is done

:defer

Example

Normal 0 / 2

Call function when current function is done.

Script runtime

Load state
not sourced
Before & after

Before

Script runtime

Load state
not sourced

After

Script runtime

g:events
['body', 'cleanup']
Deferred calls
none remaining

Documentation-backed illustration. a Vim build supporting :defer and +eval; source code is prepared as defer-demo.vim. :defer appears in the complete source file. add(..., 'cleanup') is deferred until Remember() returns, after the immediate body append. Primary help: :defer.

Sample buffer
function! Remember() abort
  let g:events = []
  defer add(g:events, 'cleanup')
  call add(g:events, 'body')
endfunction
call Remember()
echo g:events
Cursor on line 1, column 1
Details & Vim help

Call function when current function is done. This Ex reference uses the full command name; the alphabetical index omits arguments. Supply the ranges, arguments, flags and prerequisite state documented in the linked help. Press Enter to submit a complete command. The index spelling :defe[r] puts the optional abbreviation suffix in square brackets; those brackets are not typed. Availability and exact behavior can depend on Vim build features, options, mappings and editor state; the linked help is authoritative.