lecture 10 maps
play

Lecture 10: Maps Part II: Core Commands Announcements HW3 due NOW! - PowerPoint PPT Presentation

Lecture 10: Maps Part II: Core Commands Announcements HW3 due NOW! Announcements HW3 due NOW! There will be no HW4... Don't have enough time to give feedback for HW3. However, everything covered so far will still be on the


  1. Lecture 10: Maps Part II: Core Commands

  2. Announcements • HW3 due NOW!

  3. Announcements • HW3 due NOW! • There will be no HW4... – Don't have enough time to give feedback for HW3. – However, everything covered so far will still be on the exam regardless.

  4. Announcements • What will be on the exam: – EVERYTHING from Part II of the course, including the homework material. – Maps and commands from today. – You do not need to know about code tools (covered next lecture)...Though you probably should.

  5. Announcements • Exam format: – Will NOT involve descriptions. – Will get very little credit for writing descriptions only. (You NEED to know all the commands.) – Use commands at your discretion (ergonomics, clarity) – Have to know sound practices of vimscript! – Will be penalized (hard) for bad practices.

  6. Something else you have to know • The undo and redo commands: u Undo previous change <C-r> Redo previous change • Will NOT undo/redo previous locate, use <C-o>/<C-i> for that.

  7. Today: Maps

  8. Today: Maps • Absolute worst of vimscripting

  9. Today: Maps • Absolute worst of vimscripting • Dark side of vimscript

  10. Today: Maps • Absolute worst of vimscripting • Dark side of vimscript • Be extra careful!

  11. Today: Maps • Absolute worst of vimscripting • Dark side of vimscript • Be extra careful! • Sometime I don't get it right (and it's near impossible to debug)

  12. Maps • A map is pressing one key combination to perform another key combination . • Note that this does NOT guarantee pressing one key combination to execute certain commands!

  13. Maps • A map is pressing one key combination to perform another key combination . • Note that this does NOT guarantee pressing one key combination to execute certain commands! – wrong mode, previous keymaps, and other shenannigans (which you shall see).

  14. Maps • Notation: • If I want to press the keys Q to perform the keys dt , I write: Q --> dt

  15. Maps • Why do we need our own notation?

  16. Maps • Why do we need our own notation? • Because SHENANIGANS !

  17. Maps • Why do we need our own notation? • Because SHENANIGANS !

  18. General Syntax for Maps

  19. General Syntax for Maps

  20. General Syntax for Maps • To map: <keystrokes1> --> <keystrokes2> • We put this in vimscript: <maptype> <keystrokes1> <keystrokes2>

  21. General Syntax for Maps • To map: <keystrokes1> --> <keystrokes2> • We put this in vimscript: <maptype> <keystrokes1> <keystrokes2>

  22. <maptype> • <maptype> indicates what kind of context your map works in.

  23. <maptype> • <maptype> indicates what kind of context your map works in. • Two questions: • Which mode does your mapping work in? • What kind of mapping should it map to?

  24. <maptype> • Which mode should it work in? Should it work in... – Normal Mode, prefix with n – Insert Mode, prefix with i – Visual Mode, prefix with v (not true technically, but nobody cares). – Command Line Mode, prefix with c – Any mode, don't prefix.

  25. <maptype> • Which mode should it work in? Should it work in... – Normal Mode, prefix with n – Insert Mode, prefix with i – Visual Mode, prefix with v (not true technically, but nobody cares). – Command Line Mode, prefix with c – Any mode, don't prefix. (99.999% of time, you don't want to map to every mode)

  26. <maptype> • Examples: • nmap u v – u --> v (in normal mode only) • imap xx <Esc> – xx --> <Esc> (in insert mode only)

  27. <maptype> • What kind of mapping should it map to?

  28. <maptype> • What kind of mapping should it map to? • Much much harder question to answer.

  29. <maptype> • What kind of mapping should it map to? • Much much harder question to answer. • When I map: u --> v , whose functionality should I invoke?

  30. <maptype> • What kind of mapping should it map to? • For example: • v --> i • u --> v

  31. <maptype> • What kind of mapping should it map to? • For example: • v --> i • u --> v • So is u --> i ?

  32. <maptype> • What kind of mapping should it map to? • For example: • v --> i • u --> v • So is u --> i ? Depends on <maptype> .

  33. <maptype> • What kind of mapping should it map to? • For example: • v --> i • u --> v • Two cases: – if I want to use the default behavior of Vim's v keystroke, it is called a nonrecursive mapping. – if I want to use my already mapped behavior of Vim's v keystroke, it is called a recursive mapping .

  34. <maptype> • Syntax for <maptype> : • <mode indicator><recursive?>map • Where <recursive?> can be: – Nothing, if it is recursive – nore , if it is nonrecursive.

  35. <maptype> • Examples! • nnoremap u v – u --> v (in normal mode, and invokes the default behavior of v , which is enter/exit visual mode) • nmap lol u – lol --> u (in normal mode, and invokes the mapped behavior of u , which so far, is the same as invoking the default behavior of v , which is enter/exit visual mode)

  36. <maptype> • Best Practices (they are strict, always follow them!): • ALWAYS ask yourself those two questions when writing maps!

  37. <maptype> • Best Practices (they are strict, always follow them!): • Bad examples: • 1. Mapping to all modes: – noremap Q K – When in insert mode, can't type Q anymore!

  38. <maptype> • Best Practices (they are strict, always follow them!): • Bad examples: • 1. Mapping to all modes: – noremap Q K – When in insert mode, can't type Q anymore!

  39. <maptype> • Best Practices (they are strict, always follow them!): • Bad examples: • 2. Using a recursive mapping when a nonrecursive mapping should be used instead. – nnoremap w W – nmap Q :w<CR> • w --> W . • Q --> :w<CR> --> :W<CR> • Not valid command anymore!

  40. <maptype> • Best Practices (they are strict, always follow them!): • Bad examples: • 2. Using a recursive mapping when a nonrecursive mapping should be used instead. – nnoremap w W – nmap Q :w<CR> • w --> W . • Q --> :w<CR> --> :W<CR> • Not valid command anymore!

  41. <maptype> • Best Practices (they are strict, always follow them!): • Bad examples: • 2. Using a recursive mapping when a nonrecursive mapping should be used instead. – nmap u v – nmap v u • u -> v -> u -> v -> u -> v -> u -> v -> ...

  42. <maptype> • Best Practices (they are strict, always follow them!): • Bad examples: • 2. Using a recursive mapping when a nonrecursive mapping should be used instead. – nmap u v – nmap v u • u -> v -> u -> v -> u -> v -> u -> v -> ...

  43. <maptype> • Best Practices (they are strict, always follow them!): • Bad examples: • 2. Using a recursive mapping when a nonrecursive mapping should be used instead. – nmap u v – nmap v u • u -> v -> u -> v -> u -> v -> u -> v -> ...

  44. <maptype> • Hence: • ALWAYS write maps on the specific mode you want to map to (you can guarantee that you will never use a raw map ). • Don't use recursive maps unless you know what you are doing (few cases permit that, when you really want to perform your own mappings!)

  45. <maptype> • When to use recursive maps: " Magically map %% to current file directory cnoremap <expr> %% getcmdtype() == ':' ? expand('%:h').'/' : '%%' " Quick directory edit load nmap <leader>e :edit %%

  46. and now, onto the actual shenannigans...

  47. and now, onto the actual shenannigans...

  48. Bad Syntax Design 101 • <maptype> <keystrokes1> <keystroke2> • Are delimited by single spaces...

  49. Bad Syntax Design 101 • <maptype> <keystrokes1> <keystroke2> • Are delimited by single spaces... • ... and is not a good idea!

  50. Bad Syntax Design 101 • nnoremap u v ^ ^^ • So is it: • u --> <Space>v ? • u<Space> --> v ? • u --> v ?

  51. Bad Syntax Design 101 • nnoremap u v ^ ^^ • So is it: • u --> <Space>v ? • u<Space> --> v ? • u --> v ? • Short answer: I don't know! Depends on version. Never rely on that.

  52. Bad Syntax Design 101 • nnoremap u<Space> v ^ ^ • Right way to write that ^. • So is it: • u --> <Space>v ? • u<Space> --> v ? • u --> v ? • Short answer: I don't know! Depends on version. Never rely on that.

  53. Bad Syntax Design 102 • nmap <leader>e :edit %% • ^ ^ ^ • So is it: • <leader>e --> :edit<Space>%% ? • :edit --> %% ? • Something else ?

  54. Bad Syntax Design 102 • nmap <leader>e :edit %% • ^ ^ ^ • So is it: • <leader>e --> :edit<Space>%% ? • :edit --> %% ? • Something else ? • Vimscript maps are ALWAYS greedy in these situations! • Swallows the first two spaces, maps the the thing in between the two spaces to after the second space.

Recommend


More recommend