This VIM Ex command is very helpful in database programming when you need to take a very large list of items and put each item in quotes and separate by a comma so that the list can simply be cut and pasted into your SQL WHERE clause.
Here is the regex:
%s/[ \t]*// | %s/[ \t]*$// | %s/.*/'\0',/g
--trim beg-----trim end------put into single quotes with comma at end
And here is why this is very useful
A list which is cut and pasted from another source may be in a form like the below …
apples
oranges
bananas
pears
*Note: If you copied a list from another source you probably have whitespace before and after your word on each line (as above) and this command will strip that out so that each word will line up in the first column.
…and you need to quickly put this list into a form that can be used in a SQL statement WHERE clause such as:
WHERE fruit in (
‘apples’,
‘oranges’,
‘bananas’,
‘pears’
)
*Note: Your last line will actually have the quotes and comma (i.e. ‘pears’,) but it is a quick and simple manual touch up required to finalize your WHERE clause.
Comments
(There are currently no comments for this post.)
(Comments are disabled for this post.)