Vimscript - how can i check if a character is present in a variable

Viewed 352

i am trying to make a function that returns only parts of the getcwd() value.

So, what i wanted, was something like

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

So i want to add something that does this: if getcwd() returns more than three directories, like, let's say: /home/hrq/project-folder, i want to add an specific if command to it. If returns two or less folders, like just /home/hrq, i want to add an else command, that i will add both later.

So i though that maybe i could check if it has, like, three backlases in the output of getcwd()

and i tried with the match expression, the max, the len, but i wasn't able to.

1 Answers

Very simple actually:

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