vim常用命令

vim 是 Linux 系统上一款常用的文本编辑器

编辑文件: vim filenamevi filename
只读模式查看文件: view filename

常用命令

# 1.进入输入状态的命令

a #从光标所在位置后追加文字
A #从光标所在行最后追加文字
i #从光标所在位置前面插入文字
I #从光标所在行首开始插入文字
o #在光标所在行下新增一行
O #在光标所在行上方新增一行

# 2. 删除命令

x   #删除光标所在字符
dw  #删除光标所在整个单词
dd  #删除光标所在的行
ndd #删除从光标所在行到n行
d$  #删除从光标到所在行结尾的字符
dG  #删除当前所在行到文件结尾的字符

# 3. 修改命令

r #修改光标所在字符
R #修改光标所在行

# 4. 删除和修改命令

s #删除单个字符并进入输入状态
S #删除所在行并进入输入状态

# 5. 光标位置移动

0 #移动到光标所在的最前面
$ #移动到光标所在的最后面
H #跳到第一行
M #跳到中间一行
L #跳到最后一行

ctrl f #向下翻一页
ctrl b #向上翻一页

1G #回到行首
G  #到最底行

# 6. 内容查找

/name #向下查找name
/name\c #查找name不区分大小写
?name #向上查找name

n #重复查找下一个匹配
N #反向查找下一个匹配

# 7. 内容替换

:%s/old_world/new_world/g #整个文件中替换

# 8. 内容复制和位置变换

y   #单行复制
yw  #单词复制
nyy #多行复制
p #粘贴

# 9. 退出保存

:q  #退出
:wq #保存退出
:x  #保存退出
:q! #强制退出

其他常用命令

# 1. vim复制缩进排版全乱
:set paste
# 进入paste模式以后,可以在插入模式下粘贴内容,不会有任何变形

# 2. 显示行号
:set number
# 或
:se nu

# 3. 跳转到指定行
:1000 # 跳到1000行

#4. 撤销与反撤销
u #撤销
ctrl + r #反撤销

编辑.vimrc文件

""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"" 参考 https://github.com/ma6174/vim-deprecated/blob/master/.vimrc
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"" 显示相关
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" 语法高亮
syntax on
" 高亮光标所在行
set cul
" 去掉一致性模式,避免以前版本的局限
set nocompatible
" 自动缩进
set autoindent
set cindent
" Tab键的宽度
set tabstop=4
" 统一缩进为4
set softtabstop=4
set shiftwidth=4
" 使用空格代替制表符
set expandtab
" 在行和段开始处使用制表符
set smarttab
" 显示行号
set number
" 历史记录数
set history=1000
"搜索逐字符高亮
set hlsearch
set incsearch
" 侦测文件类型
filetype on
" 载入文件类型插件
filetype plugin on
" 为特定文件类型载入相关缩进文件
filetype indent on
" 将tab替换为空格
nmap tt :%s/\t/    /g<CR>

""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"" 新文件模板
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" 新建.sh,.py文件,自动插入文件头 
autocmd BufNewFile *.sh,*.py exec ":call SetTitle()" 
" 定义函数SetTitle,自动插入文件头 
func SetTitle()
  if &filetype == 'sh'
    call setline(1,"\#!/usr/bin/env bash")
    call append(line("."), "# File Name: ".expand("%"))
    call append(line(".")+1, "# Created Time: ".strftime("%Y-%m-%d %H:%M:%S"))
    call append(line(".")+2, "")
    elseif &filetype == 'python'
        call setline(1,"#!/usr/bin/env python3")
        call append(line("."), "# coding=utf-8")
        call append(line(".")+1, "")
  endif
endfunc
" 新建文件后,自动定位到文件末尾
autocmd BufNewFile * normal G

""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"" 键盘命令
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"sh, python 按F5运行
map <F5> :call CompileRunGcc()<CR>
func! CompileRunGcc()
 exec "w"
 if &filetype == 'sh'
  :!time bash %
 elseif &filetype == 'python'
  exec "!time python3 %"
 endif
endfunc

""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"" 自动补全
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
:inoremap ( ()<ESC>i
:inoremap ) <c-r>=ClosePair(')')<CR>
:inoremap { {}<ESC>i
:inoremap } <c-r>=ClosePair('}')<CR>
:inoremap [ []<ESC>i
:inoremap ] <c-r>=ClosePair(']')<CR>
:inoremap " ""<ESC>i
:inoremap ' ''<ESC>i
function! ClosePair(char)
        if getline('.')[col('.') - 1] == a:char
                return "\<Right>"
        else
                return a:char
        endif
endfunction
posted @ 2023-06-15 22:26  rustling  阅读(59)  评论(1编辑  收藏  举报