ViM: how to put a string from an input dialog into a list

VIM: Does anyone know how to put a string from an input dialog into a list?

pe :.

string "3,5,12,15"

in

list item[1] = 3 list item[2] = 5 list item[3] = 12 etc. 

and how can I find out how many list items are there?

+4
source share
2 answers

From :h E714

 :let l = len(list) " number of items in list :let list = split("abc") " create list from items in a string 

In your case

 let string = "3,5,7,19" let list = split(string, ",") echo len(list) 
+4
source

Use the split , len and empty functions:

 let list=split(string, ',') let list_length=len(list) " If all you want is to check whether list is empty: if empty(list) throw "You must provide at least one value" endif 

Please note that if you want to get a list of numbers from a string, you will have to use a map to convert list items to numbers:

 let list=map(split(string, ','), '+v:val') 

In most cases, you can expect strings to be converted to numbers, but sometimes such a conversion fails.

+2
source

Source: https://habr.com/ru/post/1345427/


All Articles