我用 vim 不是为了装 B,只是因为习惯。
git clone https://github.com/vim/vim.git
# 安装EPEL源
yum install epel-release -y
# 安装python3开发包
yum install python34-devel -y
cd vim/src
make clean
# 如果需要 python3 支持
./configure --with-features=huge --enable-python3interp --enable-pythoninterp --enable-rubyinterp --enable-luainterp --enable-perlinterp --enable-multibyte --enable-cscope --prefix=/usr/local/vim/
sudo make install
amix 是一个比较优雅的配置文本:https://github.com/amix/vimrc
Vundle:功能大而全。
vim plug:使用感受:简洁友好。
安装 vim plug:clone repo https://github.com/junegunn/vim-plug 之后,将 vim-plug
放在./vim/autoload
即可。
增加需要的目标插件:将类似下面的语句插入到 vimrc 文件中。
call plug#begin('~/.vim/plugged')
Plug 'jayli/vim-easycomplete'
Plug 'jayli/vim-dictionary'
" Initialize plugin system
call plug#end()
安装插件:在 vim 命令模式下输入:PlugInstall
模式:显示在左下角,简单来说,就是在不同模式下做不同的事。
模式 | 做什么 | 怎么进入 | 会显示什么 | 例子 |
---|---|---|---|---|
命令行模式 | 输入命令 | 输入: |
: |
设置行号:set number ,或者替换单词:%s/int/INT/g |
输入模式 | 输入code、字符 | 输入a 或i 或s |
-- INSERT -- |
可直接输入字符 |
可视模式 | 选择字符们 | v 进行逐个字符逐行选择,V (ctrl+v)竖列选择 |
-- VISUAL -- |
可对选好的内容操作,比如删除或者批量操作 |
按 ESC 退出当前模式。
vim test.txt " 打开这个文件
vim test.txt +100 " 打开并定位到第 100 行
vim ./ " 打开当前目录
二进制方式:
首先以二进制方式编辑这个文件 vi -b test
使用 xxd 转换为 16 进制:%!xxd
,如下:
0000000: 1f8b 0808 39d7 173b 0203 7474 002b 4e49 ....9...........
0000010: 4b2c 8660 eb9c ecac c462 eb94 345e 2e30 K,.`.....b..4^.0
0000020: 373b 2731 0b22 0ca6 c1a2 d669 1035 39d9 7;'1.".....i.59.
转回去 :%!xxd -r
:w " 保存
:q " 退出
:wq " 保存并退出
:q! " !表示强行退出
:qa " a表示退出所有
符号:%
用来进行小括号、中括号和大括号的匹配。
以下指令符号中,用大写表示Ctrl+字符按键
在命令行模式下:
b
跳到前一个词
w
跳到后一个词
F
下一页 B
上一页
E
向下滚动一行
Y
向上滚动一行
D
向下滚动半屏
U
向上滚动半屏
[[
gg
跳到文件开头
]]
G
跳到文件结尾
[]
跳到上一个函数结尾
][
跳到下一个函数结尾
:5
跳到文件的第 5 行
0
行首
$
行尾
v
或者V
选中之后,按上下左右,或者G
0
$
之类的,调整目标范围。
y
复制,p
粘贴,多行也可以,竖列操作选中之后,p
还可以竖着粘贴。
yy
复制当前行
dd
删除当前行
d
删除选中的内容
line 1
line 2
line 3
编辑成序列:
this is line 1
this is line 2
this is line 3
V
进入纵向编辑模式,竖向(按上下键)选择后,I
行首插入模式,然后输入this is
,按 ESC,就会得到目标序列结果
替换为:s///g
,需要注意转义字符。
:%s/old/new/g " 将字符串old替换成new
:15s/dog/cat/g " 在15行替换dog为cat
:28,35s/int/INT/g " 在28到35行的int替换为INT
s 的其他用法:
:%s/yes//gn " 查询yes字符串的个数
:%s/\s\+$// " 删除空格
修改 vimrc
fdm:foldmethod
set fdm=indent
no fold found
zi
展开所有
zc
折叠一处
zC
折叠所有
zo
展开一处
zO
展开当前所有
:set wrap 设置自动折行
:set nowrap 设置不自动折行
set 仅为临时的设置(文件格式修改之后是永久的修改,但是也仅仅是对当前文件的修改),如果需要进行编辑器级别的设置,需要将 set 语句放到 vimrc 文件中。
修改文件格式
:set fileencoding " 显示当前文件编码格式
:set encoding=utf-8 fileencodings=ucs-bom,utf-8,cp936 " 设置格式
修改预览格式
set number " 显示行号
set nonumber " 不显示行号
set list " 显示非空格的空白符,比如行尾符$,tab ^I
set nolist " 不显示
提示 80 字符的长度限制:在右侧显示一条限制线,用于提醒我们不要写得太长。这个特性从 7.3 版本开始引入,具体可:help colorcolumn
。
set colorcolumn=81
查找时,大小写忽略,退出后设置失效:
set ic " 忽略大小写(ignore case)
set noic "不忽略大小写
" 显示文件名
set statusline+=%F
set laststatus=2
" 自动排版,按==即可对当前行排版
filetype plugin indent on
set cindent shiftwidth=4
" 显示行号
set number
" Tab 宽度
set ts=4
" 自动缩进
set autoindent
set sw=4
set fdm=indent # 代码折叠 zo 打开当前 zc 折叠当前 zM折叠全部 zR 打开全部
set ff=unix # 设置文件格式为unix
ctags -R
递归生成索引文件,用于跳转查看文件。如果不行,解决方案:文件在根目录里面,可能和以前不一样了,vimrc 设置:
set tags+=~/.vim/systags
set tags=tags;
set autochdir
指令:
ctrl+] " 跳转到定义过的地方
ctrl+o " 按走过的地方回去
vi –t name " 把 name 替换为欲查找的变量或函数名
:ts " ts 助记字:tags list
:tp " tp 助记字:tags preview 此命令不常用,可以不用记
:tn " tn 助记字:tags next 此命令不常用,可以不用记
cscope -Rb
方法生成 cscope.out 文件,这是一个索引文件,类似于 ctags 生成的 tags 索引文件;在源代码目录下打开 vim。用于查看,比如一段函数被哪里调用过。要使用 cscope 查找就必须加载 cscope.out 文件.在 vim 命令行下执行::cs add cscope.out
。如果递归没有成功,尝试:
find `pwd` -name "*.[ch]" -o -name "_.cpp" > cscope.files
cscope -bR -i cscope.files
vim 中,指令:
:cs help
:cs find c calling this function # do*cycle()
e grep
s C Symbol
t in text
YCM(YouCompleteMe)是一个非常精良的补全工具。安装过程有时候会有些问题
YouCompleteMe unavailable no module named builtins
这是因为网络很慢,导致要依赖的文件还没有下载全,所以可以先把文件下载了
cd /home/yourusername/.vim/bundle/YouCompleteMe
git submodule update --init --recursive
。git clone https://github.com/vim/vim.git
cd vim/src
make && make install
./configure \
--disable-nls \
--enable-cscope \
--enable-gui=no \
--enable-multibyte \
--enable-pythoninterp \
--enable-rubyinterp \
--prefix=/home/lixuefei05/.vim \
--with-features=huge \
--with-python-config-dir=/usr/local/lib/python3.8/ \
--with-tlib=ncurses \
--without-x
在 NERDTreeToggle 插件使用时发现。解决方法就就是增加路径。
这个是在编辑完文件,提交到 git 时出现的,是因为换行符不一致。
可以:set binary noeol
如果是在 win 下面,并且是 LF 和 CRLF 混用的情况下,建议还是用之前的编辑器吧。
^M
因为 linux/unix 下的回车符是0d
,而在 windows 下侧是0d0a
^M
可以用 dos2unix 命令去除:
dos2unix filename
or sed -e "s/\r//g" file > newfile
or vi [in ESC mode] type: :%s/^M$//
或者在 vim 中 :set fileformat=unix
TLDR; add export CSCOPE_EDITOR=vim to ~/.bashrc and close your terminal window.
In my case, setting vim as the default editor for cscope fixed the issue:
echo "export CSCOPE_EDITOR=vim" >> ~/.bashrc # Append to .bashrc
source ~/.bashrc # Load .bashrc into the current shell
After this, you may also use the following commands in your project directory for a quick reconfiguration of cscope and ctags:
ctags -R
cscope -R
Yes, you should tell Vim where to find your tags file with something like:
:set tags=/path/to/tags
This is not very optimal, though. This line in your ~/.vimrc should help:
set tags=./tags,tags;$HOME
It tells Vim to look for a tags file in the directory of the current file, in the current directory and up and up until your $HOME (that's the meaning of the semicolon), stopping on the first hit.
ctags -R --languages=C
no connection
error: cs add \$CSCOPE_DB
to my .vimrc