我的.vimrc
文件包含以下行:
let read_path = '/var/www/html/readContent.html'
function ReadContentProcess()
if (expand('%:p') == read_path)
call feedkeys("\<C-A>")
call feedkeys("\<C-V>")
endif
endfunction
当我运行call ReadContentProcess()
时,我收到以下错误:
Error detected while processing fucntion ReadContentProcess:
Line 1:
E121: Undefined variable: read_path
E15: Invalid expression: (expand('%:p') == read_path)
为什么呢?我已将read_path
定义为变量,为什么vim告诉我它不存在?
答案 0 :(得分:3)
变量具有默认范围。在函数外部定义时,它具有全局范围g:
。在函数内部,它具有局部范围l:
。因此,您需要通过在read_path
前加g:
let read_path = '/var/www/html/readContent.html'
function ReadContentProcess()
if (expand('%:p') == g:read_path)
call feedkeys("\<C-A>")
call feedkeys("\<C-V>")
endif
end function
来自:help g:
(以及之后的部分)
global-variable g:var g: Inside functions global variables are accessed with "g:". Omitting this will access a variable local to a function. But "g:" can also be used in any other place if you like. local-variable l:var l: Inside functions local variables are accessed without prepending anything. But you can also prepend "l:" if you like. However, without prepending "l:" you may run into reserved variable names. For example "count". By itself it refers to "v:count". Using "l:count" you can have a local variable with the same name.