看这种书挺枯燥,还是learn by doing 比较好,使用一段时间就很明确自己需要什么功能了。
By default
oc/kubectl edit
will usevi
in Linux if environment variableOC _EDITOR
is empty, if you have.vimrc
that is not compatible withvi
, will get error message, to usevim
setexport OC_EDITOR=vim
orexport KUBE_EDITOR=vim
.
Other resources
How I booted my Vim Informative!! I grab lots of configurations from it, go and poke around the author’s vimrc file.
Vim Tutorial Videos There are a lot, see O`Reilly.
Actually you can learn Vim
by running command vimtutor
in your terminal, it is a simple and quick tutorial. For a quick review, just see summary after each lesson.
Daily Foundation
掌握好这些操作基本上就满足日常需求了。
Configuration
.vimrc
is just like .bashrc
. you create a .vimrc
(even it’s empty) to tell vim use vim not vi-compatiable mode!
关于vimrc的配置,参考my github repo: https://github.com/chengdol/vim-configuration
- Basic configuration
- Custom key mapping
- Vimscript plugin
About mapping:
- nmap: normal mode
- vmap: visual mode
- imap: insert mode
- map: normal, visual and operating-pending mode
- map!: command and insert mode
To list mapping: :map
or more specific :vmap
Which mode is currently use should be clear: Normal
mode, Insert
mode and Visual
mode, Command-Line
mode, Replace
mode, etc. Visual
mode lets us select text in the buffer
and then operate on the selection.
If you suspect that your customizations are causing interference, here’s a quick test. Try quitting Vim and then launching it with these options:
1 | vim -u NONE -N |
The -u NONE
flag tells Vim not to source your vimrc
on startup. That way, your customizations won’t be applied and plugins will be disabled. In older versions of Vim, not loading a vimrc
file activates vi
compatible mode, which causes many useful features to be disabled. The -N
flag prevents this by setting the nocompatible
option. Since version 8.0 of Vim, the nocompatible
option is set by default, making the -N
flag unnecessary.
vim -N file
: -N
not using vi compatible mode, use newer vim mode. or can configuring in .vimrc
file.
:set list
, :set nolist
, :set list?
, :set list&
: set, unset, check value, set as default. 以此类推.
1 | set autochdir "auto set current dir as working dir |
Reload .vimrc
Reload .vimrc without restarting vim: :source ~/.vimrc
. 已经加入vimrc了。
Plugin
Vim 8.0 has its own built-in package manager. For Vim version less than 8.0, I use vim-plug
as the plugin manager:
https://github.com/junegunn/vim-plug
You can even specify git address of the plugin, for example:
1 | Plug 'https://github.com/tfnico/vim-gradle.git' |
If the plugin does not get auto installed, run :PlugInstall
, check the plug status by :PlugStatus
. 这些在vim-plug
README中都有说明。
Please see my vim-configuration git repo, download the vimrc file.
- nerdtree: file system explorer for vim editor
- fuzzyfinder: fzf
- buffer explorer: 比默认的buffer要好看
- taglist: source code browser
NERDTree,在目录界面中通过m
启动常规文件/文件夹操作。C
回车 进入子文件夹。
Display Management
Vim中切换编辑多个文件:
:cd /tmp
change vim working directory
:pwd
show current working directory
:set hidden
(put it in .vimrc
): https://medium.com/usevim/vim-101-set-hidden-f78800142855
:e .
browse current directory, select file to edit
:ls
list buffer (open files)
- %: buffer in current window
- +: unsave changes
- =: read-only
:bn
go to next buffer
:bp
go to previous buffer
:bd
delete buffer, such as :bd5
:bf
go to first buffer
:bl
go to last buffer
:b3
go to 3rd buffer
gf
jump to the file under the cursor, use ctrl+o
jump back
可以在vim :e <path>
中直接创建,删除文件或者文件夹,显示long format, sort等
在使用
vim-plug
加载插件后,这部分功能失效了, 不过可以使用command mode去做查看。
- i: long, thin, tree format
- s: sort by name, time, file size
- r: reverse sort order
- gh: hide or unhide dotfiles
- d: make a directory
- D: delete file or directory
- R: rename file or dir
- -: go up a directory
windows & splits
All start with ctrl+w
:
-
s: split horizontally
-
h: move focus left
-
l: move focus right
-
v: split vertically
-
j: move focus down
-
k: move focus up
-
w: cycle focus
-
p: focus previous win
-
c: close win current focus
-
o: close all win except current focus
:h window-resize
, check for window resize.
- +: enlarge windows,
5+
- -: reduce windows,
3-
Tab
各个文件单独的tab,不用划分window了, 这个部分是放在vimrc中的:
1 | " Tab mappings |
What is Leader key
in Vim?
https://stackoverflow.com/questions/1764263/what-is-the-leader-in-a-vimrc-file
The Leader key
is a way of extending the power of VIM’s shortcuts by using sequences of keys to perform a command.
Operation
Using hjkl
(在后续许多命令中都有涉及) or arrow
keys to move around, can have number ahead to indicate how many line to move.
:q!
quit without saving.
:wq
quit with saving, always retouch the timestamp.
sometimes using :q!
and :wq!
: https://unix.stackexchange.com/questions/88247/use-of-in-vim
:x
the same as :wq
, but write only when changes have been made.
:w filename
used to save file to filename
.
:w !sudo tee %
write to sudo with current file name, %
represent current file name. (用在保存更改read-only文件的内容)
Quick Movement
A
append at end of the line, a
appends after cursor.
I
insert ata head of the line, i
insert at cursor.
W
jump contiguous words, w
jump one word.
o
will open a line below cursor, O
open above.
3w
move 3 words forward, 0
move to the start of the line.
^
go to first non-empty char in line
3fn
find 3rd n
char at this line. repeat by ;
Screen line movement
screen line指被terminal因宽度限制产生的行,并不是指原始的很长的那一行。
g0
, gm
, g$
: start, middle, end of a line.
gk
, gj
: move up/down in screan line (can use arrow instead k/j ).
Scrolling
Press G
to move you to the bottom of the file.
Type gg
to move you to the start of the file.
line number + G
or line number + gg
will go to the line specified.
To go back to where you came from press Ctrl-o
(Keep Ctrl
down while pressing the letter o
). To go forward Ctrl-i
H
, M
and L
move the cursor to top, medium and bottom of current page
zt
, zz
, zb
: move cursor line to top, middle, bottom of screen
Make Mark
For example, jump back and forth spots:
https://www.linux.com/news/vim-tips-moving-around-using-marks-and-jumps/
:marks
Shift
Shift configuration is in .vimrc
:
1 | " shift with tab |
:retab
: replace all tabs with spaces in current buffer.
>>
, <<
shift current line, 4>>
, 4<<
block shift with 4 lines together.
In insert mode, can use tab itself to shift (now override by auto-complete in my vimrc file), or using ctrl+t
, ctrl+d
: shift right and left.
对于code block indention,我在.vimrc 中设置了vmap, 用visual mode选中后shift就很方便了。
Search and Replace
/
search forward, ?
search backword. when hit the search, n
go forward, N
go backword. If the cursor is at search word, use *
to search forward, #
search back, use g*
, g#
do partial match search.
/case\c
search case-insensitive, /CaSe\C
search case-sensitive. \c
and \C
can be anywhere in pattern.
:s/old/new/g
to substitute old
by new
in a line for all occrurences. you must place the cursor in that line.
To change every occurrence of a character string between two lines,
:#,#s/old/new/g
where #,#
are the line numbers of the range of lines where the substitution is to be done.
:%s/old/new/g
to change every occurrence in the whole file.
:%s/old/new/gc
to find every occurrence in the whole file, with a prompt whether to substitute or not, c
is confirmation.
For case-sensitive: :%s/old/new/Igc
, but actually can be :%s/old\C/new/gc
.
Copy and Paste
这里和vim 的 register有关. Default is unnamed register, delete, change, substitute, search and yank all use registers.
Pasting text into a terminal running Vim with automatic indentation enabled can destroy the indentation of the pasted text: https://vim.fandom.com/wiki/Toggle_auto-indenting_for_code_paste, use
vim-bracketed-paste
can fix this.
Show register contents: :registers
y
is copy operator, for example, yw
copy one word, y$
copy to end of the line and yy
used to copy a line, then you can paste through p
. you can use v
to select and copy. By default it uses unnamed register. Register 0
always stores the yank content, so you can use "0p
to paste.
"ayy
, "ap
use register a
to yank and paste. Usually we use a~z
and A~Z
register, the uppercase register can append text to current content.
使用上不同文件之间copy/paste没啥问题,但如果需要copy到system clipboard,需要设置: https://stackoverflow.com/questions/3961859/how-to-copy-to-clipboard-in-vim vim has to be compiled with clipboard support for any of the suggestions mentioned here to work. Mine wasn’t configured that way on Mac OS X by default and I had to rebuild vim. Use this the command to find out whether you have it or not:
1 | vim --version | grep 'clipboard' |
+clipboard
means you’re good and the suggestions here will work for you, while -clipboard
means you have to recompile and rebuild vim.
d
is the delete operator, use motion to specify the action, for example dw
, de
, d$
. Without d
, vim just move the cursor: w
, e
, $
2dw
delete 2 words ahead
4dd
delete 4 lines in a row
Type p
to put previously deleted
text after the cursor. for example, you delete a line and replace it in another place
v
and then you move the cursor to select the text you want, if you want to delete them next, type d
, if you want to save the selected text to another file, press :
, then type w filename
. V
is moved linewise.
Folding
这个非常有用,比如编辑yaml 文件。
Fold by syntax or indent (yaml). :sed foldmethod=indent/syntax
zM
, zR
: fold and unfold all
zi
: toggle fold all
zc
, zo
: close, open fold block
za
: toggle fold block
zk
, zj
: move fold focus up and down
还可以根据file type去设定, for example:
1 | " Automatic fold settings for specific files. Uncomment to use. |
Recording
暂时没用到,对重复的复杂操作有用。
Block comment and uncomment
quick way to comment and uncomment block of codes comment:
- press
Esc
- hit
ctrl+v
, enter visual block mode - use arrow keys select the lines, it won’t highlight everthing, it’s ok
shift+i
- insert the text you want, e.g: #, //, %
- press
Esc
twice
uncomment:
- press
Esc
- hit
ctrl+v
, enter visual block mode - use arrow keys select text you want to delete
- press
x
to delete the text
Others
Press u
to undo the last commands, U
to undo all the changes in a line.
crtl+r
to undo undo.
Type rx
to replace the one character at the cursor with x
. R
is used to replace more then one chars, type R
then get into insert mode.
ce
deletes the word and places you in Insert mode, so you can type new word, it will replace old ones. c
is change operator, just like d
.
比如要更改3个连续的words 在某行中: c3w
, then type.
Ctrl+g
will show cursor location in the file and the file status, also the file name.
%
is used tp match another part of (, [ or {
. This is very useful in debugging a program with unmatched parentheses. 也可以用来match其他block语句,比如if-end。
:!<commands>
is used to execute external commands, for example :!ls -ltr /tmp
. (the same as IPython)
:r filename
will retrive the file placed under the cursor. you can also read the output of an external command, :r !ls -ltr
.