vim 的常用配置及快捷键

本文详细介绍Vim编辑器的高级配置与使用技巧,包括.vimrc文件个性化设置、快捷键应用、插件安装及正则表达式替换等。适合希望提升Vim使用效率的程序员阅读。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

vimrc 配置

整理一下自己经常用到的一些设置,要保存在 .vimrc 中
具体有
1)从其他地方复制造成的问题
2)tab 转4个空格
3)记住上次打开的位置
4)F5直接运行各种脚本

filetype plugin indent on
syntax on
" colorscheme desert

" 很早以前记下的,应该是把tab转为空格,可能还有解决复制错行的问题
set nocompatible
set backspace=indent,eol,start
set autoindent
set softtabstop=4
set shiftwidth=4
set cinoptions={0,1s,t0,n-2,p2s,(03s,=.5s,>1s,=1s,:1s
if &term=="xterm"
    set t_Co=8
             set t_Sb=^[[4%dm
    set t_Sf=^[[3%dm
endif
" 开启行号显示
set nu
" 解决打开文件乱码? (很早之前记的)
set encoding=utf-8 fileencodings=ucs-bom,utf-8,cp936
set pastetoggle=<F9>
" 
" 打开记住上次打开的位置
set viminfo='10,\"100,:20,%,n~/.viminfo 
au BufReadPost * if line("'\"") > 0|if line("'\"") <= line("$")|exe("norm '\"")|else|exe "norm $"|endif|endif

:set ts=4
:set expandtab
:%retab!

" 最近经常用到的,非常好用的编译运行功能
" 原文:https://blog.youkuaiyun.com/zijin0802034/article/details/77709465
map <silent>  <F5> :call CompileRunGcc()<CR>

func! CompileRunGcc()
    exec "w" 
    if &filetype == 'c' 
        exec '!g++ % -o %<'
        exec '!./%<'
    elseif &filetype == 'cpp'
        exec '!g++ % -o %<'
        exec '!./%<'
    elseif &filetype == 'ruby'
        exec '!ruby %'
    elseif &filetype == 'python'
        exec '!python %'
    elseif &filetype == 'sh'
        :! bash %
    endif
endfunc

vim 操作快捷键

主要记一些容易忘记的

编辑类

J 删除换行,使下一行上来

A 进入行尾编辑,$ 这种的仅仅是定位到最后一个字符
I 行首编辑。 注意一般用的是 i 字符左侧编辑,a 字符右侧编辑

:[range]copy {address} 直接命令行复制,如 5, 8 copy/move 10

括号匹配

% 查到匹配括号
下面可参考
vi / yi / di 分别选中、复制、删除括号中的内容。 只要在内部就行
将i换成a 可以包括括号本身。
注意使用时类似 vi"/ vi’ / vi(

查看类

zz 将本行定在屏幕中,类似有 zt, zb
f / F 行内快速定位

ctrl+b/f 前后翻页
ctrl+d/u 前后半页
ctrl+p/n 前后一行,注意ctrl+n在编辑模式下是非常有用的补全命令
ctrl+e/y 前后一行,但是光标不动

https://www.cnblogs.com/lxwphp/p/7738351.html
w / b /e 单词级别移动
回车/ 空格/ 回退 命令模式下移动

切换命令模式

ctrl+o 只能切换一次
ctrl+[ 可以代替 Esc
此外还有 比较危险的 ctrl+c

命令模式

ctrl+r 0 vim 中复制的可以送上来
% 12,. 12,$ 12, 22 代表range位置索引,可用于 替换 s, copy 等
r ! cat file.txt 可直接读入命令结果,很好用

实用案例

复制word 搜索

v1:

  1. yaw 复制文字
  2. 进入命令模式
  3. /
  4. C+r 0 读取寄存器值

v2:
* 可以直接搜索该词

v3: [全选word]
bve 单词最开头选中结尾

vim 插件

其实主要是 Vundle

摘出来如下,不过需要执行以下进行安装

git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim
vim +PluginInstall +qall
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'

" The following are examples of different formats supported.
" Keep Plugin commands between vundle#begin/end.
" plugin on GitHub repo
Plugin 'tpope/vim-fugitive'
" plugin from http://vim-scripts.org/vim/scripts.html
" Plugin 'L9'
" Git plugin not hosted on 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'
" The sparkup vim script is in a subdirectory of this repo called vim.
" 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
" different version somewhere else.
" Plugin 'ascenator/L9', {'name': 'newL9'}

Plugin 'vhdirk/vim-cmake'

" All of your Plugins must be added before the following line
call vundle#end()            " required
filetype plugin indent on    " required
" 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



" Specify a directory for plugins
" - For Neovim: stdpath('data') . '/plugged'
" - Avoid using standard Vim directory names like 'plugin'
"call plug#begin('~/.vim/plugged')
"call plug#begin('/home/wa/.vim/plugged')

安装vim 8

home="xxx"
./configure  --prefix="$home/bin/" --with-features=huge \
    --enable-multibyte \
    --enable-rubyinterp=dynamic \
    --with-ruby-command="$home/bin/bin/ruby" \
    --enable-pythoninterp=no \
    --enable-python3interp=yes \
    --with-python3-config-dir="$home/bin/anaconda3/lib/python3.8/config-3.8-x86_64-linux-gnu/" \
    --enable-cscope \
    --enable-gui=auto \
    --enable-gtk2-check \
    --enable-fontset \
    --enable-largefile \
    --disable-netbeans \
    --enable-fail-if-missing

make -j8 && make install

vim 命令行

vim 支持从脚本加载命令比如

 vim vs_sky.hlsl -s cmd.vim

cmd.vim

ggvG=:wq

通过 :help -s 可以看到其实是执行了 :source! file

正则替换

%s#Item(\(\S\+\))#\1#g 展示了如何匹配与替换,注意\1代表了匹配的内容
13,$s#get_HOMEDIR()#_REL_HOMEDIR#g 从某行开始把字符串替换,注意括号是字符串的一部分

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值