【问题标题】:Vimscript - how can i check if a character is present in a variableVimscript - 我如何检查变量中是否存在字符
【发布时间】:2020-12-27 04:19:15
【问题描述】:

我正在尝试创建一个仅返回部分 getcwd() 值的函数。

所以,我想要的是类似

function! FileDir()
    let file_dir == getcwd()
    if file_dir "has more than three backlashes"
       return 'mycommand'
    else 
       return 'mycommand2'
endfunction

所以我想添加一些执行此操作的内容:如果 getcwd() 返回三个以上的目录,例如:/home/hrq/project-folder,我想向其中添加特定的 if 命令。如果返回两个或更少的文件夹,比如/home/hrq,我想添加一个else 命令,我稍后会添加。

所以我也许可以检查它是否在getcwd() 的输出中具有三个反激

我尝试了match 表达式、maxlen,但我做不到。

【问题讨论】:

    标签: vim match string-matching neovim


    【解决方案1】:

    其实很简单:

    let l:slash = exists('+shellslash') && !&shellslash ? '\' : '/'
    if count(getcwd(), l:slash) > 3
        ...
    else
        ...
    endif
    

    【讨论】:

      猜你喜欢
      • 2020-09-25
      • 2010-10-25
      • 1970-01-01
      • 2020-05-28
      • 2014-01-30
      • 2012-07-05
      • 2011-08-18
      • 1970-01-01
      相关资源
      最近更新 更多