Lecture 3: Editors (vim) (2020)

Missing Semester
1 Feb 202048:26

Summary

TLDR本次讲座深入探讨了文本编辑器vim的强大功能和高效编程理念。通过vim的多模式操作,讲解了如何在编程中快速导航和编辑文本。强调了vim的可编程性和通过.vimrc文件进行个性化配置的能力,以及通过插件扩展功能的方法。此外,还介绍了vim模式在其他程序中的应用,如Python REPL和终端,以及如何通过练习和探索配置来精通vim。

Takeaways

  • 📝 Vim 是一个程序员文本编辑器,与编写英语散文的文本编辑器有所不同,因为它专为编程设计。
  • 🔍 Vim 拥有多种操作模式,如普通模式(Normal Mode)、插入模式(Insert Mode)和命令行模式(Command Line Mode)等。
  • ⏫ 在 Vim 中,可以使用 hjkl 键进行光标移动,这比使用箭头键更高效。
  • 🔑 Vim 的强大之处在于其模态编辑,可以将不同的键组合在一起,类似于编程语言中的函数和命令组合。
  • 📖 Vim 支持通过键盘快捷键执行命令,如保存文件(:w)、退出编辑器(:q)等,而无需使用鼠标。
  • 🔄 Vim 中的复制(y)、粘贴(p)和删除(d)命令可以通过结合移动命令来操作文本。
  • 🔢 在 Vim 中,可以通过数字前缀(如 4j)来重复执行移动或编辑命令多次。
  • 📌 Vim 允许用户自定义配置文件(vimrc),以便根据个人喜好调整编辑器的行为和外观。
  • 🔧 Vim 可以通过插件扩展功能,如模糊文件查找、撤销历史可视化和文件资源管理器等。
  • 📈 Vim 的模式也可以在其他程序中使用,如 Python REPL、终端和 Jupyter Notebook 等,提高在这些工具中的编辑效率。
  • 🚀 学习 Vim 可能需要一定的时间和练习,但一旦掌握,可以极大提高编程和文本编辑的效率。

Q & A

  • 文本编辑器对于程序员的重要性是什么?

    -文本编辑器对于程序员非常重要,因为程序员花费大量时间编辑文本和程序代码。如果能够通过使用文本编辑器提高编辑效率,那么在本科学习或职业生涯中可以节省数百小时的时间。

  • 为什么程序员在选择文本编辑器时应该坚持使用一种特定的工具?

    -坚持使用一种特定的文本编辑器可以使程序员更加熟练掌握该工具,从而提高编程效率。尽管一开始可能会因为学习新工具而降低速度,但在大约20小时的编程实践后,程序员将恢复到原来的编程速度,并随着对工具的深入理解而不断提高效率。

  • Vim的 modal editor(模态编辑器)概念是什么?

    -Vim是一个模态编辑器,意味着它有多个操作模式。这些模式包括普通模式(normal mode)、插入模式(insert mode)、替换模式(replace mode)等。在普通模式下,按键用于导航和编辑操作,而在插入模式下,按键则直接输入文本。这种设计允许程序员根据任务的不同选择合适的模式,提高编辑效率。

  • Vim中的normal mode(普通模式)和insert mode(插入模式)有什么不同?

    -在Vim中,普通模式主要用于导航和编辑操作,如移动光标、删除字符、复制和粘贴文本等。插入模式则是用于输入文本,当进入插入模式时,按键会直接将字符输入到文本中。程序员可以根据当前任务的需要在这两种模式之间切换。

  • 如何在Vim中打开和保存文件?

    -在Vim中,可以通过命令行模式来打开和保存文件。要打开文件,只需在命令行中输入`vim`后跟文件名。要保存文件,可以在普通模式下按`:`进入命令行模式,然后输入`w`保存更改。如果想要退出Vim,可以输入`:q`来关闭编辑器,或者`:q!`来放弃所有更改并退出。

Outlines

00:00

📝 介绍与文本编辑器的重要性

本段落介绍了本次讲座的主题——文本编辑器,强调了作为程序员,我们花费大量时间编辑文本和程序,因此提高编辑效率能节省大量时间。讲师分享了自己对文本编辑器的热爱,并指出不同编程场景需要不同的编辑工具,例如 Vim、Emacs、VS Code 和 Sublime 等。此外,讲师还强调了学习文本编辑器的重要性,并介绍了今天讲座的主要内容,包括教程和练习,以及如何通过坚持使用和探索来掌握这些工具。

05:01

🌟 Vim编辑器及其多模式特性

这一部分详细讲解了Vim编辑器及其多模式特性。Vim是一个模式编辑器,具有多种操作模式,如普通模式、插入模式、替换模式等。每种模式都有其特定的用途和按键组合,例如在普通模式下,按键用于导航和编辑,而在插入模式下,按键则直接输入文本。讲师还提到了Vim的一些高级特性,如可视化模式和命令行模式,并解释了如何通过按键组合在不同模式间切换。

10:02

📖 Vim的基本操作和命令

在这一部分中,讲师介绍了Vim的一些基本操作和命令,包括如何打开文件、保存更改以及退出Vim。详细讲解了命令行模式下的命令输入方法,例如使用':w'保存文件,':q'退出Vim等。此外,还介绍了如何使用帮助系统来获取关于Vim命令的信息,以及如何通过键盘快捷键执行常用操作,而不必使用鼠标,从而提高编辑效率。

15:04

🔍 Vim的缓冲区、窗口和标签页模型

本段落深入探讨了Vim的缓冲区、窗口和标签页的概念及其工作方式。Vim维护一组打开的缓冲区,代表打开的文件,用户可以在多个标签页和窗口中查看这些缓冲区。每个标签页可以包含多个窗口,而每个窗口对应一个缓冲区。特别地,一个缓冲区可以在多个窗口中打开,这使得同时查看和编辑同一个文件的不同部分成为可能。此外,还讨论了关闭窗口和退出Vim的命令,以及如何使用':qa'命令一次性关闭所有窗口并退出Vim。

20:05

🛠 Vim的普通模式和编辑效率

这一部分重点介绍了Vim的普通模式,解释了为什么它是Vim最有趣的特性之一。普通模式允许用户通过编程方式与Vim接口进行交互,不同的按键组合有不同的效果,并且可以组合使用,就像编程语言一样。通过使用普通模式,用户可以快速移动光标、编辑文本和执行复杂的编辑任务。讲师还强调了通过普通模式进行编辑可以大大提高编辑速度,使得编辑文件的速度接近于思考的速度。

25:05

➡️ Vim的移动命令和编辑命令

在这一部分中,讲师详细介绍了Vim的移动命令和编辑命令。移动命令包括使用hjkl键进行光标移动、使用W和B键移动到单词的开头和结尾、使用0和$键移动到行的开头和结尾等。编辑命令则包括i命令进入插入模式、o和O命令打开新行、D命令删除文本、u命令撤销更改等。讲师还解释了如何使用可视模式来选择和操作文本块,以及如何使用修饰键如'a'和'i'来改变移动命令的行为。

30:06

🔄 Vim的复制粘贴、计数和修饰符

本段落讲解了Vim中的复制粘贴操作,包括y命令用于复制文本、p命令用于粘贴文本,以及如何结合移动命令来选择要复制的文本。计数功能允许用户通过数字前缀来重复命令多次。修饰符如'a'和'i'可以改变移动命令的行为,'a'代表around(环绕),'i'代表inside(内部)。讲师还提到了如何使用这些功能来提高编辑效率,并展示了如何通过组合不同的命令来执行复杂的编辑任务。

35:07

📚 Vim的可编程性和在其他程序中的应用

这一部分讨论了Vim的可编程性,包括如何通过修改vimrc文件来配置Vim,以及如何使用插件来扩展Vim的功能。讲师提到了Vim的配置文件vimrc,以及如何通过这个文件来开启语法高亮和行号等特性。此外,还介绍了Vim的插件系统,可以通过安装插件来增加如模糊文件查找、撤销历史可视化等功能。最后,讲师提到了Vim模式在其他程序中的应用,如Python REPL、终端和Jupyter Notebook等,可以通过启用Vim模式来提高这些工具的编辑效率。

Mindmap

Keywords

💡文本编辑器

文本编辑器是程序员编写和修改代码的主要工具。在视频中,特别强调了文本编辑器与普通文本处理软件的不同,因为编程时需要频繁阅读和编辑代码,而不仅仅是像写文章那样连续输入。

💡Vim

Vim 是一种在命令行界面中运行的高效文本编辑器,它通过模式编辑和键盘快捷操作来提高编程效率。Vim 拥有多种模式,如普通模式、插入模式和命令模式等,每种模式下按键的功能都不相同。

💡模式编辑

模式编辑是 Vim 编辑器的核心特性之一,指的是 Vim 根据当前所处的模式(如普通模式、插入模式)改变按键的功能。这种设计使得用户可以通过键盘快捷键高效地执行编辑任务,而不需要使用鼠标。

💡键盘快捷键

键盘快捷键是指通过组合键盘上的键来快速执行命令或操作。在 Vim 中,键盘快捷键被广泛应用于各种编辑任务中,从而提高编辑效率和速度。

💡效率

效率是指完成任务所需的时间和精力的多少。在视频中,提高编辑效率是通过学习和使用 Vim 这样的文本编辑器来实现的,它能通过减少不必要的鼠标操作和提供强大的键盘快捷操作来节省时间。

💡插入模式

插入模式是 Vim 编辑器中的一种模式,用户可以在这种模式下像在普通文本编辑器中一样输入文本。从普通模式进入插入模式后,按下的键将会直接插入到文本中,而不是执行其他编辑命令。

💡命令模式

命令模式是 Vim 编辑器提供的一种模式,用户可以在此模式下执行编辑命令,如保存文件、退出编辑器等。进入命令模式是通过按下 ':' 键实现的,然后用户可以输入相应的命令来执行操作。

💡光标导航

光标导航是指在文本编辑器中移动光标位置的操作。在 Vim 中,光标导航主要通过键盘快捷键完成,如使用 'hjkl' 键在普通模式下上下左右移动光标。

💡文本操作

文本操作包括在文本编辑器中对文本进行的各种编辑任务,如插入、删除、修改和复制粘贴等。Vim 提供了丰富的文本操作命令,可以通过键盘快捷键高效地完成这些任务。

💡撤销和重做

撤销和重做是文本编辑中的两个基本功能,允许用户取消之前的操作或重新执行已经撤销的操作。在 Vim 中,可以通过 'u' 命令撤销更改,通过 'Ctrl + R' 命令重做更改。

Highlights

文本编辑器对于程序员来说非常重要,因为编程工作很大一部分时间都在编辑文本。

不同于编写英语散文,编程时你会花很多时间阅读已编写的代码,频繁地在代码中导航和进行小的修改。

Vim 和 Emacs 等文本编辑器与 Microsoft Word 这类编辑英语散文的程序不同,它们专门为编程设计。

学习文本编辑器的关键是开始使用教程,然后坚持使用该编辑器完成所有编辑任务。

Vim 是一个功能强大的编辑器,虽然最初可能会降低你的编程速度,但坚持下去,你会逐渐提高效率。

Vim 是一个模态编辑器,它有多个操作模式,不同模式下按键的功能不同。

Vim 的正常模式(Normal Mode)用于导航和读取代码,插入模式(Insert Mode)用于输入文本。

Vim 有很多其他模式,例如替换模式(Replace Mode)、视觉模式(Visual Mode)和命令行模式(Command Line Mode)。

Vim 的设计理念是尽可能通过键盘操作,避免使用鼠标,以提高编程效率。

Vim 通过命令行模式可以执行各种命令,如保存文件、退出编辑器等。

Vim 的帮助系统非常强大,你可以通过 :help 命令获取关于特定命令或按键的帮助信息。

Vim 的缓冲区(Buffers)、窗口(Windows)和标签页(Tabs)的概念与其他编辑器不同,一个缓冲区可以对应多个窗口。

Vim 的正常模式允许你使用键盘快捷键进行快速编辑和导航,这使得你可以几乎以思考的速度编辑文件。

Vim 的界面本身就是一种编程语言,不同的按键组合有不同的效果,可以组合它们以执行复杂的编辑任务。

Vim 支持通过键盘快捷键进行复制和粘贴操作,这比使用鼠标更高效。

Vim 允许用户通过修改配置文件 vimrc 来定制编辑器的行为,以符合个人喜好。

Vim 可以通过插件扩展功能,这些插件可以提供模糊文件查找、撤销历史可视化、文件浏览器等增强功能。

Vim 模式也可以在其他程序中启用,如 Python REPL、终端、Jupyter Notebook 等,这使得你可以在不同环境中使用相同的编辑技巧。

Transcripts

00:00

Okay, cool. So, welcome to the third

00:03

lecture of the missing semester of your

00:05

si a second today we're going to be

00:07

talking about text editors this is a

00:09

topic that I really like I think it's

00:11

one of the most valuable topics that

00:13

we're teaching in this class because as

00:14

programmers you spend so much of your

00:17

time editing text editing programs that

00:19

if you invest time into making yourself

00:21

more efficient at doing this you'll save

00:23

a ton of time probably hundreds of hours

00:25

over the course of your undergrad or

00:27

over the course of your career so text

00:31

editors are a little bit different than

00:33

other programs you might use to edit say

00:36

things like English prose because

00:38

programming is different than writing

00:39

English prose when you're programming

00:41

you spend a lot of time reading what

00:44

you've written you spend a lot of time

00:45

navigating around a buffer and you spend

00:47

a lot of time making little edits to

00:49

code all over the place rather than just

00:51

writing in a long stream like you do

00:52

when you're writing an essay or

00:53

something and so it makes sense that

00:55

there different programs for these

00:56

different purposes right so yeah things

00:57

like Microsoft Word for writing essays

00:59

and things like vim and Emacs and vs

01:01

code and sublime for writing code so the

01:05

way you learn a text editor and become

01:07

really good at it is you start with a

01:09

tutorial and so that's basically going

01:11

to be the function of today's lecture

01:12

plus the exercises we've given you and

01:15

then after the tutorial you need to

01:17

stick with the editor for all your

01:19

editing tasks and when you're learning a

01:22

sophisticated tool so today we're going

01:24

to teach you vim which is one powerful

01:25

editor that a lot of programmers use

01:26

when you're learning such a

01:28

sophisticated tool it may be the case

01:30

that initially switching to this tool

01:32

slows you down a little bit when you're

01:33

programming but stick with it because

01:35

I'd say that in about 20 hours of

01:38

programming using a new editor you'll be

01:41

back to the same speed at which you

01:42

programmed using your old tool and then

01:44

after that the benefits will start and

01:46

you'll get faster and faster as you

01:47

learn more with these sophisticated

01:50

programs like them it takes not way too

01:53

long to learn the basics but a lifetime

01:55

to master and so throughout the time

01:58

you're using this tool make sure you

01:59

look things up as you go if you ever get

02:01

to a point where you're like oh this is

02:03

a really inefficient way of doing things

02:04

is there a better way the answer is

02:06

almost always yes because these text

02:09

editors were written by programmers for

02:11

programmers and so of course like the

02:13

peep

02:13

I wrote these tolls ran into the same

02:15

kinds of issues and fixed them so that

02:17

you don't need to deal with these

02:18

anymore and so yeah as you're learning

02:19

make sure you look things up as you go

02:21

either use Google or feel free to send

02:23

us emails if you have questions or come

02:25

to office hours and we'll help you

02:27

figure out how to do things really fast

02:29

so as far as which editor to learn in

02:32

previous iterations of this class we

02:34

actually avoided teaching a specific

02:35

editor because we didn't want to enforce

02:36

our opinions on you guys but we actually

02:39

think that it's really useful to teach

02:40

you how to use one particular tool and

02:42

use it well and so people have really

02:44

strong opinions about editor so you can

02:46

see the course notes for more links on

02:48

this topic looking at which editors have

02:52

been popular over the years Stack

02:54

Overflow I'm sure you've all heard of

02:56

that does a survey every year asking

02:59

developers various questions and one

03:00

thing to ask is which TextEdit Derk do

03:02

you use and it seems to be that

03:03

currently the most popular kind of

03:05

graphical editor is vs code and the most

03:07

popular editor that is based within a

03:10

command line interface is vim and so

03:12

we're going to be teaching you vim and

03:13

there a couple reasons for this one is

03:16

that all the instructors so me John and

03:17

Jose use vim as our primary editor and

03:19

we've been doing this for many years and

03:21

we've been very happy with it

03:23

and we think that there are a lot of

03:25

interesting ideas behind them so even if

03:28

you don't end up using this particular

03:30

tool in the long term I think it's

03:32

valuable to learn these ideas and also a

03:35

lot of tools have actually been really

03:37

excited about the ideas in vim and so

03:39

they support a vim emulation mode for

03:42

example vs code what's apparently the

03:44

most popular editor in use today

03:45

supports vim bindings and this vim

03:48

emulation mode as of now has like 1.4

03:50

million downloads and as you'll see over

03:53

the course of this lecture a lot of

03:56

different tools like including your

03:57

shell including things like the Python

03:59

repple and like Jupiter notebook and all

04:01

sorts of other things even your web

04:03

browser can support of emulation mode

04:04

and so yeah we're going to be teaching

04:06

you this really neat tool today and in

04:10

this lecture we can't really cover all

04:12

of them right it's a very powerful tool

04:14

but our goal is to teach you and the

04:17

core philosophy of them like the really

04:18

neat ideas behind it and then in

04:20

addition to that some of the basics like

04:22

how do you open a file close a file

04:23

navigate around a file make edits and

04:25

things like that and

04:27

you may not remember every single little

04:28

detail from this lecture because we're

04:29

gonna go pretty fast through some of the

04:31

material but it's all in the lecture

04:32

notes and then the exercises actually

04:34

give you links to some tutorials and

04:36

things so I highly recommend that you

04:37

actually go through all the exercises at

04:40

least the non advanced exercises any

04:43

questions so far great okay so one of

04:49

the really cool ideas behind them is

04:51

that vim is a modal editor what does

04:54

this mean modal comes from the word mode

04:56

and this means that vim has multiple

04:58

operating modes and this is kind of

05:00

developed from the idea that when you're

05:02

programming there often times where

05:04

you're doing different types of things

05:05

like sometimes you're reading code

05:07

sometimes you're making small edits to

05:09

code like you're finding a particular

05:10

point like changing a little thing

05:12

somewhere sometimes you're just writing

05:15

a lot of code in one go like suppose

05:16

you're just writing a function from

05:17

scratch and so there's different

05:19

operating modes for doing these

05:20

different types of things and so I'm

05:24

actually want to write this down on the

05:25

blackboard so I'll have a useful thing

05:26

to refer to later so when you start vim

05:28

up it starts up in what's called normal

05:33

mode and in this mode all the different

05:35

key combinations behave in one way and

05:37

then there are different key

05:38

combinations that switch you between

05:40

normal mode and other modes which change

05:42

the meaning of different keys so for the

05:45

most part you'll be spending most of

05:47

your time in vim in normal mode or what

05:49

is called insert mode and to go to

05:55

insert mode you press the key I for

05:57

normal mode and to go from insert mode

05:59

back to normal mode you press the Escape

06:01

key a little note on notation because

06:06

we'll need this later in the notation

06:09

I'm going to be using in this lecture

06:11

and what's also in the lecture notes and

06:12

what vim uses to give you feedback they

06:15

have a couple different ways of talking

06:16

about different keys so when they're

06:17

talking about bare keys like just the

06:19

eye key on your keyboard they'll just

06:20

say eye but for a different key

06:22

combinations like when you press control

06:24

and something like say control V it's

06:27

notated in one of like approximately

06:29

three ways one way that can be notated

06:32

is a caret and then the control

06:34

character so this is control V another

06:37

way this might be written I think we've

06:39

written it this way in

06:40

lecture notes as control-v this is

06:42

probably the one you're more used to

06:43

seeing and then in some parts of them

06:46

this is written as angle brackets C - V

06:48

close angle bracket so just a little bit

06:51

of notation that will be useful later

06:54

so yeah vim has a couple different modes

06:56

where normal mode is designed for

06:59

navigating around a file reading things

07:01

going from file to file things like that

07:03

and then insert mode is where you type

07:05

in text so most keys that you press here

07:08

will just go into your text buffer where

07:10

as keys that you press here are not

07:12

being put into the buffer and instead

07:13

are used for things like navigation or

07:15

making edits and actually the picture is

07:20

a little bit more complicated than this

07:21

there are a whole bunch of other modes

07:23

and I'm just gonna write them down here

07:24

because we'll have them here to refer to

07:25

later and so if it also has a replace

07:29

mode for rather than inserting text and

07:33

kind of pushing what's ahead of it

07:34

forward it will overwrite text and then

07:36

it has a bunch of different modes for

07:37

selection so it has a mode called visual

07:44

mode and then it has visual line and

07:48

visual block this one is enter via the R

07:53

key this was entered via the V key this

07:57

one is entered via shift B and this one

08:01

is entered via control V and then

08:05

there's the command line mode which is

08:13

entered via the colon key

08:19

okay so now that we have that on the

08:21

board to refer to later we can actually

08:31

try some of this out all right so one

08:35

thing we noticed looking at that picture

08:36

is that to go from normal mode to any of

08:38

the other modes we press some key but to

08:40

go from any of the other modes back to

08:41

normal mode where we spend a lot of our

08:43

time we use the Escape key on our

08:45

keyboard so for this reason since you

08:48

under pressing the Escape key a lot when

08:49

using vim a lot of programmers rebind

08:51

one of the keys on their keyboard to be

08:53

escape because it's really inconvenient

08:55

to reach up with your left pinkie to

08:56

press that tiny little Escape key in the

08:58

corner of your keyboard and so a lot of

08:59

people use the caps lock key instead so

09:01

it's right there in the home row and we

09:04

have some links in the lecture notes for

09:06

how you can do this key rebinding okay

09:14

so now that we've talked about kind of

09:15

one of the core ideas of vim the idea of

09:17

modal editing we can talk about some of

09:19

the basics like how do you open up this

09:20

text editor how do you open file save

09:23

files and things like that and so this

09:25

is a command line based program although

09:28

there are some graphical variants and

09:29

the way you start this program is by

09:31

running vim one thing you might notice

09:34

is that in the bottom left corner of my

09:35

screen they actually saw what I just

09:37

typed this will be useful later in this

09:40

lecture where I'm actually typing in

09:41

commands for vim and I'll be saying what

09:44

I'm typing but you'll also see it on the

09:45

screen so if I press ctrl C see it says

09:47

ctrl C over there is that text big

09:50

enough for everybody to read great okay

09:55

so the way we open vim is just by

09:57

running the program vim on our command

09:59

line and this comes pre-installed on

10:02

most systems and if you don't have it

10:03

you can install it using your package

10:05

manager vim can also take an argument if

10:07

we want to use it to edit a particular

10:08

file instead of just opening up the

10:10

program then later opening a file so for

10:12

example I have a file in this directory

10:15

this is actually the lecture notes for

10:17

this lecture so I can do vim editors MD

10:19

and press ENTER and then boom them is

10:21

started and then in this lecture I'm not

10:25

running vim in the completely Exton I've

10:29

configured a couple of things that

10:32

behave a little bit nicer by defaults

10:34

little things like having line numbers

10:36

on the left or having some more status

10:38

information on the bottom and if you

10:40

want to start with this default

10:41

configuration we have a link to this in

10:42

the lecture notes so you can get a

10:43

slightly more sane config by default. So,

10:49

once you've opened Vim, what do you do?

10:51

Well, as I said earlier, Vim starts in

10:53

normal mode, so if I just start typing

10:54

letters like, say, type X it didn't insert

10:57

X into the buffer. You can see the cursor

10:59

up in the top left: it actually deleted

11:01

one of the characters. That's because I'm

11:03

in normal mode, not insert mode. So insert

11:07

mode is basically what you're used to with

11:08

all the other text editors you've used

11:09

in the past, where there's a cursor

11:10

somewhere, you press the character, and it

11:12

just goes into your buffer. I'm instead

11:15

in vim you start a normal mode and you

11:18

can press I to go into insert mode so

11:19

see I've pressed I and then in the

11:21

bottom left notice that it says - -

11:23

insert the bottom left always tells you

11:26

what mode you're in unless this normal

11:27

mode in which case it's blank and now

11:29

that I'm an insert mode if I press the X

11:31

character for example see it just gets

11:33

inserted into my text buffer and I can

11:35

backspace over at type other stuff and

11:37

now my text editor kind of behaves like

11:39

you'd expect any other program to behave

11:41

so from this point how to go back to

11:43

normal mode if I want to stop inserting

11:45

characters. Yes, exactly. I press escape.

11:48

And, that's the symbol my keystroke

11:51

visualizer uses for escape, so

11:53

just be aware of that. And then, Vim has

11:58

this idea, that using the mouse is

12:01

inefficient, like your hands are on the

12:02

keyboard, moving your hand over to the

12:04

mouse takes a lot of time, right, like you

12:06

don't want to waste those couple of

12:08

milliseconds while you're programming,

12:09

like in the middle of things. So, instead,

12:11

all Vim functionality can be accessed

12:13

just through the keyboard. And, it's all

12:15

sorts of things you might be used to

12:16

doing like opening files by going, like,

12:19

file open, or file save, or things like

12:21

that, or instead access through the

12:23

keyboard. And, how is that done? That's

12:25

done through one of the other Vim modes

12:27

that are on the board over there. In

12:28

particular, through command line mode. So,

12:31

if you're in normal mode, and you press

12:33

the ":" key, you'll notice that the

12:36

cursor - I guess my visualizer is covering

12:39

the thing right now but it's gone - the

12:41

cursor jumps to the bottom - the bottom

12:43

left - and it shows that ":" I just typed.

12:46

Now, I can type in a command. So, you can

12:48

think of this almost like the command

12:50

shell that we've talked been talking

12:51

about over the last few days, except this

12:53

is Vim's

12:54

command shell, so you give Vim commands

12:55

here, instead of shell commands. And

12:57

there are a bunch of built-in commands

12:59

that do all the things that you're used

13:00

to. Like, for example, one command that you

13:02

might want to know is how to quit this

13:04

editor. You might notice that if you're in

13:05

normal mode, I can press "Escape" to go

13:07

back from command line mode to normal

13:08

mode, and I press "control C", unlike what

13:11

happens to a lot of programs, this doesn't

13:13

quit Vim. So, how do I quit Vim? I can

13:16

press ":", and then go into command

13:18

line mode, and then I can type in the

13:19

command "quit". "Q-U-I-T". You'll see that I

13:23

- maybe I should move this over to the

13:24

middle or something - see, it says ":quit"

13:28

and I press ENTER, and it quits Vim. I can

13:31

open Vim up again. There's actually a

13:32

short form for this command, just ":q", and

13:35

that'll do the same thing. And, there are a

13:38

bunch of other useful commands like this.

13:40

So, some other handy ones to know are

13:42

how do you save a file? So, suppose I make

13:45

some edits here, like "hello world".

13:47

So, I pressed "i" to go into insert mode - or

13:51

let me redo that - I press "i" to go into

13:53

insert mode. Right now, I can use the down

13:55

arrow to... I think I've slightly I

14:02

should fix that. Can you fix the config,

14:03

actually, John? Never mind that.

14:07

Okay, so, suppose I go down to this line,

14:11

and I press "i" to go into insert mode, and

14:14

type in some text, and then press "escape"

14:17

to go back to normal mode. Now, how do I

14:18

actually save this file? Well, there's

14:20

another command for that. So, ":" to go

14:22

into command mode, and then I can type "W"...

14:25

...and press "Enter". "W" stands for write. And,

14:30

it says in the bottom "editors.md"

14:33

whatever blah blah written. And, so, this

14:35

means it saved the file and so now if I

14:37

":q" to quit and open the same file,

14:40

again, you'll see that the changes have

14:42

been persisted. There are a couple other

14:46

there's... So, there's a ton of different

14:49

Vim commands that are useful for

14:50

different reasons. But, I'll just explain

14:53

a couple more to you now. One command

14:56

that's really useful is help. ":help"

14:59

And you can do ":help", and then type in a

15:01

particular key, or a particular command,

15:04

and get help for that keystroke or that

15:06

command. So, if I want to know what

15:08

":w" does, I

15:10

can do : help : W, and that'll give me

15:15

the documentation on : w or : write. If I

15:21

do : q, it'll close this window and bring

15:23

me back to where I was before.

15:25

And, notice that ":help :w" is different

15:28

from ":help w", because the W key is the W

15:35

that, like, when you're in normal mode and

15:38

press W, what happens is just the W key

15:40

here without the ":".

15:41

And, if I look for help for ":w", that's

15:43

the help for the W command. So, now you

15:50

basically have the bare fundamentals

15:56

needed to use them. right? You can open

15:58

the editor, use it to edit a particular

16:00

file, press "i" to go into insert mode and

16:03

type in some text, press "escape" to go

16:05

back to normal mode, and then :w to save

16:06

your changes, :w to quit. So, like already

16:09

you have the bare fundamentals necessary

16:11

to edit files using Vim, albeit somewhat

16:13

inefficiently. So, any questions so far?

16:17

Yeah in the back. Yeah so the question is,

16:23

What's the benefit of the

16:25

normal mode? And, we'll talk about that in

16:27

more detail, in like five minutes.

16:28

But, in short, insert mode is just for

16:31

typing in text. So, I'm in insert mode, I

16:33

can type in text. But, when I'm

16:34

programming, I actually spend a lot of

16:36

time moving around my file making small

16:39

little changes. So, I go here and like, oh

16:40

maybe I want to change this HTTPS link to

16:42

an HTTP.

16:45

I can make like small point edits, things

16:48

like that, in normal mode. And we'll see a

16:49

whole lot more of that in about five

16:51

minutes. Good question!

16:52

Any other questions? Okay cool. So, moving

16:59

along, one other thing that's kind of

17:05

useful to know, I think, is, at a high

17:11

level, Vim's model of

17:12

buffers versus windows versus tabs. So,

17:15

it's probably the case that whatever

17:16

program you were using before, like

17:17

Sublime Text or VS Code or whatever,

17:19

you could open multiple files in it,

17:21

right, and you could probably have

17:23

multiple tabs open and have multiple

17:24

windows open of your editor. So, Vim also

17:27

has a notion of those different things.

17:29

But, its model is a little bit different

17:31

than most other programs. So, Vim

17:34

maintains a set of open buffers - that's

17:36

the word it uses for open files - and so,

17:38

it has a set of open files, and then kind

17:40

of separately from that, you can have a

17:42

number of tabs, and tabs can have windows.

17:44

The kind of weird thing which makes Vim

17:46

a little bit different than the

17:47

program you've probably used in the past is

17:48

that there isn't necessarily a

17:50

one-to-one correspondence between

17:52

buffers and windows. So, one thing I can

17:55

do, for example, here - and we'll show you

17:56

the key combinations and stuff for this

17:58

later - but one thing you can do is create

18:00

two different windows. So, I have one

18:02

window up here, and then one window down

18:04

here. And, notice that the same files open

18:07

in both windows. So, if I make some edits

18:08

over here, they actually happen in the

18:10

bottom window, as well, because it's the

18:11

same buffer that's open in both windows.

18:14

And, this is kind of useful, for, say,

18:16

looking at two different parts of a

18:18

single file at the same time. Like, so you

18:19

want to be able to look at the top of a

18:20

file, say at an import to your program,

18:22

while you're down below, working

18:25

somewhere else. So, this is one helpful

18:27

thing to keep in mind, that Vim has this

18:29

idea of - there are a number of tabs, and

18:32

each tab has some number of windows, and

18:35

then each window has, uh, corresponds to

18:38

some buffer. But, a particular buffer can

18:40

be open in zero or more windows at a

18:42

time. Just one thing that confused me

18:44

when I was initially learning Vim, so I

18:45

want to explain that early on. And then,

18:48

the ":q" command, which we talked about

18:50

earlier, is not exactly quit. It's kind of

18:53

"close the current window", and then, when

18:55

there are no more open windows, Vim will

18:56

quit. So, here, if I do ":q", it'll only

18:59

close the window, I think, on the top here

19:01

because that's the one I was in, and, now,

19:03

the remaining window becomes fullscreen.

19:05

I can do : Q again to close this. Now we're in

19:08

the second tab that I'd opened. If I do :Q

19:10

a final time, okay, now, Vim exits. And if

19:14

you don't want to press ":q" way too many

19:16

times... Okay, so, here I have three split

19:17

windows. If I do ":qa", for quit all, it

19:21

closes all the open windows.

19:25

All right, so, now, to answer your question of

19:28

"What is normal mode actually for?" This is

19:30

another, really cool idea in Vim, and I

19:32

think this is actually, like, the most

19:34

fundamentally interesting idea of this

19:36

program. It's that, like, you're all

19:38

programmers, you like programming; Vim has

19:42

this idea that Vim's normal mode, like,

19:44

Vim's interface, itself, is a programming

19:47

language. And, let me repeat that.

19:49

That's like a kind of fundamentally

19:51

interesting idea: the interface is a

19:52

programming language.

19:54

What does that mean? It means that

19:56

different key combinations have

19:57

different effects, and, once you learn the

19:59

different effects, you can actually

20:00

combine them together - just like in a

20:02

programming language - you can learn

20:03

different functions and stuff and then

20:04

glue them all together to make an

20:05

interesting program. In the same way, once

20:08

you learn Vim's different movement and

20:09

editing commands, and things like that,

20:10

you can talk to Vim by programming Vim

20:13

through its interface. And, once this

20:16

becomes muscle memory, you can basically

20:19

edit files at the speed at which you

20:20

think. Like at least for me, I don't think

20:22

I've been able to do this with any other

20:23

text editor that I've used in the past,

20:25

but this one gets pretty close. So, let's

20:29

dig into how exactly normal mode works.

20:31

So, you can try to follow along with this,

20:34

like, open up some random file in Vim, and

20:36

follow some of the key combinations I

20:38

type in. So, one basic thing that you

20:40

might want to do, is just navigate around

20:42

a buffer. Like, move your cursor up/down/

20:44

left/right. And, so the way you do that in

20:46

Vim, is using the hjkl keys, not the arrow

20:48

keys. Though they do work by default, try

20:50

to avoid them, because you don't want to

20:52

have to move your hand all the way over

20:53

to the arrow keys. Like, there's a ton of

20:54

time you're wasting, right? HJKL is right

20:57

on the home row. And, so, J moves down, K

21:00

moves up, H moves left, and L moves right.

21:04

And, this may seem a little unintuitive

21:06

now; there was some historical reason for

21:08

it, like, the keyboard the original vi

21:10

developer used had the hjkl keys, like,

21:13

labeled, and arranged in a way that made

21:14

this more reasonable. But, this will very

21:17

soon become muscle memory. So, this is the

21:19

basic way you can move your cursor

21:20

around while in normal mode. Now, what

21:24

else can you do? Well, if we had to move

21:26

around files like this, it'd be really

21:27

slow. We don't want to have to

21:29

hold down these keys, and like, wait for a

21:31

long time for Vim to do its thing. And

21:33

so, there are all these other, different

21:34

key combinations for doing different

21:35

movements.

21:36

Also, by the way, this is all in the

21:38

lecture notes, so you don't need to

21:39

memorize every single key and its

21:40

meaning right now. Just try to understand

21:43

the overall idea that Vim's interface is

21:45

a programming language. So, another thing

21:47

you can do is press the W key. This moves

21:50

the cursor forward by one word. And then,

21:52

similarly, the "B" key moves the cursor

21:54

backward by one word. So, this allows

21:57

slightly more efficient movement within

21:59

the line. There's also the "E" key for

22:01

moving to the end of a word. I'm going to

22:04

move this over a little bit. So, if I'm

22:06

here, for example, and I press the "E" key

22:08

it'll go to the end of this word, end of

22:09

this word, end of the next word and so on.

22:12

You can also move by whole lines, so zero

22:15

moves to the beginning of a line, dollar

22:17

sign moves to the end of a line, and the

22:19

caret key moves to the first non-empty

22:22

character on a line. So, let me find one

22:25

of those, for example. So, here, my

22:31

cursor's right here; if I press 0, my

22:33

cursor goes to the beginning of the line,

22:34

dollar sign, end of the current line; and

22:37

if I press caret, where, like, on what

22:39

character will the curser end up? Can

22:41

anybody guess? So, caret goes to the first

22:44

non-empty character on a line, kind of

22:47

like Regex caret. Yeah, exactly! It goes

22:53

to this dash. Let's talk about some more

22:57

movement commands. There're ways to

22:59

scroll up and down in a buffer, so

23:01

control U goes up, and control D scrolls

23:06

down. So, this is better than holding down

23:08

the K or J keys, for example. This is a

23:10

lot slower than moving by entire pages.

23:13

Control D and control U. There's also

23:17

ways to move by the entire buffer. So,

23:19

capital "G" moves all the way down... "gg"

23:22

moves all the way up. Some of these

23:26

movement keys are mnemonics; so, they're

23:28

like, a little bit easier to remember for

23:29

that reason, right, like, "W" is word, "B"

23:31

is beginning of word, E is end of word. Those

23:33

all seem pretty logical. 0, caret and

23:36

dollar, kind of inspired from Regex, so

23:38

those make a little bit of sense. There's

23:39

some other ones that, like, don't

23:41

necessarily make way too much sense, but,

23:42

there are only so many keys on your

23:43

keyboard, so what are you going to do? For

23:46

example, the "L" key moves your cursor to

23:48

the lowest

23:49

line that's shown on the screen. "L" for

23:51

lowest makes sense, M for middle, and then H for

23:54

highest, I guess. And, there's a whole

24:01

bunch of other interesting movements

24:02

like this. So, we're obviously not going

24:04

to be able to cover all of them right

24:05

now, but you'll be able to go through

24:07

them in the Vim tutor exercise, which is

24:08

exercise number one for this lecture.

24:11

Some other ones I want to talk about now -

24:15

maybe I'll talk about one more. There's

24:18

another movement called "find". This is

24:19

also kind of useful. Suppose I'm on this

24:21

line, and I want to jump to the first

24:23

character that equal's to... Like, I

24:25

want to jump to the first "o". I can press

24:27

"fo", and my cursor moves to the first

24:31

"o". I've like, found "o". I can do fw and

24:33

it'll move to the first "w", which I think

24:35

is right here. fc: find the first C. I can

24:39

also do the same thing, but backwards. If

24:40

I do capital F, w, I can find the W that's

24:44

before it. Capital F, s: find the s that's

24:47

before that. And then, there's a variant

24:49

of f, for find: t for to, so I can jump to

24:53

O, and it jumps, like, until it's found o.

24:56

But not on top of it, right before it. And

24:58

capital T say, t, jumps backwards to the t

25:03

except not all the way on top of it, one

25:04

character before. And, so, you can already

25:07

see that idea I talked about, of like, Vim

25:09

is a programming language; you can, like,

25:10

compose these commands. "F", and "T", are

25:13

"find", and "to", and you can say "find" a

25:15

particular character, or jump "to" a

25:16

particular character. So, those are a

25:20

couple of Vim movement commands. So, any

25:22

questions about those so far? So this is -

25:26

yeah, question? Or... no? Okay, cool. So,

25:32

those are Vim movement commands.

25:33

This is how you can navigate around a

25:35

file quickly in normal mode. Now, another

25:37

category of useful commands are editing

25:40

commands. So, one we kind of already

25:42

talked about is the "i" command

25:44

for moving from "normal" mode to "insert"

25:46

mode, where you can start just writing

25:48

text. So, suppose I go up here and I press

25:50

"i". Now I can type in whatever text I want

25:52

"Hello world", "enter". Then, press

25:57

"escape" to go back to normal mode, and

25:58

I've made a change to my buffer. But,

26:00

there are a whole bunch of other

26:01

commands for making efficient edits

26:03

that makes sense for when you're dealing

26:05

with programming languages. So, one useful

26:07

command that I accidentally used earlier,

26:09

before teaching you about it, is the "o"

26:10

command. So, suppose my cursor is, like,

26:13

over here, and if I press "o", from normal

26:16

mode, what it does, is it opens a new line

26:18

below where my cursor is.

26:19

That's what "o" stands for. And it, so it

26:22

creates a new line, and it put me into

26:23

insert mode. So, now I can start typing in

26:24

some text, press escape, and go back to

26:27

normal mode. And then, just like the "o"

26:29

command, there's a capital "O" command, so

26:32

if I'm here and I do capital "O", it puts

26:35

me into insert mode above where I

26:37

currently am. There's another Vim command

26:41

for deleting things. So, suppose my cursor

26:44

is, like, on top of this word right here,

26:47

and I press the D key. "D" for delete. Oh,

26:51

nothing happens; turns out that the D key

26:54

needs to be combined with a movement

26:55

command. So, remember we just talked about

26:57

different movement commands, like hjkl,

26:59

and, like, word, and backward word, and

27:02

things like that.

27:03

So, I press D. Whoops. I press D and I can

27:07

press W, and it's deleted a word. So, let

27:11

me undo that. Undoing in Vim is just u for

27:13

undo. So, notice my cursor's right here. I

27:16

do "dw": it's deleted a word. I can move

27:19

around, and then delete another word.

27:21

Suppose I'm - uh, keeps getting in the way

27:25

Suppose I'm, like, somewhere in the middle

27:27

of a word, and I want to delete to the end

27:29

of a word. Any guesses for what

27:31

combination of keys I'd use for that? "d"

27:35

and what? de, exactly. Delete to the end of

27:39

the word. Another useful editing command

27:43

is the c command. c stands for change. So,

27:46

change is really similar to delete,

27:48

except change puts you in insert mode,

27:50

because, like, I want to delete a thing,

27:52

but change it to something else. So, if

27:54

I'm here, and I do "ce", it's like, change

27:56

to the end of the word. And, it gets rid

27:59

of the contents until the end of the

28:00

word, and notice it put me in insert mode.

28:02

So now, whatever characters I type go

28:04

into the buffer. If I press "escape", I go

28:06

back into normal mode. And so, c and d are

28:11

analogs: they both take motions as

28:13

arguments. And, they will either delete

28:15

that motion, or change

28:17

that motion. So, for example, if you press

28:20

the c key, there's also this pattern that,

28:24

if you press a particular editing key

28:25

twice, it'll have that effect on the

28:27

given line. So, if I press "dd", that

28:30

deletes the line. If I press "cc", that

28:32

deletes the given line, but puts me in

28:34

insert mode, so I can replace it with

28:35

some other line. We'll cover a couple

28:39

other, uh, editing commands, because then

28:43

later we'll see how all these things

28:44

interact together. So, another useful one

28:46

is the x command. So, suppose my cursor is

28:48

over some particular character. If I

28:50

press "x", it just deletes that

28:52

character. There's another command called

28:57

r. If I'm over a particular character, and

29:00

I press "r", it takes another character as

29:02

an argument, and it replaces that

29:03

particular character with some other

29:05

character. And, I'll cover a couple more

29:12

editing commands. So, I think one I

29:14

talked about a moment ago - but, of course

29:16

you can undo changes you've made in Vim.

29:17

And the way you do that is by pressing

29:19

"u" while you're in normal mode. So, u for

29:21

undo is pretty easy to remember. So, I

29:23

press "u" a whole bunch of times; it's

29:24

undone all the changes I've made. And

29:26

then, the opposite of undo is, of course,

29:28

redo. And, the binding for that in Vim is

29:31

control + R. All right, one other editing

29:40

command I'm going to talk about is copy

29:42

and paste because-oh yes, question? That's

29:55

a - that's a great question! So, the

29:56

question is, "Does 'undo' undo everything

29:58

you've done since you've gone into

29:59

insert mode, or just the last character?"

30:00

It's - it's actually a little bit more

30:02

complicated than that.

30:03

"Undo" does, like, undoes the last change

30:05

you've made. So, if you went into insert

30:07

mode, and typed in some stuff, and went

30:08

back into normal mode, and then press "u"

30:10

for "undo", it'll undo all you've done in

30:12

insert mode. But, if you've done some

30:14

other type of editing command, like, say I

30:16

press "x" to delete a character... If I do

30:18

"u" for undo, it'll just undo that change

30:20

that that editing command made. Now, does

30:24

that answer the question? (Yeah) Great any

30:26

other questions?

30:28

Cool. So, I'll talk about copy and paste

30:32

as well, because that's a popular one. The

30:35

y command stands for copying, and the p

30:37

command stands for pasting. y for "copy",

30:39

because, yank. Like, that's the word they-

30:41

that's the terminology that

30:43

Vim uses for copying. And, these commands

30:46

are- y also takes a motion as an argument.

30:49

So if I do like, yy, it copies the current

30:52

line. And, if I press "p" for "paste",

30:54

notice that now these two lines are

30:56

identical, because I've just pasted a

30:57

line below. "u" for "undo". But if I do

31:00

something like "yw", it's copied the word.

31:02

And then I can do "p", and it just pasted

31:04

that word again,

31:05

right where my cursor was. One useful

31:08

thing, especially in the context of copy

31:10

and paste, is to be able to select a

31:12

block of stuff and copy it, right? Like,

31:14

this is probably how you used copy and

31:15

paste in whatever editor you were using

31:16

before. And so, that's where we get into

31:19

the visual modes. So, these are another

31:21

set of modes that are all related to

31:23

each other, and that can be reached from

31:24

normal mode, and they're used for

31:26

selecting chunks of text. So, one mode is,

31:28

just, regular visual mode. You can enter

31:31

that by pressing v. And then, once you're

31:33

in this mode, you can use most of the

31:34

regular normal mode commands to move

31:36

your pointer around. And it selects

31:38

everything in between. So I can use, like,

31:39

hjkl just to move the cursor, or I can

31:42

use "w" to move by words, or different

31:44

things like that, and it will select a

31:45

block of text. And, once I've selected

31:48

this block of text there are a whole bunch

31:49

of different types of useful things you

31:50

could do with it. One of the most popular

31:52

things to do is copying this. So, once

31:54

I've selected, I can do y to copy, and it

31:57

puts me back into normal mode. And now,

31:59

it's copied this to the - to the paste

32:01

buffer. And then if I go somewhere else,

32:02

and press "p", it pastes in that whole

32:04

chunk of text I copied. And it's similar

32:07

to visual mode, which selects kind of a

32:10

contiguous stream of text. There's visual

32:13

line mode: that can be reached by

32:15

pressing capital V, and that selects

32:17

whole lines at a time. And then there's

32:19

VISUAL BLOCK mode, which can be selected

32:21

by pressing "control" + " V", and that can

32:23

select rectangular blocks of text. So

32:24

this is something your old editor

32:26

couldn't do. Alright, so, there's a lot

32:31

more Vim editing commands to learn.

32:33

There's lots of, like, really weird and

32:35

fancy things. Like, for example, the tilde

32:36

command changes the case of the

32:38

character, or the selection that you've

32:40

currently selected. So for example,

32:41

I can take this, like, Visual Studio Code,

32:43

and flip the case on the whole thing, by

32:45

selecting it and pressing tilde. And,

32:48

there's a whole bunch of other things

32:49

like that; they get more and more

32:50

esoteric as you go. So, we're not going to

32:52

cover all of those, but you'll get to

32:54

those in the exercises. So, those are Vim

32:56

editing commands, and a lot of them can

32:58

be composed with movement commands. So,

33:00

any questions about either of those so

33:02

far? Cool. So, moving along, another

33:09

category of things -of commands- that are

33:12

mostly relevant to normal mode are

33:14

counts. So, you can give them a number, to

33:16

do a particular thing, some number of

33:18

times. So suppose my cursor is here, and I

33:22

want to move down, like 1 2 3 4 lines. One

33:25

way I can do that is by pressing "j" four

33:26

times - go down four times. "kkkk" goes

33:29

up four times. But, rather than pressing a

33:32

particular key again, and again, I can use

33:34

a count. So if I press "4", "j", it does j

33:37

four times, right?

33:38

Vim's interface is a programming

33:40

language. If I do "4k", it moves up four

33:42

times. If I am here, and I press "v" to go

33:45

into visual mode... Okay so now I can move

33:47

my cursor around, and select blocks of

33:49

text. I can do, like, "eee" to select a

33:51

couple of words, but, I could also go back

33:53

here -v for visual mode- and press three e

33:56

to select, like, three "ends of words"

33:59

forward. And then of course these can also

34:03

be combined with editing commands. So,

34:05

like, suppose I want to delete seven

34:08

words. I can do that by moving my cursor

34:10

somewhere, and doing "7dw". Seven delete

34:13

words. And so, this is particularly useful

34:17

for things like, suppose my cursor is

34:19

somewhere on the screen, and I'm looking

34:21

somewhere else on the screen, or, I want

34:22

my cursor to go to that particular line.

34:24

Notice that I've set up relative line

34:26

numbering on the left. So, wherever my

34:28

cursor is, it shows the current line

34:29

number, but everywhere else, it's just the

34:31

offset from where I am. Now, suppose my

34:33

cursor is here, but I want to move down

34:35

to the like "Microsoft Word" down here, so

34:38

that's eight lines down. So, what

34:40

combination of keys would I press, to do

34:41

that? Like, what's the most efficient way?

34:44

Yeah, exactly!

34:45

Let's try that out-8j-and my cursor moved

34:47

down to this line.

34:51

Okay. And then, one, final category of key

34:56

meanings in Vim is something called

34:57

modifiers. So we have, so far, movement,

34:59

edits, counts, and, finally, we have

35:02

modifiers. So, modifiers kind of change

35:04

the meaning of a movement command a

35:06

little bit. And, a couple modifiers that

35:08

are especially useful are the "a" and "i"

35:11

modifier. So, a stands for like around and

35:15

"i" stands for inside. And, to see where

35:18

this is really useful, I can move my

35:19

cursor to somewhere like here, for

35:21

example.

35:22

So, hopefully, most of you are familiar

35:24

with markdown syntax - and if not it

35:26

doesn't matter too much. Uh, this is a

35:28

link in markdown; it's a text rendered in

35:30

square brackets, and then the link in

35:31

parentheses. Suppose my cursor is inside

35:33

here, and I want to change the text

35:35

corresponding to this link. Well, one way

35:38

I could do that is, like, move back here

35:40

with b, and, like, 2dw, and then "i" to go

35:43

into insert mode. That's one of the

35:45

many ways I can make this change, and I

35:46

can type in whatever other thing I want -

35:47

u to undo, u to undo. Another way I could

35:51

have done that is change two words - "c2w"

35:53

- and then type in some other text. But,

35:58

one final way I could do the same change

35:59

is using the modifier commands to talk

36:03

about how I want to interact with these

36:05

different types of grouping things like

36:07

parentheses and square brackets. So, one

36:09

final way of doing this is change inside

36:12

square brackets-"c" "i" "["-and that puts

36:15

me into insert mode, after deleting the

36:17

contents that are inside the brackets. So,

36:20

do you see how we can take all these

36:21

different ingredients, like we talked

36:22

about "change", and we could combine that

36:24

with different movement commands. We

36:25

talked about inside, how it's a modifier.

36:27

And then we talked about, uh... we didn't

36:30

talk about parentheses. But, if your

36:32

cursor is hovering over a different, uh,

36:34

different types of grouping things like

36:36

parentheses, or square brackets, you can

36:37

press the percent movement key to jump

36:39

back and forth between matching

36:40

parentheses. If I go over here and I do d,

36:45

i, (, I can delete the contents

36:49

inside these parentheses. And so, those

36:52

are Vim, uh, modifiers. I guess we talked

36:55

about i, but we didn't talk about a. If I

36:57

do "da(", it deletes a whole,

37:00

like parenthesized group including the

37:02

parentheses so I is inside

37:04

is around or including all right so

37:10

those are basically the different

37:11

categories of things you can combine

37:13

together, when interacting with Vim's

37:15

interface. So, any questions about that or

37:18

the overall idea of this interface being

37:20

a programming language? Cool. So, let's do

37:28

a quick demo, to kind of demonstrate the

37:31

power of this editor. And, it will kind of

37:33

help us see how this tool can work

37:36

really fast and kind of match the speed

37:38

at which we think. So, over here is a very

37:41

broken "fizzbuzz" implementation that

37:43

doesn't actually print anything.

37:44

Uh, hopefully, most of you have heard of

37:46

"fizzbuzz" - if not, I'll explain it super

37:48

briefly. Uh, "fizzbuzz" is a programming

37:51

exercise where you print the numbers 1

37:52

through n, but when the number is

37:54

divisible by 3, you print fizz - when it's

37:56

divisible by 5, you print buzz. And, when

37:58

it's divisible by both 3, and 5. you print

38:00

fizzbuzz. And, if none of those apply, you

38:02

just print the number. So, you should

38:05

print like 1, 2, fizz, 4, buzz, and so on.

38:09

But, if I run this program, it doesn't

38:13

print anything here I have them on the

38:16

left in just a terminal on the right ok

38:20

so there's a bunch of issues with this

38:21

one is that main is never called so

38:23

let's start off with fixing that so

38:26

here's how I would make this change and

38:29

notice how few keystrokes this requires

38:31

capital G means go to the bottom of the

38:34

file o opens a new line below and now I

38:37

can just type in stuff so I'm an insert

38:39

mode okay so I've typed in whatever

38:45

change I want to make escape to go back

38:47

to normal mode if I do : W so command

38:50

mode right let me go back here okay now

38:53

at least my program prints something

38:54

when I run it another issue with this

38:56

program is that it starts at 0 instead

38:58

of 1 so let's go fix that so I want to

39:02

go over to this range whoops this range

39:05

thing and it shouldn't be going from 0

39:06

to limit it should be going from 1 to

39:08

limit plus 1 1 command which I didn't

39:11

show you about is how you search in vim

39:12

so you press forward slash

39:17

to close this and restart it if you

39:19

press forward slash it starts search so

39:21

if I type in range enter my cursor goes

39:24

from wherever it was before to the first

39:26

instance of range it found so it's a

39:27

really efficient way of moving where I

39:28

want to move WW to move forward two

39:31

words I to go into insert mode add the

39:33

one comma space escape I'm back in

39:36

normal mode this is a very common

39:38

pattern in vim you stay in normal mode

39:40

you go somewhere you go into insert mode

39:41

you make a tiny change and you jump

39:43

right back to normal mode like normal

39:44

mode is home and that's where you should

39:45

be most of the time I also want to add a

39:48

plus one so e to go to the end of this

39:51

word a for a pend plus one escape

39:55

alright fix that problem and other issue

39:58

is that this program prints fizz for

40:00

both divisible by three and five so

40:02

let's fix that slash fizz searches for

40:05

fizz been oppressed and it goes to the

40:07

next match see I press E I quote changes

40:11

what's inside the quote so it's deleted

40:13

the fizz and put me in insert mode right

40:15

in between those two quotes and I can

40:17

type in whatever I want

40:18

escape to go back to normal mode so

40:23

great I've fixed that particular problem

40:26

another problem with this program is

40:28

that it prints fizz and Buzz on separate

40:31

lines for multiples of 15 so let's let's

40:37

go and fix that let me go down to this

40:42

line here

40:42

one way I can don't actually worry about

40:44

like the actual contents of this program

40:46

like this some stupid program that

40:47

doesn't matter pay attention to what

40:48

keys I'm pressing in vim that allow me

40:51

to make changes to this program really

40:52

efficiently so my cursor is on this line

40:54

i press dollar to go to the end of this

40:56

line i for insert mode okay and i'm

40:59

typing some stuff escape to go back to

41:01

normal mode now I want to make the same

41:03

change the print below look at this JJ

41:07

dot so what dot dozen vim is it repeats

41:11

the previous editing command that was

41:13

made and so this is a really nice way of

41:15

doing repetitive tasks without typing

41:17

the same thing over and over again so in

41:19

that particular case that inserted comma

41:21

end quote and so it applied the same

41:24

thing on this line when I press dot and

41:27

then when I guess one final part of this

41:29

demo is

41:30

we will fix the issue that this program

41:32

maybe should take a command-line

41:34

argument instead of having this

41:35

hard-coded 10 down here so how do we do

41:38

that I'll press GG to go to the top

41:40

capital o so now I've opened a line

41:43

above and I'm going to type in some text

41:44

like imports this enter escape to go

41:47

back to normal mode and then I want to

41:49

go down to where this 10 is so slash 10

41:52

makes me jump straight down there CI

41:54

pren to edit what's inside the

41:57

parentheses and now I can type in like

41:59

whatever thing I need to type in here

42:02

and then once I've done this my program

42:07

does fizzbuzz correctly I think I missed

42:09

one change I wanted to make but it

42:10

doesn't matter this demonstrates that

42:12

you can make lots of changes really fast

42:14

so any questions about this demo or the

42:17

overall idea we've been talking about ok

42:23

so this will be covered Tuesday so the

42:27

kind of outside environment I'm running

42:29

vim on the left and my shell on the

42:31

right and then this is team ox on the

42:33

outside one variant of that question

42:38

might be like how do you switch between

42:39

different vim windows and you can see

42:41

the lecture notes for that but there's a

42:42

key binding for that so if you have the

42:44

same window open or multiple things open

42:45

there's a way of doing that question ah

42:50

good question so delete takes a motion

42:54

and then removes those contents but

42:56

keeps you in normal mode so you can keep

42:58

just moving around in a file what change

43:00

does is very similar to delete it takes

43:02

motions and treats them in the same way

43:03

deletes those contents but then puts you

43:06

in insert mode and so it saves you from

43:07

typing one extra keystroke so if I'm

43:11

here for example I want to delete main

43:13

DW deletes a word but now if I press

43:16

whatever key likes I press J it just

43:18

moved me down if I undo that do cw4

43:21

change a word now it's actually put me

43:23

into insert mode and I can type in

43:24

whatever I want it to insert so DWI is

43:28

the same thing as CW but it saves a

43:31

keystroke one thing we've linked in the

43:33

resources is something called vim golf

43:35

basically people have set up a game

43:37

online where you can get an editing task

43:39

and try to figure out the minimal number

43:41

of keystrokes necessary to complete that

43:43

editing

43:44

it's actually really addictive so I'd

43:46

only suggest going on their chest and

43:47

script time I think I saw a hand for

43:52

another question yeah uh period yeah one

44:00

of the most useful of em commands good

44:02

question any other questions

44:06

cool so I think we have about five

44:10

minutes left and I'm gonna briefly talk

44:13

about a thing that's also covered in

44:14

detail in the notes so make sure you

44:15

look at the notes for this so vim is a

44:19

programmers text editor and so of course

44:22

it's highly programmable not only

44:24

through its interface that's a

44:25

programming language but also a couple

44:27

of different ways there's lots of

44:29

settings that you can tweak to match

44:31

your preferences and you can also

44:32

install plugins for them that do all

44:34

sorts of useful stuff so the way vim is

44:37

configured is through a file on disk

44:40

called vim RC and you'll see this is a

44:42

common pattern in a lot of shell based

44:45

tools there'll be a plain text file that

44:47

configures how the Tool Works and so if

44:49

I edit this file and it may or may not

44:50

exist on your machine yet but I've

44:53

downloaded the we've created a kind of

44:56

default vim RC for you and linked it on

44:58

the course website so you can start with

44:59

that one if I do vim tilde slash boom RC

45:02

I can see here a bunch of comments and

45:04

then particular commands like by default

45:07

we want syntax highlighting on and we

45:09

want line numbers if we didn't do some

45:11

of these things like let me remove the

45:12

stuff that sets line numbers if I remove

45:14

those configurations and relaunch vim

45:16

notice that I no longer have line

45:17

numbers on the left but yeah so in short

45:21

there's a lot of stuff you can configure

45:23

with them we've given you a very basic

45:26

configuration that tries to like remove

45:28

some of the kind of weird behavior

45:29

that's on by default in vim but we don't

45:32

really try to enforce too many of our

45:33

other opinions on you but of course like

45:35

the three of us used them a lot and we

45:37

have heavily customized VMR C's so we've

45:39

linked to our personal configurations

45:41

too if you want to take anything from

45:42

that and also like thousands or like

45:44

millions of people share their var C's

45:46

on github so there's lots of places to

45:47

look for inspiration

45:48

there's also cool blog posts on this

45:49

topic another thing you can do in vim is

45:53

you can extend it with plugins that do

45:54

all sorts of useful things this lets you

45:56

do things like fuzzy file

45:58

finding which a lot of text editors come

45:59

with by default so you can get like a

46:00

pop-up window you can type in a name of

46:02

a file or approximately the name of a

46:04

file and find it very quickly or there

46:07

are things that show you like

46:08

visualizations of undo history there are

46:10

things that show you like file explorers

46:14

things like that so we've linked to a

46:16

couple of our favorite plugins on the

46:18

course website and so I highly recommend

46:20

becoming familiar with how to install a

46:22

plug-in because it takes like three

46:23

seconds and some of them are really cool

46:24

and then finally the last topic I'll

46:29

briefly mention before we finish today's

46:32

lecture is vim mode and other programs

46:34

so turns out that a lot of programmers

46:37

we're really excited about VIMS

46:38

interface and so they've implemented

46:40

similar functionality and other tools

46:42

for example like I've configured my

46:44

Python repple to run in vim mode so I

46:47

can like type in stuff here and if I

46:49

press escape now I'm in normal mode in

46:51

my Python repple and I can like move

46:53

back and forth and like press X here

46:55

delete a thing like CW change a word and

46:58

do all those good things and it's not

47:01

just the Python repple like I have my

47:03

terminal behaving this way too so like I

47:05

can type in whatever I want here and

47:07

like escape and I'm in normal mode I can

47:10

go here and like go into visual mode

47:11

inside my terminal and like select

47:13

blocks of text press tilde to change the

47:15

case whatever so we've linked sir like

47:19

how exactly you can enable vim mode for

47:22

like bash z shell fish a lot of read

47:25

line based programs like jupiter

47:27

notebook a whole bunch of other things

47:28

and if it's not another place you can

47:30

probably find it by googling it because

47:32

a lot of people like to have this sort

47:33

of functionality and if you're really

47:36

gonna like commit to learning them I

47:37

think it's valuable to enable this sort

47:39

of like vim emulation mode in every tool

47:41

you use is like one or like make you

47:43

learn the tool a lot better and to once

47:44

you become good at vim like those skills

47:46

will now transfer to all your other

47:47

tools you use okay so I think that's it

47:53

for our rapid introduction to them

47:54

there's some other neat material that we

47:57

weren't able to fit in today's lecture

47:58

but it's in the lecture notes and then

47:59

finally I highly recommend going through

48:01

the exercises for today like at least

48:02

for me personally I think spending time

48:05

learning my text editor has been like

48:07

the most beneficial thing out of like

48:08

the kinds of things we're teaching in

48:10

this class

48:11

so yeah that's it for today's lecture

48:14

and we'll see you tomorrow

48:15

note that we've changed tomorrow's

48:17

lecture to data wrangling Thursday and

48:20

Tuesday lectures are now switched this

48:22

is reflected on the course website in

48:23

case anybody was going to come to one

48:25

but not the other

Rate This

5.0 / 5 (0 votes)

العلامات ذات الصلة
Vim教程文本编辑编程效率Vim模式插件扩展代码导航编辑命令键盘快捷键Vim配置Vim社区
هل تحتاج إلى ملخص باللغة العربية؟