Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Introduction to Vim

Introduction to Vim

An introduction and bit advanced intro to Vim

Avatar for Vysakh Sreenivasan

Vysakh Sreenivasan

April 29, 2013
Tweet

More Decks by Vysakh Sreenivasan

Other Decks in Programming

Transcript

  1. Vim Table of Contents 1 Vim History Vi Basics Advanced

    Plugins Resources and Links Vysakh Sreenivasan (vysakh.quora.com) Vim - The Hacker’s Editor April 29, 2013 2 / 35
  2. Vim What is the big deal? Vi, that got its

    start in 1976.- Grand Daddy! Moving: if IDEs walk, vim swings Default in most OSes Increases Productivity!! Most Hackers use! The last editor you would use! Vysakh Sreenivasan (vysakh.quora.com) Vim - The Hacker’s Editor April 29, 2013 4 / 35
  3. Vim What is the big deal? Vi, that got its

    start in 1976.- Grand Daddy! Moving: if IDEs walk, vim swings Default in most OSes Increases Productivity!! Most Hackers use! The last editor you would use! Vysakh Sreenivasan (vysakh.quora.com) Vim - The Hacker’s Editor April 29, 2013 4 / 35
  4. Vim What is the big deal? Vi, that got its

    start in 1976.- Grand Daddy! Moving: if IDEs walk, vim swings Default in most OSes Increases Productivity!! Most Hackers use! The last editor you would use! Vysakh Sreenivasan (vysakh.quora.com) Vim - The Hacker’s Editor April 29, 2013 4 / 35
  5. Vim What is the big deal? Vi, that got its

    start in 1976.- Grand Daddy! Moving: if IDEs walk, vim swings Default in most OSes Increases Productivity!! Most Hackers use! The last editor you would use! Vysakh Sreenivasan (vysakh.quora.com) Vim - The Hacker’s Editor April 29, 2013 4 / 35
  6. Vim What is the big deal? Vi, that got its

    start in 1976.- Grand Daddy! Moving: if IDEs walk, vim swings Default in most OSes Increases Productivity!! Most Hackers use! The last editor you would use! Vysakh Sreenivasan (vysakh.quora.com) Vim - The Hacker’s Editor April 29, 2013 4 / 35
  7. Vim What is the big deal? Vi, that got its

    start in 1976.- Grand Daddy! Moving: if IDEs walk, vim swings Default in most OSes Increases Productivity!! Most Hackers use! The last editor you would use! Vysakh Sreenivasan (vysakh.quora.com) Vim - The Hacker’s Editor April 29, 2013 4 / 35
  8. Vim How do Vim users feel :) Vysakh Sreenivasan (vysakh.quora.com)

    Vim - The Hacker’s Editor April 29, 2013 5 / 35
  9. Vim History Line and Screen Editors History of Text editors

    in a nutshell Vysakh Sreenivasan (vysakh.quora.com) Vim - The Hacker’s Editor April 29, 2013 6 / 35
  10. Vim History Line and Screen Editors History of Text editors

    in a nutshell Basic Line editor - Ed editor - made by Ken Thompson Vysakh Sreenivasan (vysakh.quora.com) Vim - The Hacker’s Editor April 29, 2013 6 / 35
  11. Vim History Line and Screen Editors History of Text editors

    in a nutshell Basic Line editor - Ed editor - made by Ken Thompson Advanced line editor Ex editor Vysakh Sreenivasan (vysakh.quora.com) Vim - The Hacker’s Editor April 29, 2013 6 / 35
  12. Vim History Line and Screen Editors History of Text editors

    in a nutshell Basic Line editor - Ed editor - made by Ken Thompson Advanced line editor Ex editor Screen Editor - you could see stuff while editing :) Vi Vysakh Sreenivasan (vysakh.quora.com) Vim - The Hacker’s Editor April 29, 2013 6 / 35
  13. Vim History Line and Screen Editors History of Text editors

    in a nutshell Basic Line editor - Ed editor - made by Ken Thompson Advanced line editor Ex editor Screen Editor - you could see stuff while editing :) Vi An improved Vi - Vim Vysakh Sreenivasan (vysakh.quora.com) Vim - The Hacker’s Editor April 29, 2013 6 / 35
  14. Vim Vi Basics Fire up Vim vim filename Opens up

    vim with a filename. :w - to save :q - to quit :wq or :x - to save and quit :q! - to quit without saving. :e filename - to open another file :r filename - to read content from one file into current file Vysakh Sreenivasan (vysakh.quora.com) Vim - The Hacker’s Editor April 29, 2013 7 / 35
  15. Vim Vi Basics Modes Insert - Only for typing!!! i

    to insert in the cursor position I to insert in the beginning of line a to append after cursor position A to append at the end of the line o to insert a new line below O to insert a new line above Command - delete/copy/paste/movement/.. Ctrl + C or Ctrl+ [ or esc Vysakh Sreenivasan (vysakh.quora.com) Vim - The Hacker’s Editor April 29, 2013 8 / 35
  16. Vim Vi Basics Modes contd.. Ex mode - Similar to

    ex editor :wq - begins with a colon, followed by commands. :g/re/p - grep was inspired from ex editor. This prints the lines which has ”re” Visual - For Selection v Selects characters V Selects rows/lines Ctrl+v Selects block/column Vysakh Sreenivasan (vysakh.quora.com) Vim - The Hacker’s Editor April 29, 2013 9 / 35
  17. Vim Vi Basics Panes ctrl+w+s or :sp - split horizontally

    ctrl+w+v or :vsp - split vertically ctrl+w+w - move next pane Vysakh Sreenivasan (vysakh.quora.com) Vim - The Hacker’s Editor April 29, 2013 10 / 35
  18. Vim Vi Basics Tabs :tabe filename To open a file

    in a new tab gt To move to next tab gT To move to prev tab Vysakh Sreenivasan (vysakh.quora.com) Vim - The Hacker’s Editor April 29, 2013 11 / 35
  19. Vim Vi Basics Movements j - points downwards! k -

    the straight k line points upwards Vysakh Sreenivasan (vysakh.quora.com) Vim - The Hacker’s Editor April 29, 2013 12 / 35
  20. Vim Vi Basics More on Movements.. gg - go to

    first line G - go to last line 4G - go to 4th line { and } movements to the beginning or end of the paragraph respectively. ( and ) to beginning or end of the sentence. Vysakh Sreenivasan (vysakh.quora.com) Vim - The Hacker’s Editor April 29, 2013 13 / 35
  21. Vim Vi Basics More on Movements.. w - go to

    the beginning of next word e - go to the end of the next word b - go the beginning of the previous word 0 - go to the beginning of the line $ - go to the end of the line Vysakh Sreenivasan (vysakh.quora.com) Vim - The Hacker’s Editor April 29, 2013 14 / 35
  22. Vim Vi Basics Copy/Delete/Paste y - yank/copyies, yy - copies

    the line, yw - copies the word d - deletes, dd - deletes the line, dw - deletes the word c - change, cc deletes the line and go to insert mode p - paste the copied/deleted text x - delete character under the cursor X - delete character before the cursor Vysakh Sreenivasan (vysakh.quora.com) Vim - The Hacker’s Editor April 29, 2013 15 / 35
  23. Vim Vi Basics Combination of keys 10p - paste ten

    times. d$ - delete till the end of the line 4yw - copy four words ggdG - deletes the entire file (goes to first line (gg), starts deleting (d) till the last line G) Vysakh Sreenivasan (vysakh.quora.com) Vim - The Hacker’s Editor April 29, 2013 17 / 35
  24. Vim Vi Basics More on key combo diw - delete

    the word where the cursor is in. dw - deletes letters from cursor till next word. ciw - changes the word where cursor is in. di’ d“ di{ di( di[ replaces words inside ’ ” { ( [ Vysakh Sreenivasan (vysakh.quora.com) Vim - The Hacker’s Editor April 29, 2013 18 / 35
  25. Vim Vi Basics Search, Replace Search /searchword Search current word

    * Search and Replace :%s/search/replace/g % - every lines (without that, only current line) s - substitute g - replace all occurences (without that, only first occurence) Vysakh Sreenivasan (vysakh.quora.com) Vim - The Hacker’s Editor April 29, 2013 19 / 35
  26. Vim Vi Basics Play around with ex editor/ex mode ex

    filename (will open the file) i (to insert words) . (in the next line will end inserting) p (to print current line) %p (to print all lines) a (to append at the bottom) . (full stop in next will end appending) Vysakh Sreenivasan (vysakh.quora.com) Vim - The Hacker’s Editor April 29, 2013 20 / 35
  27. Vim Vi Basics Play around with ex editor/ex mode ex

    filename (will open the file) i (to insert words) . (in the next line will end inserting) p (to print current line) %p (to print all lines) a (to append at the bottom) . (full stop in next will end appending) Vysakh Sreenivasan (vysakh.quora.com) Vim - The Hacker’s Editor April 29, 2013 20 / 35
  28. Vim Vi Basics Play around with ex editor/ex mode ex

    filename (will open the file) i (to insert words) . (in the next line will end inserting) p (to print current line) %p (to print all lines) a (to append at the bottom) . (full stop in next will end appending) Vysakh Sreenivasan (vysakh.quora.com) Vim - The Hacker’s Editor April 29, 2013 20 / 35
  29. Vim Vi Basics Play around with ex editor/ex mode ex

    filename (will open the file) i (to insert words) . (in the next line will end inserting) p (to print current line) %p (to print all lines) a (to append at the bottom) . (full stop in next will end appending) Vysakh Sreenivasan (vysakh.quora.com) Vim - The Hacker’s Editor April 29, 2013 20 / 35
  30. Vim Vi Basics Play around with ex editor/ex mode ex

    filename (will open the file) i (to insert words) . (in the next line will end inserting) p (to print current line) %p (to print all lines) a (to append at the bottom) . (full stop in next will end appending) Vysakh Sreenivasan (vysakh.quora.com) Vim - The Hacker’s Editor April 29, 2013 20 / 35
  31. Vim Vi Basics Play around with ex editor/ex mode ex

    filename (will open the file) i (to insert words) . (in the next line will end inserting) p (to print current line) %p (to print all lines) a (to append at the bottom) . (full stop in next will end appending) Vysakh Sreenivasan (vysakh.quora.com) Vim - The Hacker’s Editor April 29, 2013 20 / 35
  32. Vim Vi Basics Play around with ex editor/ex mode ex

    filename (will open the file) i (to insert words) . (in the next line will end inserting) p (to print current line) %p (to print all lines) a (to append at the bottom) . (full stop in next will end appending) Vysakh Sreenivasan (vysakh.quora.com) Vim - The Hacker’s Editor April 29, 2013 20 / 35
  33. Vim Vi Basics t$ (copy current line to the end

    of the file) 1,3m5 (move 1-3 lines after 5th line) c (to change the current line) w (to write/save changes) wq (to save and quit) q (to quit) e filename (to edit the file) Vysakh Sreenivasan (vysakh.quora.com) Vim - The Hacker’s Editor April 29, 2013 21 / 35
  34. Vim Vi Basics t$ (copy current line to the end

    of the file) 1,3m5 (move 1-3 lines after 5th line) c (to change the current line) w (to write/save changes) wq (to save and quit) q (to quit) e filename (to edit the file) Vysakh Sreenivasan (vysakh.quora.com) Vim - The Hacker’s Editor April 29, 2013 21 / 35
  35. Vim Vi Basics t$ (copy current line to the end

    of the file) 1,3m5 (move 1-3 lines after 5th line) c (to change the current line) w (to write/save changes) wq (to save and quit) q (to quit) e filename (to edit the file) Vysakh Sreenivasan (vysakh.quora.com) Vim - The Hacker’s Editor April 29, 2013 21 / 35
  36. Vim Vi Basics t$ (copy current line to the end

    of the file) 1,3m5 (move 1-3 lines after 5th line) c (to change the current line) w (to write/save changes) wq (to save and quit) q (to quit) e filename (to edit the file) Vysakh Sreenivasan (vysakh.quora.com) Vim - The Hacker’s Editor April 29, 2013 21 / 35
  37. Vim Vi Basics t$ (copy current line to the end

    of the file) 1,3m5 (move 1-3 lines after 5th line) c (to change the current line) w (to write/save changes) wq (to save and quit) q (to quit) e filename (to edit the file) Vysakh Sreenivasan (vysakh.quora.com) Vim - The Hacker’s Editor April 29, 2013 21 / 35
  38. Vim Vi Basics t$ (copy current line to the end

    of the file) 1,3m5 (move 1-3 lines after 5th line) c (to change the current line) w (to write/save changes) wq (to save and quit) q (to quit) e filename (to edit the file) Vysakh Sreenivasan (vysakh.quora.com) Vim - The Hacker’s Editor April 29, 2013 21 / 35
  39. Vim Vi Basics t$ (copy current line to the end

    of the file) 1,3m5 (move 1-3 lines after 5th line) c (to change the current line) w (to write/save changes) wq (to save and quit) q (to quit) e filename (to edit the file) Vysakh Sreenivasan (vysakh.quora.com) Vim - The Hacker’s Editor April 29, 2013 21 / 35
  40. Vim Vi Basics tip Ctrl+w in insert mode will backspace

    the word you are typing. Also works in shell. J Capital j will join two lines. gf If the cursor is on a word sample, the command gf would open the file sample if it is present in current directory gi Go back to the place where you are editing. Handy command when you are in the middle of your typing and go to command mode to fix issues. Vysakh Sreenivasan (vysakh.quora.com) Vim - The Hacker’s Editor April 29, 2013 22 / 35
  41. Vim Advanced Omni complete Ctrl + n or Ctrl +

    p (in insert mode, handy when calling functions) Ctrl + x + k - dictionary completion Ctrl + x + f - file name autocompletion Vysakh Sreenivasan (vysakh.quora.com) Vim - The Hacker’s Editor April 29, 2013 23 / 35
  42. Vim Advanced Macros Records set of actions - qm or

    qa or qb (begin with q) To repeat the recorded macro - @m or @a or @b (begin with @ followed by the letter) Vysakh Sreenivasan (vysakh.quora.com) Vim - The Hacker’s Editor April 29, 2013 24 / 35
  43. Vim Advanced Marks Mark your position in the file -

    ml or mb or mq (begin with m) Go to the marked position - ’l or ’b or ’q (begin with ’ followed by corresponding letter) Vysakh Sreenivasan (vysakh.quora.com) Vim - The Hacker’s Editor April 29, 2013 25 / 35
  44. Vim Advanced Jumps Ctrl + o - to go the

    previous position in your files. ctrl + i - to go to the next position in your files. :jumps - to see all the jumps Vysakh Sreenivasan (vysakh.quora.com) Vim - The Hacker’s Editor April 29, 2013 26 / 35
  45. Vim Advanced Registers ”kyy - Copying the current line to

    k register ”kp - Pasting the line from k register ”Kyy - Appending to the k register ”kp - Paste the lines from k register Vysakh Sreenivasan (vysakh.quora.com) Vim - The Hacker’s Editor April 29, 2013 27 / 35
  46. Vim Advanced norm in ex mode :1,3 norm I Wow

    This would add the add word Wow at the beginning of 1-3 lines :[range] norm [Commands] Vysakh Sreenivasan (vysakh.quora.com) Vim - The Hacker’s Editor April 29, 2013 28 / 35
  47. Vim Advanced What we have learnt now? Vysakh Sreenivasan (vysakh.quora.com)

    Vim - The Hacker’s Editor April 29, 2013 29 / 35
  48. Vim Plugins .vimrc The file .vimrc You could customize your

    vim setup Vysakh Sreenivasan (vysakh.quora.com) Vim - The Hacker’s Editor April 29, 2013 30 / 35
  49. Vim Plugins Plugins - installation Plugin Managers Use Vundle.(latest one)

    or Pathogen. Vysakh Sreenivasan (vysakh.quora.com) Vim - The Hacker’s Editor April 29, 2013 31 / 35
  50. Vim Plugins Plugins I use NERDTree Nerdcommenter Ctrl+P dwm surround

    snipmate rails.vim supertab LaTeXBox Vysakh Sreenivasan (vysakh.quora.com) Vim - The Hacker’s Editor April 29, 2013 32 / 35
  51. Vim Plugins New Plugins which is worth a try gist

    vim Writing your gist files in Vim :D Etherpad vim Yup! Etherpad in Vim! :) Also check out remote pair programming using Vim In browser Vimium in Chrome! is way awesome. Pentadactly in Firefox. (not sure if it is active) Vysakh Sreenivasan (vysakh.quora.com) Vim - The Hacker’s Editor April 29, 2013 33 / 35
  52. Vim Resources and Links Learn Vim vimtutor (in terminal after

    installing Vim) :help thingyouwantosearch (inside Vim) openvim.com vimcasts.com usevim.org plenty more Vysakh Sreenivasan (vysakh.quora.com) Vim - The Hacker’s Editor April 29, 2013 34 / 35