Editors (Vim)¶
约 1214 个字 110 行代码 1 张图片 预计阅读时间 5 分钟
Modal editing¶
Vim has multiple operating modes.
- Normal: for moving around a file and making edits
- Insert: for inserting text
- Replace: for replacing text
- Visual (plain, line, or block): for selecting blocks of text
- Command-line: for running a command
Basics¶
Inserting text¶
From Normal mode, press i
to enter Insert mode.
Now, Vim behaves like any other text editor, until you press<ESC>
to return to Normal mode.
Buffers, tabs, and windows¶
Vim maintains a set of open files, called “buffers”.
A Vim session has a number of tabs, each of which has a number of windows (split panes).
Each window shows a single buffer. Unlike other programs you are familiar with, like web browsers, there is not a 1-to-1 correspondence between buffers and windows; windows are merely views.
A given buffer may be open in multiple windows, even within the same tab.
This can be quite handy, for example, to view two different parts of a file at the same time.
By default, Vim opens with a single tab, which contains a single window.
Command-line¶
Command mode can be entered by typing :
in Normal mode.
Your cursor will jump to the command line at the bottom of the screen upon pressing :
.
This mode has many functionalities, including opening, saving, and closing files, and quitting Vim.
:q
quit (close window):w
save (“write”):wq
save and quit:e {name of file}
open file for editing:ls
show open buffers:help {topic}
open help:help :w
opens help for the:w
command:help w
opens help for thew
movement
Vim’s interface is a programming language¶
Movement¶
Movements in Vim are also called “nouns”, because they refer to chunks of text.
- Basic movement:
hjkl
(left, down, up, right) - Words:
w
(next word),b
(beginning of word),e
(end of word) - Lines:
0
(beginning of line),^
(first non-blank character),$
(end of line) - Screen:
H
(top of screen),M
(middle of screen),L
(bottom of screen) - Scroll:
Ctrl-u
(up),Ctrl-d
(down) - File:
gg
(beginning of file),G
(end of file) - Line numbers: :
{number}<CR>
or{number}G
(line {number}) - Misc:
%
(corresponding item) - Find:
f{character}
,t{character}
,F{character}
,T{character}
- find/to forward/backward {character} on the current line
,
/;
for navigating matches
- Search:
/{regex}
,n
/N
for navigating matches
Selection¶
Visual modes:
- Visual: v
- Visual Line: V
- Visual Block: Ctrl-v
Can use movement keys to make selection.
Edits¶
Vim’s editing commands are also called “verbs”, because verbs act on nouns.
i
enter Insert mode- but for manipulating/deleting text, want to use something more than backspace
o
/O
insert line below / aboved{motion}
delete{motion}
- e.g.
dw
is delete word,d$
is delete to end of line,d0
is delete to beginning of line
- e.g.
c{motion}
change{motion}
- e.g.
cw
is change word - like
d{motion}
followed byi
- e.g.
x
delete character (equal dodl
)s
substitute character (equal tocl
)- Visual mode + manipulation
- select text,
d
to delete it orc
to change it
- select text,
u
to undo,<C-r>
to redoy
to copy / “yank” (some other commands liked
also copy)p
to paste- Lots more to learn: e.g.
~
flips the case of a character
Counts¶
You can combine nouns and verbs with a count, which will perform a given action a number of times.
3w
move 3 words forward5j
move 5 lines down7dw
delete 7 words
Modifiers¶
You can use modifiers to change the meaning of a noun.
Some modifiers are i
, which means “inner” or “inside”, and a
, which means “around”.
ci(
change the contents inside the current pair of parenthesesci[
change the contents inside the current pair of square bracketsda'
delete a single-quoted string, including the surrounding single quotes
Customizing Vim¶
Vim is customized through a plain-text configuration file in ~/.vimrc
(containing Vimscript commands).
Here is the basic configuration file that provided by this course.
For macos, There might not have the default .vimrc
file in ~
.
But never mind, just type vim ~/.vimrc
and paste the code into it, then type :wq
.
You will find that everything is done!
Extending Vim¶
copy lecture notes...need review
There are tons of plugins for extending Vim. Contrary to outdated advice that you might find on the internet, you do not need to use a plugin manager for Vim (since Vim 8.0). Instead, you can use the built-in package management system. Simply create the directory ~/.vim/pack/vendor/start/
, and put plugins in there (e.g. via git clone
).
Here are some of our favorite plugins:
- ctrlp.vim: fuzzy file finder
- ack.vim: code search
- nerdtree: file explorer
- vim-easymotion: magic motions
We’re trying to avoid giving an overwhelmingly long list of plugins here. You can check out the instructors’ dotfiles (Anish, Jon, Jose) to see what other plugins we use. Check out Vim Awesome for more awesome Vim plugins. There are also tons of blog posts on this topic: just search for “best Vim plugins”.
Resources¶
- vimtutor is a tutorial that comes installed with Vim - if Vim is installed, you should be able to run
vimtutor
from your shell - Vim Adventures is a game to learn Vim
- Vim Tips Wiki
- Vim Advent Calendar has various Vim tips
- Vim Golf is code golf, but where the programming language is Vim’s UI
- Vi/Vim Stack Exchange
- Vim Screencasts
- Practical Vim (book)
Exercises¶
Warning
I will write my own solution to the problem, If you want to solve those by yourself, please stop.
Problem1
Complete vimtutor
. Note: it looks best in a 80x24
(80 columns by 24 lines) terminal window.
Solution
Bash | |
---|---|
Problem2
-
Install and configure a plugin: ctrlp.vim.
- Create the plugins directory with
mkdir -p ~/.vim/pack/vendor/start
- Download the plugin:
cd ~/.vim/pack/vendor/start; git clone https://github.com/ctrlpvim/ctrlp.vim
- Read the documentation for the plugin. Try using CtrlP to locate a file by navigating to a project directory, opening Vim, and using the Vim command-line to start
:CtrlP
. - Customize CtrlP by adding configuration to your
~/.vimrc
to open CtrlP by pressing Ctrl-P.
- Create the plugins directory with
Solution
...TO DO
Problem3
To practice using Vim, re-do the Demo from lecture on your own machine.
Here is a broken fizz buzz implementation:
Python | |
---|---|
fix the following issues:
- Main is never called
- Starts at 0 instead of 1
- Prints “fizz” and “buzz” on separate lines for multiples of 15
- Prints “fizz” for multiples of 5
- Uses a hard-coded argument of 10 instead of taking a command-line argument
Solution
G
move to the bottom of the fileo
create a new line- insert code:
/limit
search forlimit
, then pressn
to move to the next matchi
enterinsert
mode, and changelimit
to1, limit + 1
/fizz
search forfizz
, then pressn
to move to the next match.ci'
change the inner text of'
, insertbuzz
- move the cursor to the
print('fizz')
line, press$
, theni
enter insert mode and insert, end=''
jj.
move the cursor to line 6 and make the same change as previous didgg
move the cursor to the top of the file,O
create the new line above,and insertimport sys
/10
find10
, thenci(
change the text in(
, typeint(sys.argv[1])
The final result:
Python | |
---|---|
Problem4
(Advanced) Convert XML to JSON (example file) using Vim macros. Try to do this on your own, but you can look at the macros section above if you get stuck.
Solution
...TO DO