vi是Visual interface的简称,是Linux中最经典的文本编辑器。在没有图形界面的环境下,要编辑文件,vi是最佳选择。
vi
的特点:- 没有图形界面 的 功能强大 的编辑器
- 只能是编辑 文本内容,不能对字体、段落进行排版
- 不支持鼠标操作
- 没有菜单
- 只有命令
vi
编辑器在 系统管理、服务器管理 编辑文件时,其功能永远不是图形界面的编辑器能比拟的
vim
vim = vi improved
vim
是从vi
发展出来的一个文本编辑器,支持 代码补全、编译 及 错误跳转 等方便编程的功能特别丰富,在程序员中被广泛使用,被称为 编辑器之神
查询软连接命令(知道)
- 在很多
Linux
发行版中直接把vi
做成vim
的软连接
vi
有三种基本工作模式:
- 命令模式
- 打开文件首先进入命令模式,是使用
vi
的 入口 - 通过 命令 对文件进行常规的编辑操作,例如:定位、翻页、复制、粘贴、删除……
- 在其他图形编辑器下,通过 快捷键 或者 鼠标 实现的操作,都在 命令模式 下实现
- 打开文件首先进入命令模式,是使用
- 末行模式 —— 执行 保存、退出 等操作
- 要退出
vi
返回到控制台,需要在末行模式下输入命令 - 末行模式 是
vi
的 出口
- 要退出
- 编辑模式 —— 正常的编辑文字
打开和新建文件
vi 文件名
打开文件并且定位行
vi 文件名 +行数
### 末行模式命令
| 命令 | 英文 | 功能 |
| :---: | :---: | --- |
| w | write | 保存 |
| q | quit | 退出,如果没有保存,不允许退出 |
| q! | quit | 强行退出,不保存退出 |
| wq | write & quit| 保存并退出 |
| x | | 保存并退出 |
移动命令
行内移动
行内移动
| 命令 | 英文 | 功能 |
| w | word | 向后移动一个单词 |
| b | back | 向前移动一个单词 |
| 0 | | 行首 |
| ^ | | 行首,第一个不是空白字符的位置 |
| $ | | 行尾 |
行数移动
行数移动
| 命令 | 英文 | 功能 |
| gg | go | 文件顶部 |
| G | go | 文件末尾 |
| 数字gg | go | 移动到 数字 对应行数 |
| 数字G | go | 移动到 数字 对应行数 |
| :数字 | | 移动到 数字 对应行数 |
屏幕移动
屏幕移动
| 命令 | 英文 | 功能 |
| Ctrl + b | back | 向上翻页 |
| Ctrl + f | forward | 向下翻页 |
| H | Head | 屏幕顶部 |
| M | Middle | 屏幕中间 |
| L | Low | 屏幕底部 |
段落移动
vi
中使用 空行 来区分段落- 在程序开发时,通常 一段功能相关的代码会写在一起 —— 之间没有空行
| 命令 | 功能 |
| { | 上一段 |
| } | 下一段 |
% 括号匹配及切换
标记
| 命令 | 英文 | 功能 |
| mx | mark| 添加标记 x,x 是 a~z 或者 A~Z 之间的任意一个字母 |
| 'x | | 直接定位到标记 x 所在位置 |
选中文本(可视模式)
- 学习
复制
命令前,应该先学会 怎么样选中 要复制的代码 - 在
vi
中要选择文本,需要先使用Visual
命令切换到 可视模式 vi
中提供了 三种 可视模式,可以方便程序员选择 选中文本的方式- 按
ESC
可以放弃选中,返回到 命令模式
| 命令 | 模式 | 功能 |
| v | 可视模式 | 从光标位置开始按照正常模式选择文本 |
| V | 可视行模式 | 选中光标经过的完整行 |
| Ctrl + v | 可视块模式 | 垂直方向选中文本 |
撤销和恢复撤销
u 撤销上次命令
CTRL + r 恢复撤销的命令
删除文本
| 命令 | 英文 | 功能 |
| x | cut | 删除光标所在字符,或者选中文字 |
| d(移动命令) | delete | 删除移动命令对应的内容 |
| dd | delete | 删除光标所在行,可以 ndd 复制多行 |
| D | delete | 删除至行尾 |
如果使用 可视模式 已经选中了一段文本,那么无论使用
d
还是x
,都可以删除选中文本
- 删除命令可以和 移动命令 连用,以下是常见的组合命令:
dw # 从光标位置删除到单词末尾
d0 # 从光标位置删除到一行的起始位置
d} # 从光标位置删除到段落结尾
ndd # 从光标位置向下连续删除 n 行
d代码行G # 从光标所在行 删除到 指定代码行 之间的所有代码
d'a # 从光标所在行 删除到 标记a 之间的所有代码
复制、粘贴
vi
中提供有一个 被复制文本的缓冲区- 复制 命令会将选中的文字保存在缓冲区
- 删除 命令删除的文字会被保存在缓冲区
- 在需要的位置,使用 粘贴 命令可以将缓冲区的文字插入到光标所在位置
| 命令 | 英文 | 功能 |
| y(移动命令) | copy | 复制 |
| yy | copy | 复制一行,可以 nyy 复制多行 |
| p | paste | 粘贴 |
可以在 编辑模式 下使用 鼠标右键粘贴
替换
| 命令 | 英文 | 功能 | 工作模式 |
| r | replace | 替换当前字符 | 命令模式 |
| R | replace | 替换当前行光标后的字符 | 替换模式 |
缩排和重复执行
| >> | 向右增加缩进 |
| << | 向左减少缩进 |
| . | 重复上次命令 |
在 可视模式 下,缩排命令只需要使用 一个 >
或者 <
查找
| /str | 查找 str |
- 查找到指定内容之后,使用
Next
查找下一个出现的位置:n
: 查找下一个N
: 查找上一个
- 如果不想看到高亮显示,可以随便查找一个文件中不存在的内容即可
单词快速匹配
| 命令 | 功能 |
| * | 向后查找当前光标所在单词 |
| # | 向前查找当前光标所在单词 |
查找并替换
- 在
vi
中查找和替换命令需要在 末行模式 下执行 - 记忆命令格式:
:%s///g
1) 全局替换
- 一次性替换文件中的 所有出现的旧文本
- 命令格式如下:
:%s/旧文本/新文本/g
2) 可视区域替换
- 先选中 要替换文字的 范围
- 命令格式如下:
:s/旧文本/新文本/g
3) 确认替换
- 如果把末尾的
g
改成gc
在替换的时候,会有提示!推荐使用!
:%s/旧文本/新文本/gc
y
-yes
替换n
-no
不替换a
-all
替换所有q
-quit
退出替换l
-last
最后一个,并把光标移动到行首^E
向下滚屏^Y
向上滚屏
插入命令
在 vi
中除了常用的 i
进入 编辑模式 外,还提供了以下命令同样可以进入编辑模式:
| 命令 | 英文 | 功能 | 常用 |
| i | insert | 在当前字符前插入文本 | 常用 |
| I | insert | 在行首插入文本 | 较常用 |
| a | append | 在当前字符后添加文本 | |
| A | append | 在行末添加文本 | 较常用 |
| o | | 在当前行后面插入一空行 | 常用 |
| O | | 在当前行前面插入一空行 | 常用 |
末行命令扩展
末行命令 主要是针对文件进行操作的:保存、退出、保存&退出、搜索&替换、另存、新建、浏览文件| 命令 | 英文 | 功能 |
| :e . | edit | 会打开内置的文件浏览器,浏览要当前目录下的文件 |
| :n 文件名 | new | 新建文件 |
| :w 文件名 | write | 另存为,但是仍然编辑当前文件,并不会切换文件 |
| 命令 | 英文 | 功能 |
| :w | write | 保存 |
| :q | quit | 退出,如果没有保存,不允许退出 |
| :q! | quit | 强行退出,不保存退出 |
| :wq | write & quit | 保存并退出 |
| :x | | 保存并退出 |
| :%s///gc | | 确认搜索并替换 |
分屏命令
使用 分屏命令,可以 同时编辑和查看多个文件
| 命令 | 英文 | 功能 |
| :sp [文件名] | split | 横向增加分屏 |
| :vsp [文件名] | vertical split | 纵向增加分屏 |
1) 切换分屏窗口
分屏窗口都是基于 CTRL + W
这个快捷键的,w
对应的英文单词是 window
| 命令 | 英文 | 功能 |
| w | window | 切换到下一个窗口 |
| r | reverse | 互换窗口 |
| c | close | 关闭当前窗口,但是不能关闭最后一个窗口 |
| q | quit | 退出当前窗口,如果是最后一个窗口,则关闭 vi |
| o | other | 关闭其他窗口 |
2) 调整窗口大小
| 命令| | 功能 |
| + | | 增加窗口高度 |
| - | | 减少窗口高度 |
| > | | 增加窗口宽度 |
| < | | 减少窗口宽度 |
| = | | 等分窗口大小 |
调整窗口宽高的命令可以和数字连用,例如:5 CTRL + W +
连续 5 次增加高度
常用命令速查图
vimrc
vimrc
是vim
的配置文件,可以设置 vim 的配置,包括:热键、配色、语法高亮、插件 等Linux
中vimrc
有两个位置,家目录下的配置文件优先级更高
/etc/vim/vimrc ~/.vimrc
- 常用的插件有:
- 代码补全
- 代码折叠
- 搜索
- Git 集成
- ……
- 网上有很多高手已经配置好的针对
python
开发的vimrc
文件,可以下载过来直接使用,或者等大家多Linux
比较熟悉后,再行学习!
附:
1.ubuntu下配置
" All system-wide defaults are set in $VIMRUNTIME/debian.vim and sourced by
" the call to :runtime you can find below. If you wish to change any of those
" settings, you should do it in this file (/etc/vim/vimrc), since debian.vim
" will be overwritten everytime an upgrade of the vim packages is performed.
" It is recommended to make changes after sourcing debian.vim since it alters
" the value of the 'compatible' option.
" This line should not be removed as it ensures that various options are
" properly set to work with the Vim-related packages available in Debian.
runtime! debian.vim
" Uncomment the next line to make Vim more Vi-compatible
" NOTE: debian.vim sets 'nocompatible'. Setting 'compatible' changes numerous
" options, so any other options should be set AFTER setting 'compatible'.
"set compatible
" Vim5 and later versions support syntax highlighting. Uncommenting the next
" line enables syntax highlighting by default.
if has("syntax")
syntax on
endif
" If using a dark background within the editing area and syntax highlighting
" turn on this option as well
"set background=dark
" Uncomment the following to have Vim jump to the last position when
" reopening a file
"if has("autocmd")
" au BufReadPost * if line("'\"") > 1 && line("'\"") <= line("$") | exe "normal! g'\"" | endif
"endif
" Uncomment the following to have Vim load indentation rules and plugins
" according to the detected filetype.
"if has("autocmd")
" filetype plugin indent on
"endif
" The following are commented out as they cause vim to behave a lot
" differently from regular Vi. They are highly recommended though.
"set showcmd " Show (partial) command in status line.
"set showmatch " Show matching brackets.
"set ignorecase " Do case insensitive matching
"set smartcase " Do smart case matching
"set incsearch " Incremental search
"set autowrite " Automatically save before commands like :next and :make
"set hidden " Hide buffers when they are abandoned
"set mouse=a " Enable mouse usage (all modes)
" Source a global configuration file if available
if filereadable("/etc/vim/vimrc.local")
source /etc/vim/vimrc.local
endif
syntax on "自动语法高亮
"winpos 5 5 " 设定窗口位置
"set lines=40 columns=155 " 设定窗口大小
set nu " 显示行号
set go= " 不要图形按钮
"color asmanian2 " 设置背景主题
set guifont=Courier_New:h10:cANSI " 设置字体
"syntax on " 语法高亮
autocmd InsertLeave * se nocul " 用浅色高亮当前行
autocmd InsertEnter * se cul " 用浅色高亮当前行
set ruler " 显示标尺
set showcmd " 输入的命令显示出来,看的清楚些
"set cmdheight=1 " 命令行(在状态行下)的高度,设置为1
"set whichwrap+=<,>,h,l " 允许backspace和光标键跨越行边界(不建议)
"set scrolloff=3 " 光标移动到buffer的顶部和底部时保持3行距离
set novisualbell " 不要闪烁(不明白)
set statusline=%F%m%r%h%w\ [FORMAT=%{&ff}]\ [TYPE=%Y]\ [POS=%l,%v][%p%%]\ %{strftime(\"%d/%m/%y\ -\ %H:%M\")} "状态行显示的内容
set laststatus=1 " 启动显示状态行(1),总是显示状态行(2)
"set foldenable " 允许折叠
"set foldmethod=manual " 手动折叠
"set background=dark "背景使用黑色
set nocompatible "去掉讨厌的有关vi一致性模式,避免以前版本的一些bug和局限
" 显示中文帮助
if version >= 603
set helplang=cn
set encoding=utf-8
endif
" 设置配色方案
"colorscheme murphy
"字体
"if (has("gui_running"))
" set guifont=Bitstream\ Vera\ Sans\ Mono\ 10
"endif
set nobackup "no backup
set tabstop=4 " 统一缩进为4
set shiftwidth=4
set pastetoggle=<F6> "插入模式粘贴不会自动缩进避免混乱
set autoindent "自动缩进
set cindent
"""""""""""""""""""
" 显示相关
"""""""""""""""""""
"set shortmess=atI " 启动的时候不显示那个援助乌干达儿童的提示
"winpos 5 5 " 设定窗口位置
"set lines=40 columns=155 " 设定窗口大小
"set nu " 显示行号
set go= " 不要图形按钮
"color asmanian2 " 设置背景主题
set guifont=Courier_New:h10:cANSI " 设置字体
"syntax on " 语法高亮
autocmd InsertLeave * se nocul " 用浅色高亮当前行
autocmd InsertEnter * se cul " 用浅色高亮当前行
"set ruler " 显示标尺
set showcmd " 输入的命令显示出来,看的清楚些
"set cmdheight=1 " 命令行(在状态行下)的高度,设置为1
"set whichwrap+=<,>,h,l " 允许backspace和光标键跨越行边界(不建议)
"set scrolloff=3 " 光标移动到buffer的顶部和底部时保持3行距离
set novisualbell " 不要闪烁(不明白)
set statusline=%F%m%r%h%w\ [FORMAT=%{&ff}]\ [TYPE=%Y]\ [POS=%l,%v][%p%%]\ %{strftime(\"%d/%m/%y\ -\ %H:%M\")} "状态行显示的内容
set laststatus=1 " 启动显示状态行(1),总是显示状态行(2)
"set foldenable " 允许折叠
"set foldmethod=manual " 手动折叠
"set background=dark "背景使用黑色
set nocompatible "去掉讨厌的有关vi一致性模式,避免以前版本的一些bug和局限
" 显示中文帮助
if version >= 603
set helplang=cn
set encoding=utf-8
endif
" 设置配色方案
"colorscheme murphy
"字体
"if (has("gui_running"))
" set guifont=Bitstream\ Vera\ Sans\ Mono\ 10
"endif
set fencs=utf-8,ucs-bom,shift-jis,gb18030,gbk,gb2312,cp936
set termencoding=utf-8
set encoding=utf-8
set fileencodings=ucs-bom,utf-8,cp936
set fileencoding=utf-8
"""""""""""""""""""""""""
"""""新文件标题""""""""""
"新建.c,.h,.sh,.java文件,自动插入文件头 " All system-wide defaults are set in $VIMRUNTIME/debian.vim and sourced by
2 " the call to :runtime you can find below. If you wish to change any of those
3 " settings, you should do it in this file (/etc/vim/vimrc), since debian.vim
4 " will be overwritten everytime an upgrade of the vim packages is performed.
5 " It is recommended to make changes after sourcing debian.vim since it alters
6 " the value of the 'compatible' option.
7
8 " This line should not be removed as it ensures that various options are
9 " properly set to work with the Vim-related packages available in Debian.
10 runtime! debian.vim
11
12 " Uncomment the next line to make Vim more Vi-compatible
13 " NOTE: debian.vim sets 'nocompatible'. Setting 'compatible' changes numerous
14 " options, so any other options should be set AFTER setting 'compatible'.
15 "set compatible
16
17 " Vim5 and later versions support syntax highlighting. Uncommenting the next
18 " line enables syntax highlighting by default.
19 if has("syntax")
20 syntax on
21 endif
22
23 " If using a dark background within the editing area and syntax highlighting
24 " turn on this option as well
25 "set background=dark
26
27 " Uncomment the following to have Vim jump to the last position when
28 " reopening a file
29 "if has("autocmd")
30 " au BufReadPost * if
autocmd BufNewFile *.cpp,*.[ch],*.sh,*.java exec ":call SetTitle()"
""定义函数SetTitle,自动插入文件头
func SetTitle()
"如果文件类型为.sh文件
if &filetype == 'sh'
call setline(1,"\#########################################################################")
call append(line("."), "\# File Name: ".expand("%"))
call append(line(".")+1, "\# Author: test")
call append(line(".")+2, "\# mail: <a href="mailto:test@jbxue.com">test@jbxue.com</a>")
call append(line(".")+3, "\# Created Time: ".strftime("%c"))
call append(line(".")+4, "\#########################################################################")
call append(line(".")+5, "\#!/bin/bash")
call append(line(".")+6, "")
else
call setline(1, "/*************************************************************************")
call append(line("."), " > File Name: ".expand("%"))
call append(line(".")+1, " > Author: test")
call append(line(".")+2, " > Mail: <a href="mailto:test@jbxue.com">test@jbxue.com</a> ")
call append(line(".")+3, " > Created Time: ".strftime("%c"))
call append(line(".")+4, " ************************************************************************/")
call append(line(".")+5, "")
endif
if &filetype == 'cpp'
call append(line(".")+6, "#include<iostream>")
call append(line(".")+7, "using namespace std;")
call append(line(".")+8, "")
endif
if &filetype == 'c'
call append(line(".")+6, "#include<stdio.h>")
call append(line(".")+7, "")
endif
"新建文件后,自动定位到文件末尾
autocmd BufNewFile * normal G
endfunc
"""""""""""""""""""""""""""""""
"键盘命令
"""""""""""""""""""""""""""""""
nmap <leader>w :w!<cr>
nmap <leader>f :find<cr>
" 映射全选+复制 ctrl+a
map <C-A> ggVGY
map! <C-A> <Esc>ggVGY
map <F12> gg=G
" 选中状态下 Ctrl+c 复制
vmap <C-c> "+y
"去空行
nnoremap <F2> :g/^\s*$/d<CR>
"比较文件
nnoremap <C-F2> :vert diffsplit
"新建标签
map <M-F2> :tabnew<CR>
"列出当前目录文件
map <F3> :tabnew .<CR>
"打开树状文件目录
map <C-F3> \be
"C,C++ 按F5编译运行
map <F5> :call CompileRunGcc()<CR>
func! CompileRunGcc()
exec "w"
if &filetype == 'c'
exec "!g++ % -o %<"
exec "! ./%<"
elseif &filetype == 'cpp'
exec "!g++ % -o %<"
exec "! ./%<"
elseif &filetype == 'java'
exec "!javac %"
exec "!java %<"
elseif &filetype == 'sh'
:!./%
endif
endfunc
"C,C++的调试
map <F8> :call Rungdb()<CR>
func! Rungdb()
exec "w"
exec "!g++ % -g -o %<"
exec "!gdb ./%<"
endfunc
""""""""""""""""""""""""
""实用设置
""""""""""""""""""""""""
" 设置当文件被改动时自动载入
set autoread
" quickfix模式
autocmd FileType c,cpp map <buffer> <leader><space> :w<cr>:make<cr>
"代码补全
set completeopt=preview,menu
"允许插件
filetype plugin on
"共享剪贴板
set clipboard+=unnamed
"从不备份
set nobackup
"make 运行
:set makeprg=g++\ -Wall\ \ %
"自动保存
set autowrite
set ruler " 打开状态栏标尺
set cursorline " 突出显示当前行
set magic " 设置魔术
set guioptions-=T " 隐藏工具栏
set guioptions-=m " 隐藏菜单栏
"set statusline=\ %<%F[%1*%M%*%n%R%H]%=\ %y\ %0(%{&fileformat}\ %{&encoding}\ %c:%l/%L%)\
" 设置在状态行显示的信息
set foldcolumn=0
set foldmethod=indent
set foldlevel=3
"set foldenable " 开始折叠
" 不要使用vi的键盘模式,而是vim自己的
set nocompatible
" 语法高亮
set syntax=on
" 去掉输入错误的提示声音
set noeb
" 在处理未保存或只读文件的时候,弹出确认
set confirm
" 自动缩进
set autoindent
set cindent
" Tab键的宽度
set tabstop=4
" 统一缩进为4
set softtabstop=4
set shiftwidth=4
" 不要用空格代替制表符
set noexpandtab
" 在行和段开始处使用制表符
set smarttab
" 显示行号
set number
" 历史记录数
set history=1000
"禁止生成临时文件
set nobackup
set noswapfile
"搜索忽略大小写
set ignorecase
"搜索逐字符高亮
set hlsearch
set incsearch
"行内替换
set gdefault
"编码设置
set enc=utf-8
set fencs=utf-8,ucs-bom,shift-jis,gb18030,gbk,gb2312,cp936
2.mac下配置