Custom Vim Functions to Format Your Text

-range ToTuple <line1>,<line2> call ToTupleFunction()This function will not only format your text, but also copy the result to your clipboard so you can paste it in whatever SQL query editor you use.

Let’s break down each line of the body of the function.

silent execute a:firstline .

“,” .

a:lastline .

“s/^/’/”For all visually selected lines, the line above jumps to the beginning of each line and inserts a single quotation mark.

silent execute a:firstline .

“,” .

a:lastline .

“s/$/’,/”This line goes to the end of each line and inserts a single quotation mark and comma.

The next line of code joins all the lines of text we have so far into one line:silent execute a:firstline .

“,” .

a:lastline .

“join”Now we add an open parenthesis at the beginning of the line:silent execute “normal I(“And then insert the closing one:silent execute “normal $xa)”The last line of the function selects the entire text and copies it to the clipboard (I have a custom mapping for copying to the clipboard: vnoremap YY "*y).

At last, here’s the function in action:If you’d like to have a similar function that creates an array instead, you need only make a small change to the ToTupleFunction and give the function a new name.

“ convert rows of numbers or text (as if pasted from excel column) to an arrayfunction!.ToArrayFunction() range silent execute a:firstline .

“,” .

a:lastline .

“s/^/’/” silent execute a:firstline .

“,” .

a:lastline .

“s/$/’,/” silent execute a:firstline .

“,” .

a:lastline .

“join” " these two lines below are different by only one character!.silent execute “normal I[“ silent execute “normal $xa]”command!.-range ToArray <line1>,<line2> call ToArrayFunction()And that’s it!.Hope this was helpful and if you guys have any cool Vim functions you use a lot for writing code or just for writing in general, let me know in the comments!If you like what I’ve written here, make sure to check out my personal blog where I have articles that aren’t available on Medium.

Originally published at bobbywlindsey.

com on July 30, 2017.

.. More details

Leave a Reply