Vimscript - 我如何检查变量中是否存在字符
Vimscript - how can i check if a character is present in a variable
我正在尝试制作一个 returns 仅 getcwd()
值的一部分的函数。
所以,我想要的是
function! FileDir()
let file_dir == getcwd()
if file_dir "has more than three backlashes"
return 'mycommand'
else
return 'mycommand2'
endfunction
所以我想添加一些东西来做这个:如果 getcwd() returns 超过三个目录,比如:/home/hrq/project-folder
,我想添加一个特定的 if
命令它。如果 returns 两个或更少的文件夹,比如 /home/hrq
,我想添加一个 else
命令,稍后我会添加它们。
所以我虽然也许我可以检查它是否有,比如,在 getcwd()
的输出中有三个反光
我尝试使用 match
表达式、max
、len
,但我做不到。
其实很简单:
let l:slash = exists('+shellslash') && !&shellslash ? '\' : '/'
if count(getcwd(), l:slash) > 3
...
else
...
endif
我正在尝试制作一个 returns 仅 getcwd()
值的一部分的函数。
所以,我想要的是
function! FileDir()
let file_dir == getcwd()
if file_dir "has more than three backlashes"
return 'mycommand'
else
return 'mycommand2'
endfunction
所以我想添加一些东西来做这个:如果 getcwd() returns 超过三个目录,比如:/home/hrq/project-folder
,我想添加一个特定的 if
命令它。如果 returns 两个或更少的文件夹,比如 /home/hrq
,我想添加一个 else
命令,稍后我会添加它们。
所以我虽然也许我可以检查它是否有,比如,在 getcwd()
我尝试使用 match
表达式、max
、len
,但我做不到。
其实很简单:
let l:slash = exists('+shellslash') && !&shellslash ? '\' : '/'
if count(getcwd(), l:slash) > 3
...
else
...
endif