Manipulation of strings in vim

In my opinion Vimscript has not a lot of features for manipulating strings. I often use matchstr(), substitute() en less often strpart(). Maybe there's more than that.

What is p.e. the best way to remove all text between linenumbers in the next string "a"?

let a = "\%8l............\|\%11l..........\|\%17l.........\|\%20l...." --> etc 

I want to keep only the digits and put them in a list. (the text between linenumbers can be diffent)

EDIT: output I want to obtain: ['8', '11', '17', '20'] --> etc

Answers


You're looking for split()

echo split(a, '[^0-9]\+') 

EDIT:

Given the new constraint: only the numbers from \%d\+l, I'd do:

echo map(split(a, '|'), "matchstr(v:val, '^%\\zs\\d\\+\\zel')")

NB: your vim variable is incorrectly formatted, to use only one backslash, you'd need to write your string with single-quotes. With double-quotes, here you'd need two backslashes. So, with

let b = '\%8l............\|\%11l..........\|\%17l.........\|\%20l....'

it becomes

echo map(split(b, '\\|'), "matchstr(v:val, '^\\\\%\\zs\\d\\+\\zel')")

One can take advantage of the substitute with an expression feature (see :help sub-replace-\=) to run over all of the target matches, appending them to a list.

:let l=[] | call substitute(a, '\\%\(\d\+\)l', '\=add(l,submatch(1))[1:0]', 'g')

Need Your Help

"Favorites" or "macros" in SQL Server Management studio?

sql-server sql-server-2005 ssms

I have a few databases that I always use SQL Server Management Studio with. I'd like to be able to create a toolbar button or keyboard shortcut that automatically opens a new query window (in the c...

What is the easiest or fastest way to make CSS render the same in all browsers

html css styles browser

Making a web page display correctly im all major browsers today is a very time consuming task.