Call function when current function is done
Example
Normal 0 / 2Command-line 1 / 2Normal 2 / 2Call function when current function is done.
Script runtime
- Load state
not sourced
Command entered · ready to apply
Script runtime
- Load state
not sourced
Command line
- Typed text
:source defer-demo.vim- Insertion offset
22
:defer appears in the complete source file. add(..., 'cleanup') is deferred until Remember() returns, after the immediate body append.
Script runtime
- g:events
['body', 'cleanup']- Deferred calls
none remaining
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.
function! Remember() abort
let g:events = []
defer add(g:events, 'cleanup')
call add(g:events, 'body')
endfunction
call Remember()
echo g:eventsCursor on line 1, column 1Details & 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.