Linux开发利器之Vim编辑器

2021-09-13 14:49:10

1.主题更换与Python代码补全

  • 下载Vundle
1
2
3
4
5
6
mkdir -p ~/.vim/bundle/
git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim
# 下载主题
git clone https://github.com/tomasr/molokai.git
unzip molokai-master.zip
mv molokai-master/colors/ ~/.vim/
  • 配置插件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
set nocompatible              " be iMproved, required
filetype off " required

" set the runtime path to include Vundle and initialize
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin('/home/zer0py2c/vim_plugins')
" alternatively, pass a path where Vundle should install plugins
"call vundle#begin('~/some/path/here')

" let Vundle manage Vundle, required
Plugin 'VundleVim/Vundle.vim'

Plugin 'file:///home/zer0py2c/vim_plugins/jedi-vim'

" All of your Plugins must be added before the following line
call vundle#end() " required
filetype plugin indent on " required
" To ignore plugin indent changes, instead use:
"filetype plugin on
"
" Brief help
" :PluginList - lists configured plugins
" :PluginInstall - installs plugins; append `!` to update or just :PluginUpdate
" :PluginSearch foo - searches for foo; append `!` to refresh local cache
" :PluginClean - confirms removal of unused plugins; append `!` to auto-approve removal
"
" see :h vundle for more details or wiki for FAQ
" Put your non-Plugin stuff after this line

set number
set cursorline
set hlsearch
" Enable molokai theme
"
let g:rehash256 = 1
let g:molokai_original = 1
highlight NonText guibg=#060606
highlight Folded guibg=#0A0A0A guifg=#9090D0
set t_Co=256
set background=dark
colorscheme molokai
set tabstop=4
" Enable jedi-vim plugin
"
let g:jedi#completions_command = "<C-N>"
let g:jedi#show_call_signatures=1
let g:jedi#environment_path = "/usr/bin/python3.8"
  • 下载Python依赖
1
python3.8 -m pip install jedi -i https://pypi.tuna.tsinghua.edu.cn/simple

2.比较重要的命令

  • 基础文本替换
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
"""
:[range]s/from/to/[flags]
range: 搜索范围,默认是当前行
flags:
c - confirm 替换前询问
g - globle 默认不询问,整行替换。不加g,默认替换第一个匹配到的文本
i - ignore 忽略大小写
cgi 表示不区分大小写,整行替换,替换前询问
"""
:%s/foo/bar/g #将所有行中出现的所有foo替换成bar
:%s/foo/bar/ #将所有行中出现的第一个foo替换成bar
:s/foo/bar/g #将当前行中出现的所有foo替换成bar
:s/foo/bar/ #将当前行中出现的第一个foo替换成bar
:5,12s/foo/bar/g #将第5-12行中出现的foo替换成bar
:%s#foo/#bar/#g #将所有行中出现的所有foo/替换成bar/
:%s+/foo1/foo2/+/bar1/bar2/+g #将所有行中出现的所有/foo1/foo2/替换成/bar1/bar2/
  • 正则文本替换
1
2
3
4
5
6
7
8
9
10
11
12
:%s/\s*$//g #删除行尾空格
:%s/^\s*//g #删除行首空格
:%s/^\n$//gc #替换多个空行为一个空行(带询问)
:%s/\<child\>/children/g #保证child是个完整单词的情况下替换
"""
:[range]g/{pattern}/[cmd] 在range范围内搜索pattern,匹配则执行cmd
"""
:g/^\s*$/d #删除所有空行
:g!/^\s*$/d #删除所有非空行(删除正文)
:g/mg[ir]box/s/box/square/g #将mgibox mgrbox中的box换成square
:8,15g/^\(.*\)$\n\1$/d #删除第8-15行的重复行
:1,7g/^/ mo 0 #翻转第1-7行的内容
  • 大小写转换
1
2
3
4
5
6
7
8
9
10
~ #将光标下的字母转换大小写
3~ #将光标下的字母及后续2个字母转换大小写
g~~ #将当前行的所有字母转换大小写
3g~~ #将当前行的所有字母及后续2行的所有字母转换大小写
U #将可视模式下选中的字符转成大写
u #将可视模式下选中的字符转成小写
gUU #将当前行的所有字母改成大写
guu #将当前行的所有字母改成小写
ggguG #将全文的所有字母转成小写
ggg~G #将全文的所有字母转换大小写
  • 多文件切换
1
2
3
4
5
6
7
8
9
10
vim -o filename1 filename2 #横向显示
vim -O filename1 filename2 #纵向显示
:split file_path #在vim中,横向打开一个文件
:vsplit file_path #在vim中,纵向打开一个文件
:q #关闭当前窗口
:close #关闭当前窗口
:only #关闭当前窗口外的其他窗口
:qa #关闭所有窗口
ctrl+ww #切换到下一个窗口
ctrl+w+方向键 #切换到指定方向的下一个窗口
  • 可视模式
1
2
3
4
5
6
7
8
9
"""
v 可视字符
V 可视行
ctrl+v 可视块
"""
c #修改
d #删除
> #右移
< #左移