vi的几个实用技巧

本文介绍了Vi编辑器的多种实用技巧,包括cscope插件的安装与使用、代码替换、高亮查找、代码对齐等功能,同时提供了多文件编辑、对比及运行控制脚本的方法。

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

转载时请注明出处和作者联系方式:http://blog.youkuaiyun.com/mimepp

作者联系方式:YU TAO <yut616 at sohu dot com>


在这里列一下自己经常用到的vi的一些功能,算是一个笔记.

1. cscope插件,帮助读代码

下载地址:

cscope.sourceforge.net/cscope_maps.vim

将其copy到你的home, 我是用的root,就copy到
/root/.vim/plugin/cscope_maps.vim
.vim/plugin目录可以自己建

插件加载成功的话, 输入":cs" 应该能看到帮助信息.

在vi中可以使用"ctrl+]"来跳转到代码的定义处.
使用"ctrl+/, 再按s"来查看调用者列表, 这个是快捷键方式, 或者手工输入":cs f s XXXX"也是一样.

有了cscope, 基本就不需要ctags了.

2. 代码替换
:%s/AAAAA/BBBB/g
就是将代码中所有的AAAAA换成BBBB, g是全局搜索.

3. 高亮,查找,跳转
在某个.c中, 把光标移动到你要查的字串上, 按小键盘的 * 键, 就可以高亮一个字串, 再按"n"或"shift+n"来向下或向上跳转到某个字符.

4. 代码对齐
在代码的头上加上
/*
 * $Header: Exp $
 * vim:cindent:ts=8:
 */
会帮助你在写代码时, 自动缩进.

5. split编辑多个文件
命令":split XXXX"
ctrl+w, 再加向上的箭头,就可以转到上面的编辑区域
ctrl+w, 再加向下的箭头,就可以转到下面的编辑区域
w指窗口切换
选中窗口后, ":q"退出当前的窗口.

6. 对比两个文件
vi -d aaa.c bbb.c
有点类似diff

7. vim运行控制脚本,例子:
root@yutao-desktop:/# cat /etc/vim/vimrc
if v:lang =~ "utf8$" || v:lang =~ "UTF-8$"
   
set fileencodings=utf-8,latin1
endif

set nocompatible        " Use Vim defaults (much better!)
set bs=2                " allow backspacing over everything in insert mode
"
set ai                 " always set autoindenting on
"set backup             " keep a backup file
set viminfo='20,"50    " read/write a .viminfo file, don't store more
                        
" than 50 lines of registers
set history=50          " keep 50 lines of command line history
set ruler               " show the cursor position all the time

" Only do this part when compiled with support for autocommands
if has("autocmd")
  
" In text files, always limit the width of text to 78 characters
  autocmd BufRead *.txt set tw=78
  
" When editing a file, always jump to the last cursor position
  autocmd BufReadPost *
   
if line("'""> 0 && line ("'""<= line("$"|
     exe 
"normal g'"" |
   endif
endif

if has("cscope")
   
set csprg=/usr/bin/cscope
   
set csto=0
   
set cst
   
set nocsverb
   
" add any database in current directory
   if filereadable("cscope.out")
      cs add cscope.
out
   
" else add database pointed to by environment
   elseif $CSCOPE_DB != ""
      cs add $CSCOPE_DB
   endif
   
set csverb
endif

" Switch syntax highlighting on, when the terminal has colors
"
 Also switch on highlighting the last used search pattern.
if &t_Co > 2 || has("gui_running")
  syntax on
  
set hlsearch
endif

if &term=="xterm"
     
set t_Co=8
     
set t_Sb=dm
     
set t_Sf=dm
endif

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值