Vim

Vim 简介

1. 三种模式

  • Normal mode:输入命令
  • Insert mode:输入文本,Esc 退出 Insert mode,进入 Normal mode
  • Last Line mode:在 Normal mode 下,输入命令 : 进入;Enter 退出Last Line mode,进入 Normal mode

 

常用命令

1. 插入命令

命令 功能
a 在光标后附加文本
A 在本行行末附加文本
i 在光标前附加文本
I 在本行行首附加文本
o 在光标下插入新行
O 在光标上插入新行

 

2. 定位命令(命令模式下)

命令 功能
h 左移(相当于方向左键)
j 下移(相当于方向下键)
k 上移(相当于方向上键)
l 右移(相当于方向右键)
$ 移至行尾
0 移至行首
   
w 下一词首
b 上一词首
e 下一词尾
   
Ctrl + o 调到上一次的位置
Ctrl + i 调到下一次的位置
   
Ctrl + f 下一页
 Ctrl + b 上一页 
H 移至屏幕上端
M 移至屏幕中央
L 移至屏幕下端
   
:set nu 设置行号
:set nonu 取消行号
gg 到第一行
G 到最后一行
nG 到第n行
:n 到第n行

 

3. 删除命令

命令 功能
x 删除光标所在处字符
nx 删除光标所在处后n个字符
dd 删除光标所在行
ndd 删除光标所在处后n行
dG 删除光标所在行到末尾的内容
D 删除从光标所在处到行尾
:n1,n2d 删除指定范围的行

 

4. 复制和粘贴

命令 功能
yy 或 Y 复制当前行
nyy 或 nY 复制当前行以下n行
dd 剪切当前行
ndd 剪切当前行以下n行
p、P 粘贴在当前光标所在行下或行上

 

5. 替换和取消

命令 功能
r 取代光标所在处字符
R 从光标所在处开始替换
u 取消上一步操作

 

6. 搜索和替换

命令 功能
/string

向前搜索指定字符串

搜索时忽略大小写 :set ic

n 搜索指定字符串的下一个出现位置
:%s/old/new/g 全文替换指定字符串
:n1,n2s/old/new/g 在一定范围内替换指定字符串

 7. 保存与退出

命令 功能
:w 写入文件
:w! 当文件为只读时,强制写入
:q 离开
:q! 强制离开,且不保存
:wq 强制离开,保存
ZZ 同上
:w [filename] 另存为
:r [filename] 将 filename 文件数据读入 当前本件中(光标后面)
:n1 n2 w [filename] 将 n1 - n2 数据保存到 filename 中

 

配置文件 vimrc

 在 ~/.vimrc 文件中配置(如无此文件,则手动创建)

set nu      " 设置行号
syntax on    " 语法高亮
set showcmd   "显示命令

 

插件管理

1. 插件管理工具 Vundle 

  • 安装
$ git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim
  • 配置

将下面代码放在 ~/.vimrc 文件的开头          

set nocompatible
filetype on
" 设置 runtime path 并初始化 set rtp+=~/.vim/bundle/Vundle.vim call vundle#begin() " let Vundle manage Vundle, required Plugin 'VundleVim/Vundle.vim' " Keep Plugin commands between vundle#begin/end 需安装的插件必须置于 vundle#begin()和vundle#end()之间 " plugin on GitHub repo, GitHub托管的插件(只写 作者名/项目名 即可) Plugin 'tpope/vim-fugitive'
Plugin 'Valloric/YouCompleteMe'
" plugin from http://vim-scripts.org/vim/scripts.html,若插件来自于 vim-scripts(官方),直接写插件名即可 " Plugin 'L9'
" Git plugin not hosted on GitHub,若Git插件不在GitHub上,需提供完整的链接 Plugin 'git://git.wincent.com/command-t.git'
" git repos on your local machine (i.e. when working on your own plugin),本地的插件需提供文件路径 " Plugin 'file:///home/gmarik/path/to/plugin'
" Pass the path to set the runtimepath properly. " Plugin 'rstacruz/sparkup', {'rtp': 'vim/'}
" Install L9 and avoid a Naming conflict if you've already installed a " Plugin 'ascenator/L9', {'name': 'newL9'} call vundle#end() filetype plugin indent on
" 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

  • 安装插件
    • 保存 ~/.vimrc 文件并退出
    • 再次用 vim 打开 ~/.vimrc 文件,在命令模式下输入 :PluginInstall,回车即可
命令 功能
:PluginList 列出配置好的插件
:PluginInstall 安装在 .vimrc 文件中列出的插件

:PluginInstall!

:PluginUpdate

更新列表的全部插件
:PluginSearch foo 查找 foo 插件
:PluginSearch! foo  刷新 foo 插件缓存
:PluginClean 清除无用插件(需确认) 
:PluginClean! 清除无用插件(无需确认)
:h 帮助和说明文档

 

2. YouCompleteMe

YouComplete 是 Vim 一款十分强大的自动补全插件(a code-completion engine for vim)。

  • 安装
    • 方法:1:如上用 Vundle 安装(即在 .vimrc 文件适当位置添加 Plugin 'Valloric/YouCompleteMe',然后输入 :PluginInstall 回车)
    • 方法2:切换到 .vim/bundle 目录下手动下载
cd ~/.vim/bundle
git clone https://github.com/Valloric/YouCompleteMe.git
  • 检查仓库完整性
cd ~/.vim/bundle/YouCompleteMe
git submodule update --init --recursive
  • 下载最新的 libclang

Clang是一个开源的编译器,它可以编译C/C++/Objective-C/Objective-C++. Clang提供的libclang库是用来给YCM为这些语言提供语义补全的引擎。和YCM配合的libclang版本必须 >=3.6。

sudo apt-get install libclang-dev
  • 编译 YouComplete 需要的 ycm_support_libs 库
sudo apt-get install build-essential cmake
sudo apt-get install python-dev python3-dev
cd ~/.vim/bundle/YouCompleteMe

./install.py            " 不需要语义补全
./install.py --clang-completer " 需要语义补全
  • 配置 YouCompleteMe
    • 在 ~/.vim/bundle/YouCompleteMe/third_party/ycmd/.ycm_extra_conf.py 文件中,添加如下代码:
 '-isystem',
 '/usr/include',
 '-isystem',
 '/usr/include/c++/5.4.0',
 '-isystem',
 '/usr/include',
 '/usr/include/x86_64-linux-gnu/c++',
    • 在 ~/.vimrc 文件中添加:
"YouCompleteMe 插件配置 
let g:ycm_global_ycm_extra_conf='~/.vim/bundle/YouCompleteMe/third_party/ycmd/.ycm_extra_conf.py' 
nnoremap <leader>jd :YcmCompleter GoToDefinitionElseDeclaration<CR>
let g:ycm_python_binary_path = '/usr/bin/python3'
nmap<C-a> :YcmCompleter FixIt<CR>

 

 

 

 

posted @ 2019-04-06 18:12  Hongkai_Ding  阅读(203)  评论(0编辑  收藏  举报