Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

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;~
  1. % selects full file

  2. <alt-s> split selection into multiple selections on newlines

  3. "yy yanks the selections into "y" register. We'll need it for later

  4. s and then input the pattern \d then <enter> which creates a selection on all digits

  5. d deletes the selections. Essentially we've removed all the digits.

  6. hh goes backwards 2 chars, important to make sure we are at the end of each word

  7. Use b to select till the beginning of every word, which also nicely selects all the words that there are

  8. ms` surrounds each word with a backtick

  9. ` switches all characters to lowercase

  10. x selects each line then use _ to trim the trailing whitespace

  11. ms( surrounds each line with parentheses

  12. I goes into insert mode at the beginning of each line

  13. Type the following:

    INSERT INTO `database
    
  14. <esc> goes back to normal mode

  15. a to go into insert mode after the backtick then type:

    .`table
    
  16. <esc> goes back into normal mode, then la to enter insert mode just before the opening parentheses

  17. Add a space then <esc> to go back into normal mode again

  18. A goes into insert mode at the end of each line, now type:

    VALUES (
    
  19. Hit <esc> to leave insert mode. Your cursor will be at the closing parenthesis.

  20. "yP pastes our previously yanked items from the "y" register

  21. S,<enter> splits current selection into multiple selections on each comma

  22. ms' surrounds each item with a single quote

  23. A; adds a semicolon at the end of each line

  24. <esc> goes back to normal mode and Fl to place your cursor on the lowercase "l" of each "location"

  25. ; collapses each selection into a single-width selection

  26. ~ toggles the case for each "l" into "L"