Vim编辑器开发IDE配置介绍
本文以Linux系统为例讲解来介绍如何配置强大的Python开发IDE

主要内容:
- VIM编辑器的安装,配置介绍
- VIM插件安装管理方法.
- VIM操作命令等快捷介绍
- VIM+Ctag展示函数列表.
Linux系统Vim编辑器安装
方法1: 使用apt-get命令软件源安装,打开终端Terminal,执行安装命令:
$ sudo apt-get update
$ sudo apt-get install vim ctag
方法2: 源码安装.
mkdir ~/github
git clone https://github.com/vim/vim.git ~/github/vim
cd ~/github/vim
make distclean ## 如果以前编译过需要执行此命令
## 编译前的自动配置校验,会自动生成正确的Makefile , 如果需要支持python3则增加 --enable-python3interp=yes 选项
./configure --enable-python3interp=yes --prefix=$HOME/apps
make -j$(nproc)
make install
echo 'export PATH=${HOME}/apps/bin:${PATH}' >> ~/.zshrc
echo 'export MANPATH=${HOME}/apps/share/man:${MANPATH}' >> ~/.zshrc
export PATH=${HOME}/apps/bin:${PATH}
export MANPATH=${HOME}/apps/share/man:${MANPATH}
which vim <-- 将输出vim路径为'/home/gituser/apps/bin/vim'
Vim使用基础
- vim 模式 : 命令模式(Normal), 编辑模式(Insert/Replace), 可视化模式(Virsual)
- 创建和打开文件: vim [+num] [+/{pattern}] [+{command}] file ..
- 保存和关闭文件: :w ,:w! , :w newfilename , :q, :q!, :wq ,ZZ(保存并退出)
- 控制模式操作:复制
y/剪切c/粘帖p, 查找字符串/search_string, - 命令模式操作: 查看帮助信息
:help, 替换字符串操作:%s/search_string/replace_string/g,
ctrl+z,jobs 和 fg 命令与 vim 结合使用,减少了重复open,close操作.
vim模式
命令模式
使用vim命令打开或创建文件的初始模式,可以使用h,j,k,l进行光标移动,可以执行 复制y/剪切c/粘帖p/删除d/替换s 等等很多操作命令.
[] : 表示可选项
{} : 必选信息,作为命令的一部分,可以是一系列不同的值.
{char1-char2}: 表示范围中的一个字符取值,例如{a-z}表示小写字母, {a-zA-Z0-9}表示任意数字字母.
{motion}: 表示一条光标移动命令,例如: w:移动到下一个单词的开头, b:移动到当前光标单词开头, 4j:向下移动四行,/The<CR>:移动光标到下次出现The的位置.
命令格式: [count]["x]<operator>{motion}
- 先介绍下
"x表示的是存储寄存器的序号,其中双引号需要要注意 - {motion} : 是一个移动光标的命令. 例如: w:一个单词, b:单词开头位置, 4j : 向下4行, /HelloWorld
: 下一次出现"HelloWorld"的位置 operator: 表示具体的操作命令,例如[c|C|y|Y|d|D|p|P].
复制操作y(yank): 使用规则与剪切命令一致
删除操作x/X/d/D(delete): 格式:["x]x, ["x]X, [count]["x]d{motion},["x]D , 实例: "1d3w , 5D, 5x ,x5(错误!)
删除并进入编辑模式操作**R/gR/c/C/s/S**(Delete and insert): 格式:["x]c{motion} , 实例:"c5 " 或 "5c ", "cw, "cc", "5cc" 或者 "c4j", "5C" , "C5"(错误!)
粘贴操作p(put or paste): p , 10p, P: 在当前光标前粘帖, 10P: 在当前光标前插入10次寄存器中的内容.
查找操作/or?: /pattern:表示从前向后找, ?pattern:从后往前找, 例如 /function_name
切换小写gu: guw: 当前光标后的单词转换为小写格式.
切换大写gu: gUw: 当前光标后的单词转换为大写格式.
> : 向右移动行
< : 向左移动行
zf: 定义一个折叠标记
编辑模式
编辑模式下我们可以通过键盘输入文本内容进行编写任务.
可视化模式
vim编辑器可以在命令模式下通过输入
v或者V进行可视化模式操作。
宏操作
The CTRL-A command is very useful in a macro. Example: Use the following steps to make a numbered list.
1. Create the first list entry, make sure it starts with a number.
2. qa - start recording into register 'a'
3. Y - yank the entry
4. p - put a copy of the entry below the first one
5. CTRL-A - increment the number
6. q - stop recording
7. <count>@a - repeat the yank, put and increment <count> times
Vim配置方法
插件管理选择
vundle 插件安装
安装vundle插件需要提前创建
$HOME/.vim/bundle目录
mkdir -p $HOME/.vim/bundle
git clone https://github.com/VundleVim/Vundle.vim.git $HOME/.vim/bundle/Vundle.vim
cat <<END >> ~/.vimrc
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()
" alternatively, pass a path where Vundle should install plugins
"call vundle#begin('~/some/path/here')
" let Vundle manage Vundle, required
Plugin 'VundleVim/Vundle.vim'
" All of your Plugins must be added before the following line
call vundle#end() " required
filetype plugin indent on " required
END
安装插件方法: 先将插件名称添加到.vimrc文件中保存,再执行:PluginInstall命令即可自动安装新增插件名.
如果想要作Python开发环境可以安装使用插件 “Plugin 'Python-mode-klen'” ,然后在.vimrc文件中添加如下内容:
"""""" Python-mode """""""""""""""""""""""""""""
" for more :help python-mode
let g:pymode_run = 1
let g:pymode_python = 'python3'
let g:pymode_run_bind = '<F5>'
let g:pymode_motion = 1 " <--- Enable Python-mode motions-keys ,like [[,]],[M,]M,aC,iC,aM,iM
" python with virtualenv support
let g:pymode_virtualenv = 1
let g:pymode_virtualenv_path = $VIRTUAL_ENV
let g:pymode_rope_completion = 1
let g:pymode_rope_complete_on_dot = 1
let g:pymode_rope_completion_bind = '<C-Space>'
更多插件可以参考我分享的github上的配置文件,也可以解决网络搜索适合自己的插件。
Vim常用快捷键
- 变更一块文本内容的方法:
命令模式下:>>和<<,>3j,<3j,.,5>>..和:5,7>;
插入模式下:Ctrl+T向右缩进移动,CTRL+D向左缩进移动 ;
可视化模式:Vjj>,gv,gn重新执行上次的模式查询操作并开始以可视化模式向后选中查询结果,gN类似gn但方向相反. - 相对行号设置
:set relativenumber or :set rnu. 格式化C代码方法:gg=G; - 文本对齐方式:
Vjj选中3行文本,然后输入:le 8, 或者:5,7le 8,左对齐到第8字节.:ri 80右对齐,宽度80字节,:ce 80居中对齐,宽度80字节 - 斜线和反斜线的快捷转换处理(
\/和\\) - 查找所有匹配的pattern内容行并列举显示:
:g/pattern/. - 替换方法:
:s/src/dest/g: 将当前行的src替换成dest;:s/src/dest/g 5或.,.+4s/src/dest/g: 将当前行和向下4行的src替换成dest;.,.+4s/src/dest/gc: g后面增加个c,表示confirm确认是否需要替换每一个匹配项。 - c+i+) : Change Inside of parens(修改括号内的文本,不包括括号), 查看更多方法
:help i), 那么, c+i+} 表示什么意思呢? - c+a+) : Change a parens(修改一对括号的文本,包括括号), c+a+} : 表示什么意思?
9.大小写切换:切换小写gu:guw: 当前光标后的单词转换为小写格式.切换大写gu:gUw: 当前光标后的单词转换为大写格式.
Vim + ctag
- 为代码生成tags标记文件:ctags *.c *.h
- Vim 实现函数跳转 : CTRL + ] 跳转到定义位置,同效果:
:tag和g]; CTRL + T 返回到历史tags栈的位置,默认为1, 同效果:pop - 查看历史tags栈列表:
:tags - 查看支持语言:
ctags –list-languages - 查看代码文件映射的后缀列表:
ctags –list-maps - 强制将某个文件识别为C语言:
ctags --language-force=c *.pc - 按照函数名直接跳转:
:tselect和:tjump
Vim + tmux/screen 窗口多分器
窗口多分器可以实现不使用nohup 命令 将运行进程提交后台运行,并且可以随时查看运行输出信息,它也可以帮我们保存我们不想要关闭当前终端窗口就消失的内容,比如 下班时 保留vim 正在编辑的代码文件和窗口(我们只需要detach脱离窗口多分器会话即可保留当前操作内容),这样第二天上班我们可以继续昨天的内容了。
窗口创建: screen操作快捷键: CTRL+A,C , tmux快捷键: CTRL+B,C
窗口选择切换: screen操作快捷键: CTRL+A,窗口序号 , tmux快捷键: CTRL+B,窗口序号
attach附着会话session功能:远程操作同屏显示效果(tmux attach/ screen -r)
detach去附着会话操作: screen操作快捷键: CTRL+A,D , tmux快捷键: CTRL+B,D
注意: 可以看到 screen 和 tmux 的快捷键特点, screen 快捷键以 CTRL+A开始 , 而 tmux 以 CTRL+B 开始。其他操作基本类似。
浙公网安备 33010602011771号