2

Following on from Put variable to line above current line VIm

I added some logic to only run if the begin and end tags in the list are present, snippet

....
let findStart='<'.taglist[s1].'>'
let findEnd='<'.'\/'.ns.taglist[s1].'>'

let fs=0
let fe=0


while something
  let fs=search(expand(findStart))
  let fe=search(expand(findEnd))
  if ( &fs>0 && &fe>0 )
      call search(findStart,'b')
      "paste the beginning fold
      :-1put =foldbegin
      .....
      :let fs=0
      :let fe=0
  endif 
let something=something +1
endwhile

In modeline this :let fs=0|:call search('<')|echo fs returns the position of the search. In Vimscript the only way I can use the variable in an if statement is if ( &fs>0 ... ).

I can't find anything to explain why the statements work differently so I can make sense of it. Any suggestions of where to look for answers?

Also :let fs=0 within the if doesn't reset the variable. Why?

UPDATE: It's something to do with using 2 letter variables but I don't know what the issue is despite googling it.

  • 1
    You should use scoping for variables. For example let l:fs = 0 makes fs as a local variable for your function and prevent you from interact with global, options etc. – Alex Kroll Sep 9 '15 at 7:23
  • Variables inside a function are local by default. You have to use g: to access global variables. – jamessan Sep 9 '15 at 20:40
2

It would help, if you could include the error message in your question.

When using '&variables' you are effectively accessing option values. See the help at :h expr-option.

So in your if statement you should not use if &fs if you want to check a variable and not the option value 'fsync', but using &fe will cause an error, because there exists no option with that (short-)name.

  • No error message I could trap but that points me to the right place to look thanks. It explains why &fe was never true. – Steve Sep 9 '15 at 6:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.