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

lecture 10 maps
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

Lecture 10: Maps

Part II: Core Commands

slide-2
SLIDE 2

Announcements

  • HW3 due NOW!
slide-3
SLIDE 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.

slide-4
SLIDE 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.

slide-5
SLIDE 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.

slide-6
SLIDE 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.

slide-7
SLIDE 7

Today: Maps

slide-8
SLIDE 8

Today: Maps

  • Absolute worst of vimscripting
slide-9
SLIDE 9

Today: Maps

  • Absolute worst of vimscripting
  • Dark side of vimscript
slide-10
SLIDE 10

Today: Maps

  • Absolute worst of vimscripting
  • Dark side of vimscript
  • Be extra careful!
slide-11
SLIDE 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)

slide-12
SLIDE 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!

slide-13
SLIDE 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).

slide-14
SLIDE 14

Maps

  • Notation:
  • If I want to press the keys Q to

perform the keys dt, I write: Q --> dt

slide-15
SLIDE 15

Maps

  • Why do we need our own notation?
slide-16
SLIDE 16

Maps

  • Why do we need our own notation?
  • Because SHENANIGANS!
slide-17
SLIDE 17

Maps

  • Why do we need our own notation?
  • Because SHENANIGANS!
slide-18
SLIDE 18

General Syntax for Maps

slide-19
SLIDE 19

General Syntax for Maps

slide-20
SLIDE 20

General Syntax for Maps

  • To map:

<keystrokes1> --> <keystrokes2>

  • We put this in vimscript:

<maptype> <keystrokes1> <keystrokes2>

slide-21
SLIDE 21

General Syntax for Maps

  • To map:

<keystrokes1> --> <keystrokes2>

  • We put this in vimscript:

<maptype> <keystrokes1> <keystrokes2>

slide-22
SLIDE 22

<maptype>

  • <maptype> indicates what kind of

context your map works in.

slide-23
SLIDE 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?
slide-24
SLIDE 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.

slide-25
SLIDE 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)

slide-26
SLIDE 26

<maptype>

  • Examples:
  • nmap u v

– u --> v (in normal mode only)

  • imap xx <Esc>

– xx --> <Esc> (in insert mode only)

slide-27
SLIDE 27

<maptype>

  • What kind of mapping should it map to?
slide-28
SLIDE 28

<maptype>

  • What kind of mapping should it map to?
  • Much much harder question to answer.
slide-29
SLIDE 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?

slide-30
SLIDE 30

<maptype>

  • What kind of mapping should it map to?
  • For example:
  • v --> i
  • u --> v
slide-31
SLIDE 31

<maptype>

  • What kind of mapping should it map to?
  • For example:
  • v --> i
  • u --> v
  • So is u --> i?
slide-32
SLIDE 32

<maptype>

  • What kind of mapping should it map to?
  • For example:
  • v --> i
  • u --> v
  • So is u --> i? Depends on <maptype>.
slide-33
SLIDE 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.

slide-34
SLIDE 34

<maptype>

  • Syntax for <maptype>:
  • <mode indicator><recursive?>map
  • Where <recursive?> can be:

– Nothing, if it is recursive – nore, if it is nonrecursive.

slide-35
SLIDE 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)

slide-36
SLIDE 36

<maptype>

  • Best Practices (they are strict, always follow

them!):

  • ALWAYS ask yourself those two

questions when writing maps!

slide-37
SLIDE 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!

slide-38
SLIDE 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!

slide-39
SLIDE 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!
slide-40
SLIDE 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!
slide-41
SLIDE 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 -> ...
slide-42
SLIDE 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 -> ...
slide-43
SLIDE 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 -> ...
slide-44
SLIDE 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!)

slide-45
SLIDE 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 %%

slide-46
SLIDE 46

and now, onto the actual shenannigans...

slide-47
SLIDE 47

and now, onto the actual shenannigans...

slide-48
SLIDE 48

Bad Syntax Design 101

  • <maptype> <keystrokes1>

<keystroke2>

  • Are delimited by single spaces...
slide-49
SLIDE 49

Bad Syntax Design 101

  • <maptype> <keystrokes1>

<keystroke2>

  • Are delimited by single spaces...
  • ... and is not a good idea!
slide-50
SLIDE 50

Bad Syntax Design 101

  • nnoremap u v

^ ^^

  • So is it:
  • u --> <Space>v ?
  • u<Space> --> v ?
  • u --> v ?
slide-51
SLIDE 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.
slide-52
SLIDE 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.
slide-53
SLIDE 53

Bad Syntax Design 102

  • nmap <leader>e :edit %%
  • ^ ^ ^
  • So is it:
  • <leader>e --> :edit<Space>%% ?
  • :edit --> %% ?
  • Something else ?
slide-54
SLIDE 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.

slide-55
SLIDE 55

Bad Syntax Design 103

  • nmap u v " Maps from u to v
  • What does it do?
  • (Reminder, " begins a comment in

vimscript.)

slide-56
SLIDE 56

Bad Syntax Design 103

  • nmap u v " Maps from u to v
  • What does it do?
  • (Reminder, " begins a comment in

vimscript.)

  • *Hint*
slide-57
SLIDE 57

Bad Syntax Design 103

  • nmap u v " Maps from u to v
  • What does it do?
  • (Reminder, " begins a comment in

vimscript.)

  • *Hint*
  • u -> v " Maps from u to v
slide-58
SLIDE 58

Bad Syntax Design 103

  • nmap u v " Maps from u to v
  • What does it do?
  • (Reminder, " begins a comment in

vimscript.)

  • *Hint*
  • u -> v " Maps from u to v
  • What does pressing u in Normal Mode

do?

slide-59
SLIDE 59

Bad Syntax Design 103

u -> v " Maps from u to v

  • What does pressing u in Normal Mode

do?

slide-60
SLIDE 60

Bad Syntax Design 103

u -> v " Maps from u to v

  • What does pressing u in Normal Mode

do?

  • v -> Enter Visual Mode.
  • <Space> -> Moves forward by one

(don't have to know that)

  • " -> Specify Register
slide-61
SLIDE 61

Bad Syntax Design 103

u -> v " Maps from u to v

  • What does pressing u in Normal Mode

do?

  • v -> Enter Visual Mode.
  • <Space> -> Moves forward by one

(don't have to know that)

  • " -> Specify Register
  • <Space> -> Specified the <Space>

register?!?!?

slide-62
SLIDE 62

Bad Syntax Design 103

u -> v " Maps from u to v

  • What does pressing u in Normal Mode

do?

  • v -> Enter Visual Mode.
  • <Space> -> Moves forward by one

(don't have to know that)

  • " -> Specify Register
  • <Space> -> Specified the <Space>

register?!?!? (There is no such thing)

slide-63
SLIDE 63

Bad Syntax Design 103

  • What does it do when it does something

invalid?

  • Same as macro termination! (Covered

waaaay waaay back)

slide-64
SLIDE 64

Bad Syntax Design 103

  • What does it do when it does something

invalid?

  • Same as macro termination! (Covered

waaaay waaay back)

  • As a safety feature, it will not continue

to execute commands, and will abort at this point.

slide-65
SLIDE 65

Bad Syntax Design 103

  • For this reason, in effect:
  • u -> v " Maps from u to v
  • does the same thing as:
  • u -> v<Space>
  • aka.
slide-66
SLIDE 66

Bad Syntax Design 103

  • For this reason, in effect:
  • u -> v " Maps from u to v
  • does the same thing as:
  • u -> v<Space>
  • aka.

It does the right thing for the completely wrong reasons!!!!

slide-67
SLIDE 67

Bad Syntax Design 103

  • For this reason, in effect:
  • u -> v " Maps from u to v
  • does the same thing as:
  • u -> v<Space>
  • aka.

It does the right thing for the completely wrong reasons!!!!

slide-68
SLIDE 68

Bad Syntax Design 103

  • For this reason, in effect:
  • u -> v " Maps from u to v
  • does the same thing as:
  • u -> v<Space>
  • aka.

It does the right thing for the completely wrong reasons!!!!

  • Even worse, very very very few people know

that, but got away with it! (you are not allowed to do that in this course)

slide-69
SLIDE 69

Maps

  • Moral of the story?
slide-70
SLIDE 70

Maps

  • Moral of the story?
  • ALWAYS put comments on a new line.

(This holds for let statements as well!)

– let hello = 1 " sets hello to 1 – What happens when you try to comment this line?

  • Be CAREFUL when you're writing maps.
slide-71
SLIDE 71

Maps

  • On a side note:
  • The <leader> key is a special key!
slide-72
SLIDE 72

Maps

  • On a side note:
  • The <leader> key is a special key!
  • Is designed specifically for the user to

map their custom commands.

– Pretty much every key is already mapped to some function in Vim. More often than not you don't want to overwrite that behaviour.

slide-73
SLIDE 73

Maps

  • On a side note:
  • The <leader> key is a special key!
  • To set <leader> to a specific key:

– let mapleader = '<key>' in vimscript. – I recommend setting leader to <Space>. – (Confession: I actually use , , which is a VERY bad habit, and trying to break out of it.)

slide-74
SLIDE 74

Maps

  • By now, you know the very basics of

vimscript! (that takes up probably 90%

  • f your vimrc).
  • You also know the nuances of even basic

vimscripting! (very few people do)

  • A few loose ends... (demo).
slide-75
SLIDE 75

vimscript

  • What we have NOT covered:

– let statements (sets variables, not options!)

  • Used to set variables for plugins, is actually

typed unlike options.

– autocmds

  • I am more convinced now that it is a bad idea to

use autocmds. Split them to different filetypes, and put the corresponding filetype in the after/ftplugin directory, so that commands run

  • n load by FILETYPE.
slide-76
SLIDE 76

Sneak Peek of Code Tools

  • Put this (at the beginning, after nocp) in

your vimrc:

– syntax enable – filetype plugin indent on – Enables syntax highlighting and indentation.

  • colorschemes - many resources on the

Internet.

  • demo.
slide-77
SLIDE 77

Next Time

  • Code Tools (finally!)
  • Syntax highlighting, Tabs and indentation,

Completion, make, process management, Command-T/selecta.

  • Will be mostly demos, since most of the

material is in flux, and may change in the future due to version changes.

  • Final is coming up. Practice commands!
  • Happy Thanksgiving!