Nvim 的 :help
頁面,從 原始碼 使用 tree-sitter-vimdoc 解析器 產生。
{name}
不是一個表達式,您不能使用作為函式參考的變數。您可以使用這個骯髒的技巧來列出以變數 "Funcref" 參考的函式。let g:MyFuncref = Funcref func g:MyFuncref unlet g:MyFuncref:fu[nction] /{pattern} 列出名稱與
{pattern}
匹配的函式。範例:列出所有以 "File" 結尾的函式。:function /File$
:verbose function SetFileTypeSH function SetFileTypeSH(name) Last set from /usr/share/vim/vim-7.0/filetype.vim
{name}
([arguments]) [range] [abort] [dict] [closure] 定義一個名為 {name}
的新函式。函式的主體會在後續的行中出現,直到匹配的 :endfunction 為止。{arguments}
的資訊,請參閱 function-argument。:function! Foo() : let x = 0 : function! Bar() closure : let x += 1 : return x : endfunction : return funcref('Bar') :endfunction :let F = Foo() :echo F()
:echo F()
:echo F()
{name}
刪除函式 {name}
。{name}
也可以是 字典 條目,它是 函式引用。:delfunc dict.init
return 'some text' .. ' some more text'
return 'some text' \ .. ' some more text'
:function
宣告的函式,不適用於 lambda 表達式 expr-lambda。function Something(key, value = 10) echo a:key .. ": " .. a:value endfunction call Something('empty') "empty: 10" call Something('key', 20) "key: 20"參數預設表達式會在函式呼叫時進行評估,而不是在定義函式時進行評估。因此,可以使用在定義函式時無效的表達式。這些表達式也僅會在呼叫期間未指定參數時才會被評估。
:function Okay(mandatory, optional = a:mandatory) :endfunction不起作用的範例:
:function NoGood(first = a:second, second = 10) :endfunction
:function Table(title, ...) : echohl Title : echo a:title : echohl None : echo a:0 .. " items:" : for s in a:000 : echon ' ' .. s : endfor :endfunction然後可以使用以下方式呼叫此函式:
call Table("Table", "line1", "line2") call Table("Empty Table")若要傳回多個值,請傳回 列表。
:function Compute(n1, n2) : if a:n2 == 0 : return ["fail", 0] : endif : return ["ok", a:n1 / a:n2] :endfunction然後可以使用以下方式呼叫此函式:
:let [success, div] = Compute(102, 6) :if success == "ok" : echo div :endif
{name}
([引數]) 呼叫函式。函式的名稱和其引數由 :function
指定。最多可以使用 20 個引數。傳回值會被捨棄。如果沒有範圍且函式接受範圍,則函式會被呼叫一次。當給定範圍時,游標會在執行函式之前定位在第一行的開頭。當給定範圍且函式本身不處理它時,函式會針對範圍中的每一行執行,游標位於該行的第一欄。游標會留在最後一行 (可能被最後一次函式呼叫移動)。引數會針對每一行重新評估。因此,這會起作用:function-range-example:function Mynumber(arg) : echo line(".") .. " " .. a:arg :endfunction :1,5call Mynumber(getline("."))
:function Cont() range : execute (a:firstline + 1) .. "," .. a:lastline .. 's/^/\t\\ ' :endfunction :4,8call Cont()
:4,8call GetDict().method()
:eval
。它不支援範圍,但允許方法鏈接,例如:eval GetList()->Filter()->append('$')也可以在評估表達式時或當它被用作方法時呼叫函式
let x = GetList() let y = GetList()->Filter()
try
/ finally
區塊來完成,但當有多個時,這會變得複雜。defer
。它會在函式傳回時排程函式呼叫,無論是否有錯誤。範例func Filter(text) abort call writefile(a:text, 'Tempfile') call system('filter < Tempfile > Outfile') call Handle('Outfile') call delete('Tempfile') call delete('Outfile') endfunc如果某些原因導致函式中止,這裡的 'Tempfile' 和 'Outfile' 將不會被刪除。可以使用
:defer
來避免這種情況func Filter(text) abort call writefile(a:text, 'Tempfile') defer delete('Tempfile') defer delete('Outfile') call system('filter < Tempfile > Outfile') call Handle('Outfile') endfunc請注意,刪除 "Outfile" 的排程是在呼叫
system()
之前,因為即使 system()
失敗,它也可以被建立。func Useless() abort for s in range(3) defer execute('echomsg "number ' .. s .. '"') endfor endfunc現在
:messages
顯示:number 2 number 1 number 0:finish
快速退出腳本。這會使 Vim 啟動更快。然後自動命令應該再次載入同一個檔案,設定一個變數以跳過 :finish
命令。:au FuncUndefined BufNet* source ~/vim/bufnetfuncs.vim檔案 "~/vim/bufnetfuncs.vim" 應該定義以 "BufNet" 開頭的函式。另請參閱 FuncUndefined。
:call filename#funcname()當呼叫這樣的函式,且尚未定義時,Vim 會在 'runtimepath' 中的 "autoload" 目錄中搜尋名為 "filename.vim" 的腳本檔案。例如 "~/.config/nvim/autoload/filename.vim"。該檔案應該像這樣定義函式
function filename#funcname() echo "Done!" endfunction如果檔案不存在,Vim 也會在 'packpath'(在 "start" 下)中搜尋,以允許在套件尚未新增到 'runtimepath' 時,從你的 vimrc 呼叫套件的函式(請參閱 套件)。
:call foo#bar#func()Vim 會在 'runtimepath' 中搜尋檔案 "autoload/foo/bar.vim"。
:let l = foo#bar#lvar但是,當自動載入腳本已經載入時,它不會再次為未知的變數載入。
:let foo#bar#toggle = 1 :call foo#bar#func()請注意,當你犯錯並呼叫應該在自動載入腳本中定義的函式,但該腳本實際上沒有定義該函式時,你會收到遺失函式的錯誤訊息。如果你修復了自動載入腳本,它不會自動再次載入。重新啟動 Vim 或手動載入腳本。