CSV to SQL
Before
id 1,Item 1,cost 1,location 1
id 2,Item 2,cost 2,location 2
id 10,Item 10,cost 10,location 10
After
INSERT INTO `database`.`table` (`id` ,`item` ,`cost` ,`location`) VALUES ('id 1','Item 1','cost 1','Location 1');
INSERT INTO `database`.`table` (`id` ,`item` ,`cost` ,`location`) VALUES ('id 2','Item 2','cost 2','Location 2');
INSERT INTO `database`.`table` (`id` ,`item` ,`cost` ,`location`) VALUES ('id 10','Item 10','cost 10','Location 10');
Preview
Command
%<alt-s>"yys\d<enter>
dhhbms
``x_ms(IINSERT INTO `
database<esc>
a.`table<esc>la <esc>
AVALUES (<esc>
"yPS,<enter>ms'A;<esc>Fl;~
-
%
selects full file -
<alt-s>
split selection into multiple selections on newlines -
"yy
yanks the selections into "y" register. We'll need it for later -
s
and then input the pattern\d
then<enter>
which creates a selection on all digits -
d
deletes the selections. Essentially we've removed all the digits. -
hh
goes backwards 2 chars, important to make sure we are at the end of each word -
Use
b
to select till the beginning of every word, which also nicely selects all the words that there are -
ms`
surrounds each word with a backtick -
`
switches all characters to lowercase -
x
selects each line then use_
to trim the trailing whitespace -
ms(
surrounds each line with parentheses -
I
goes into insert mode at the beginning of each line -
Type the following:
INSERT INTO `database
-
<esc>
goes back to normal mode -
a
to go into insert mode after the backtick then type:.`table
-
<esc>
goes back into normal mode, thenla
to enter insert mode just before the opening parentheses -
Add a space
<esc>
to go back into normal mode again -
A
goes into insert mode at the end of each line, now type:VALUES (
-
Hit
<esc>
to leave insert mode. Your cursor will be at the closing parenthesis. -
"yP
pastes our previously yanked items from the "y" register -
S,<enter>
splits current selection into multiple selections on each comma -
ms'
surrounds each item with a single quote -
A;
adds a semicolon at the end of each line -
<esc>
goes back to normal mode andFl
to place your cursor on the lowercase "l" of each "location" -
;
collapses each selection into a single-width selection -
~
toggles the case for each "l" into "L"