VIM scripting: How to check if list contains item?

3,377 views
Skip to first unread message

Reckoner

unread,
Nov 15, 2010, 12:37:01 PM11/15/10
to vim_use
If I do

:let y =['a','b']

how would I later check if 'a' is in y

:echo 'a' in y

doesn't work. I want to do something like:

:if ( 'a' in y)
: ....
:endif

Thanks!

Tim Chase

unread,
Nov 15, 2010, 12:47:19 PM11/15/10
to vim...@googlegroups.com, Reckoner
On 11/15/2010 11:37 AM, Reckoner wrote:
> If I do
>
> :let y =['a','b']
>
> how would I later check if 'a' is in y
>
> :echo 'a' in y
>
> doesn't work. I want to do something like:

You can use the index() function:

if (index(y, 'a') >= 0)
...
endif

which you can read about at

:help index()

-tim

reckoner

unread,
Nov 15, 2010, 12:56:36 PM11/15/10
to Tim Chase, vim...@googlegroups.com
Dear Tim Chase:

Thank you! I read the manual section as closely as I could and came up
with nothing.

Thanks!

Tim Chase

unread,
Nov 15, 2010, 1:02:15 PM11/15/10
to reckoner, vim...@googlegroups.com
On 11/15/2010 11:56 AM, reckoner wrote:
> Thank you! I read the manual section as closely as I could and
> came up with nothing.

Glad I could help -- the Vim manual is epic in its scope and
thoroughness, so it's easy to miss what you're looking for if you
don't already have an idea what it's called (even for us more
seasoned vimmers, there are some dark corners of the docs that I
have to :helpgrep to find, wading through a small mountain of hits).

But be cheered -- the mailing list is a generally friendly place
(especially if you don't top-post ;-) with lots of helpful folk.

-tim


Reply all
Reply to author
Forward
0 new messages