Lecture 3: Editors (vim) (2020)
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
📝 介绍与文本编辑器的重要性
本段落介绍了本次讲座的主题——文本编辑器,强调了作为程序员,我们花费大量时间编辑文本和程序,因此提高编辑效率能节省大量时间。讲师分享了自己对文本编辑器的热爱,并指出不同编程场景需要不同的编辑工具,例如 Vim、Emacs、VS Code 和 Sublime 等。此外,讲师还强调了学习文本编辑器的重要性,并介绍了今天讲座的主要内容,包括教程和练习,以及如何通过坚持使用和探索来掌握这些工具。
🌟 Vim编辑器及其多模式特性
这一部分详细讲解了Vim编辑器及其多模式特性。Vim是一个模式编辑器,具有多种操作模式,如普通模式、插入模式、替换模式等。每种模式都有其特定的用途和按键组合,例如在普通模式下,按键用于导航和编辑,而在插入模式下,按键则直接输入文本。讲师还提到了Vim的一些高级特性,如可视化模式和命令行模式,并解释了如何通过按键组合在不同模式间切换。
📖 Vim的基本操作和命令
在这一部分中,讲师介绍了Vim的一些基本操作和命令,包括如何打开文件、保存更改以及退出Vim。详细讲解了命令行模式下的命令输入方法,例如使用':w'保存文件,':q'退出Vim等。此外,还介绍了如何使用帮助系统来获取关于Vim命令的信息,以及如何通过键盘快捷键执行常用操作,而不必使用鼠标,从而提高编辑效率。
🔍 Vim的缓冲区、窗口和标签页模型
本段落深入探讨了Vim的缓冲区、窗口和标签页的概念及其工作方式。Vim维护一组打开的缓冲区,代表打开的文件,用户可以在多个标签页和窗口中查看这些缓冲区。每个标签页可以包含多个窗口,而每个窗口对应一个缓冲区。特别地,一个缓冲区可以在多个窗口中打开,这使得同时查看和编辑同一个文件的不同部分成为可能。此外,还讨论了关闭窗口和退出Vim的命令,以及如何使用':qa'命令一次性关闭所有窗口并退出Vim。
🛠 Vim的普通模式和编辑效率
这一部分重点介绍了Vim的普通模式,解释了为什么它是Vim最有趣的特性之一。普通模式允许用户通过编程方式与Vim接口进行交互,不同的按键组合有不同的效果,并且可以组合使用,就像编程语言一样。通过使用普通模式,用户可以快速移动光标、编辑文本和执行复杂的编辑任务。讲师还强调了通过普通模式进行编辑可以大大提高编辑速度,使得编辑文件的速度接近于思考的速度。
➡️ Vim的移动命令和编辑命令
在这一部分中,讲师详细介绍了Vim的移动命令和编辑命令。移动命令包括使用hjkl键进行光标移动、使用W和B键移动到单词的开头和结尾、使用0和$键移动到行的开头和结尾等。编辑命令则包括i命令进入插入模式、o和O命令打开新行、D命令删除文本、u命令撤销更改等。讲师还解释了如何使用可视模式来选择和操作文本块,以及如何使用修饰键如'a'和'i'来改变移动命令的行为。
🔄 Vim的复制粘贴、计数和修饰符
本段落讲解了Vim中的复制粘贴操作,包括y命令用于复制文本、p命令用于粘贴文本,以及如何结合移动命令来选择要复制的文本。计数功能允许用户通过数字前缀来重复命令多次。修饰符如'a'和'i'可以改变移动命令的行为,'a'代表around(环绕),'i'代表inside(内部)。讲师还提到了如何使用这些功能来提高编辑效率,并展示了如何通过组合不同的命令来执行复杂的编辑任务。
📚 Vim的可编程性和在其他程序中的应用
这一部分讨论了Vim的可编程性,包括如何通过修改vimrc文件来配置Vim,以及如何使用插件来扩展Vim的功能。讲师提到了Vim的配置文件vimrc,以及如何通过这个文件来开启语法高亮和行号等特性。此外,还介绍了Vim的插件系统,可以通过安装插件来增加如模糊文件查找、撤销历史可视化等功能。最后,讲师提到了Vim模式在其他程序中的应用,如Python REPL、终端和Jupyter Notebook等,可以通过启用Vim模式来提高这些工具的编辑效率。
Mindmap
Keywords
💡文本编辑器
💡Vim
💡模式编辑
💡键盘快捷键
💡效率
💡插入模式
💡命令模式
💡光标导航
💡文本操作
💡撤销和重做
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
Okay, cool. So, welcome to the third
lecture of the missing semester of your
si a second today we're going to be
talking about text editors this is a
topic that I really like I think it's
one of the most valuable topics that
we're teaching in this class because as
programmers you spend so much of your
time editing text editing programs that
if you invest time into making yourself
more efficient at doing this you'll save
a ton of time probably hundreds of hours
over the course of your undergrad or
over the course of your career so text
editors are a little bit different than
other programs you might use to edit say
things like English prose because
programming is different than writing
English prose when you're programming
you spend a lot of time reading what
you've written you spend a lot of time
navigating around a buffer and you spend
a lot of time making little edits to
code all over the place rather than just
writing in a long stream like you do
when you're writing an essay or
something and so it makes sense that
there different programs for these
different purposes right so yeah things
like Microsoft Word for writing essays
and things like vim and Emacs and vs
code and sublime for writing code so the
way you learn a text editor and become
really good at it is you start with a
tutorial and so that's basically going
to be the function of today's lecture
plus the exercises we've given you and
then after the tutorial you need to
stick with the editor for all your
editing tasks and when you're learning a
sophisticated tool so today we're going
to teach you vim which is one powerful
editor that a lot of programmers use
when you're learning such a
sophisticated tool it may be the case
that initially switching to this tool
slows you down a little bit when you're
programming but stick with it because
I'd say that in about 20 hours of
programming using a new editor you'll be
back to the same speed at which you
programmed using your old tool and then
after that the benefits will start and
you'll get faster and faster as you
learn more with these sophisticated
programs like them it takes not way too
long to learn the basics but a lifetime
to master and so throughout the time
you're using this tool make sure you
look things up as you go if you ever get
to a point where you're like oh this is
a really inefficient way of doing things
is there a better way the answer is
almost always yes because these text
editors were written by programmers for
programmers and so of course like the
peep
I wrote these tolls ran into the same
kinds of issues and fixed them so that
you don't need to deal with these
anymore and so yeah as you're learning
make sure you look things up as you go
either use Google or feel free to send
us emails if you have questions or come
to office hours and we'll help you
figure out how to do things really fast
so as far as which editor to learn in
previous iterations of this class we
actually avoided teaching a specific
editor because we didn't want to enforce
our opinions on you guys but we actually
think that it's really useful to teach
you how to use one particular tool and
use it well and so people have really
strong opinions about editor so you can
see the course notes for more links on
this topic looking at which editors have
been popular over the years Stack
Overflow I'm sure you've all heard of
that does a survey every year asking
developers various questions and one
thing to ask is which TextEdit Derk do
you use and it seems to be that
currently the most popular kind of
graphical editor is vs code and the most
popular editor that is based within a
command line interface is vim and so
we're going to be teaching you vim and
there a couple reasons for this one is
that all the instructors so me John and
Jose use vim as our primary editor and
we've been doing this for many years and
we've been very happy with it
and we think that there are a lot of
interesting ideas behind them so even if
you don't end up using this particular
tool in the long term I think it's
valuable to learn these ideas and also a
lot of tools have actually been really
excited about the ideas in vim and so
they support a vim emulation mode for
example vs code what's apparently the
most popular editor in use today
supports vim bindings and this vim
emulation mode as of now has like 1.4
million downloads and as you'll see over
the course of this lecture a lot of
different tools like including your
shell including things like the Python
repple and like Jupiter notebook and all
sorts of other things even your web
browser can support of emulation mode
and so yeah we're going to be teaching
you this really neat tool today and in
this lecture we can't really cover all
of them right it's a very powerful tool
but our goal is to teach you and the
core philosophy of them like the really
neat ideas behind it and then in
addition to that some of the basics like
how do you open a file close a file
navigate around a file make edits and
things like that and
you may not remember every single little
detail from this lecture because we're
gonna go pretty fast through some of the
material but it's all in the lecture
notes and then the exercises actually
give you links to some tutorials and
things so I highly recommend that you
actually go through all the exercises at
least the non advanced exercises any
questions so far great okay so one of
the really cool ideas behind them is
that vim is a modal editor what does
this mean modal comes from the word mode
and this means that vim has multiple
operating modes and this is kind of
developed from the idea that when you're
programming there often times where
you're doing different types of things
like sometimes you're reading code
sometimes you're making small edits to
code like you're finding a particular
point like changing a little thing
somewhere sometimes you're just writing
a lot of code in one go like suppose
you're just writing a function from
scratch and so there's different
operating modes for doing these
different types of things and so I'm
actually want to write this down on the
blackboard so I'll have a useful thing
to refer to later so when you start vim
up it starts up in what's called normal
mode and in this mode all the different
key combinations behave in one way and
then there are different key
combinations that switch you between
normal mode and other modes which change
the meaning of different keys so for the
most part you'll be spending most of
your time in vim in normal mode or what
is called insert mode and to go to
insert mode you press the key I for
normal mode and to go from insert mode
back to normal mode you press the Escape
key a little note on notation because
we'll need this later in the notation
I'm going to be using in this lecture
and what's also in the lecture notes and
what vim uses to give you feedback they
have a couple different ways of talking
about different keys so when they're
talking about bare keys like just the
eye key on your keyboard they'll just
say eye but for a different key
combinations like when you press control
and something like say control V it's
notated in one of like approximately
three ways one way that can be notated
is a caret and then the control
character so this is control V another
way this might be written I think we've
written it this way in
lecture notes as control-v this is
probably the one you're more used to
seeing and then in some parts of them
this is written as angle brackets C - V
close angle bracket so just a little bit
of notation that will be useful later
so yeah vim has a couple different modes
where normal mode is designed for
navigating around a file reading things
going from file to file things like that
and then insert mode is where you type
in text so most keys that you press here
will just go into your text buffer where
as keys that you press here are not
being put into the buffer and instead
are used for things like navigation or
making edits and actually the picture is
a little bit more complicated than this
there are a whole bunch of other modes
and I'm just gonna write them down here
because we'll have them here to refer to
later and so if it also has a replace
mode for rather than inserting text and
kind of pushing what's ahead of it
forward it will overwrite text and then
it has a bunch of different modes for
selection so it has a mode called visual
mode and then it has visual line and
visual block this one is enter via the R
key this was entered via the V key this
one is entered via shift B and this one
is entered via control V and then
there's the command line mode which is
entered via the colon key
okay so now that we have that on the
board to refer to later we can actually
try some of this out all right so one
thing we noticed looking at that picture
is that to go from normal mode to any of
the other modes we press some key but to
go from any of the other modes back to
normal mode where we spend a lot of our
time we use the Escape key on our
keyboard so for this reason since you
under pressing the Escape key a lot when
using vim a lot of programmers rebind
one of the keys on their keyboard to be
escape because it's really inconvenient
to reach up with your left pinkie to
press that tiny little Escape key in the
corner of your keyboard and so a lot of
people use the caps lock key instead so
it's right there in the home row and we
have some links in the lecture notes for
how you can do this key rebinding okay
so now that we've talked about kind of
one of the core ideas of vim the idea of
modal editing we can talk about some of
the basics like how do you open up this
text editor how do you open file save
files and things like that and so this
is a command line based program although
there are some graphical variants and
the way you start this program is by
running vim one thing you might notice
is that in the bottom left corner of my
screen they actually saw what I just
typed this will be useful later in this
lecture where I'm actually typing in
commands for vim and I'll be saying what
I'm typing but you'll also see it on the
screen so if I press ctrl C see it says
ctrl C over there is that text big
enough for everybody to read great okay
so the way we open vim is just by
running the program vim on our command
line and this comes pre-installed on
most systems and if you don't have it
you can install it using your package
manager vim can also take an argument if
we want to use it to edit a particular
file instead of just opening up the
program then later opening a file so for
example I have a file in this directory
this is actually the lecture notes for
this lecture so I can do vim editors MD
and press ENTER and then boom them is
started and then in this lecture I'm not
running vim in the completely Exton I've
configured a couple of things that
behave a little bit nicer by defaults
little things like having line numbers
on the left or having some more status
information on the bottom and if you
want to start with this default
configuration we have a link to this in
the lecture notes so you can get a
slightly more sane config by default. So,
once you've opened Vim, what do you do?
Well, as I said earlier, Vim starts in
normal mode, so if I just start typing
letters like, say, type X it didn't insert
X into the buffer. You can see the cursor
up in the top left: it actually deleted
one of the characters. That's because I'm
in normal mode, not insert mode. So insert
mode is basically what you're used to with
all the other text editors you've used
in the past, where there's a cursor
somewhere, you press the character, and it
just goes into your buffer. I'm instead
in vim you start a normal mode and you
can press I to go into insert mode so
see I've pressed I and then in the
bottom left notice that it says - -
insert the bottom left always tells you
what mode you're in unless this normal
mode in which case it's blank and now
that I'm an insert mode if I press the X
character for example see it just gets
inserted into my text buffer and I can
backspace over at type other stuff and
now my text editor kind of behaves like
you'd expect any other program to behave
so from this point how to go back to
normal mode if I want to stop inserting
characters. Yes, exactly. I press escape.
And, that's the symbol my keystroke
visualizer uses for escape, so
just be aware of that. And then, Vim has
this idea, that using the mouse is
inefficient, like your hands are on the
keyboard, moving your hand over to the
mouse takes a lot of time, right, like you
don't want to waste those couple of
milliseconds while you're programming,
like in the middle of things. So, instead,
all Vim functionality can be accessed
just through the keyboard. And, it's all
sorts of things you might be used to
doing like opening files by going, like,
file open, or file save, or things like
that, or instead access through the
keyboard. And, how is that done? That's
done through one of the other Vim modes
that are on the board over there. In
particular, through command line mode. So,
if you're in normal mode, and you press
the ":" key, you'll notice that the
cursor - I guess my visualizer is covering
the thing right now but it's gone - the
cursor jumps to the bottom - the bottom
left - and it shows that ":" I just typed.
Now, I can type in a command. So, you can
think of this almost like the command
shell that we've talked been talking
about over the last few days, except this
is Vim's
command shell, so you give Vim commands
here, instead of shell commands. And
there are a bunch of built-in commands
that do all the things that you're used
to. Like, for example, one command that you
might want to know is how to quit this
editor. You might notice that if you're in
normal mode, I can press "Escape" to go
back from command line mode to normal
mode, and I press "control C", unlike what
happens to a lot of programs, this doesn't
quit Vim. So, how do I quit Vim? I can
press ":", and then go into command
line mode, and then I can type in the
command "quit". "Q-U-I-T". You'll see that I
- maybe I should move this over to the
middle or something - see, it says ":quit"
and I press ENTER, and it quits Vim. I can
open Vim up again. There's actually a
short form for this command, just ":q", and
that'll do the same thing. And, there are a
bunch of other useful commands like this.
So, some other handy ones to know are
how do you save a file? So, suppose I make
some edits here, like "hello world".
So, I pressed "i" to go into insert mode - or
let me redo that - I press "i" to go into
insert mode. Right now, I can use the down
arrow to... I think I've slightly I
should fix that. Can you fix the config,
actually, John? Never mind that.
Okay, so, suppose I go down to this line,
and I press "i" to go into insert mode, and
type in some text, and then press "escape"
to go back to normal mode. Now, how do I
actually save this file? Well, there's
another command for that. So, ":" to go
into command mode, and then I can type "W"...
...and press "Enter". "W" stands for write. And,
it says in the bottom "editors.md"
whatever blah blah written. And, so, this
means it saved the file and so now if I
":q" to quit and open the same file,
again, you'll see that the changes have
been persisted. There are a couple other
there's... So, there's a ton of different
Vim commands that are useful for
different reasons. But, I'll just explain
a couple more to you now. One command
that's really useful is help. ":help"
And you can do ":help", and then type in a
particular key, or a particular command,