您的位置:首页 > 博客中心 > 网络系统 >

ubuntu下定制Vim/Gvim及使用技巧

时间:2022-04-03 11:23

vim是linux下的编辑器之神,是玩linux的必备工具,同样emacs是神的编辑器,两个编辑器是各有千秋,看个人的喜好,青菜萝卜各有所爱。我是比较喜欢vim,用vim编写bash,perl,python脚本,以及修改配置文件等等,本文就主要介绍在下gvim环境的搭建,希望对大家有所帮助。

. Ubuntu安装gvim方法

安装gvim的方法,最简单方法是在Ubuntu软件中心搜索gvim,然后点击安装即可,如下图:

技术分享如果使用命令行可以打开终端,输入sudo apt-get install vim-gtk vim-doc cscope

安装gvim常用插件

    安装好gvim后就可以使用了,不过在用gvim编写代码的时候还是有点小不方便,比如代码的颜色高亮,自动补全,自动添加相关函数注释,工程管理等等。这些都不必担忧,gvim有很多的插件可以满足上面的功能,甚至更多,下面我安装的是我常用的一些插件。

Gvim插件可以到下面的网址自行选择喜欢的插件: 

taglist.vim插件:该插件是查看源代码的结构等,支持C/C++,java,php等多种语言。

安装方法:

下载taglist.zip,解压到$HOME/.vim目录中;

进入$home/.vim/doc目录下,运行vim,输入:help tags,启用taglist的帮助文档;

重启gvim,运行:TlistToggle启用taglist

具体步骤:

 创建$HOME/.vim目录

  1. -pc:~$ mkdir .vim 

下载并解压到.vim目录中
  1. -pc:~$ cd .vim/ 
  2. -pc:~/.vim$ unzip /home/test/下载/taglist_45.zip 
  3. Archive: /home/test/下载/taglist_45.zip 
  4. inflating: plugin/taglist.vim 
  5. inflating: doc/taglist.txt 

进入到doc目录,并运行vim

  1. -pc:~/.vim$ cd doc/ 
  2.  
  3. -pc:~/.vim/doc$ vim  

 

 

supertab插件:

安装步骤:

1. 下载该插件,下载地址:

2.用vim打开supertab.vba文件,并使之生效:so %,如图:

技术分享

 

技术分享

技术分享

perl-support.vim插件:

 

安装方法:

下载插件,下载地址:

将perl-support.zip解压到$HOME/.vim目录下

  1. -pc:~/.vim$ unzip /home/linuxpc/下载/perl-support.zip 
  2. Archive: /home/linuxpc/下载/perl-support.zip 
  3. inflating: doc/perlsupport.txt 
  4. inflating: README.perlsupport 
  5. creating: ftplugin/ 
  6. inflating: ftplugin/pod.vim 
  7. inflating: ftplugin/perl.vim 
  8. creating: autoload/ 
  9. inflating: autoload/perlsupportregex.vim 
  10. inflating: autoload/perlsupportgui.vim 
  11. inflating: autoload/perlsupportprofiling.vim 
  12. inflating: plugin/perl-support.vim 
  13. creating: perl-support/ 
  14. creating: perl-support/modules/ 
  15. inflating: perl-support/modules/perl-modules.list 
  16. creating: perl-support/templates/ 
  17. inflating: perl-support/templates/Templates 
  18. inflating: perl-support/templates/idioms.template  

技术分享

 

技术分享

bash-support.vim插件

与perl-support类似的一款插件,bash-support.vim插件,这两个插件的功能类似

 

安装步骤:

下载插件,并解压到$HOME/.vim目录中

下载地址:

 

解压到目录下:

  1. -pc:~/.vim$ unzip /home/waydeechen/下载/bash-support.zip 
  2. Archive: /home/waydeechen/下载/bash-support.zip 
  3. inflating: README.bashsupport 
  4. inflating: doc/bashsupport.txt 
  5. inflating: ftplugin/sh.vim 
  6. creating: bash-support/ 
  7. creating: bash-support/templates/ 
  8. inflating: bash-support/templates/Templates 
  9. inflating: bash-support/templates/bash.paramsub.template 
  10. inflating: bash-support/templates/bash.statements.template 
  11. inflating: bash-support/templates/bash.comments.template 
  12. creating: bash-support/codesnippets/ 
  13. inflating: bash-support/codesnippets/timestamp  

技术分享

 

技术分享

AutoComplPop插件

AutoComplPop可以打开自动补全的窗口,不单单可以补全变量函数等,还可以补全文件名。

下载地址:

安装方法很简单,将下载的文件解压到$HOME/.vim目录中

使用效果图:

技术分享

最后晒下自己的.vimrc文件:

  1. set backspace=indent,eol,start 
  2. set history=50 " keep 50 lines of command line history 
  3. set ruler " show the cursor position all the time 
  4. set showcmd " display incomplete commands 
  5. set incsearch " do incremental searching 
  6. set number 
  7. syntax on “打开语法高亮 
  8. set autoindent “自动缩进 
  9. set smartindent “使用c形式自动缩进 
  10. set cindent 
  11. set tabstop=4 “TAB键的宽度 
  12. set shiftwidth=4 “统一缩进为4 
  13. set showmatch “高亮显示匹配括号 
  14. set incsearch 
  15. set nu “显示行号 
  16. color darkblue ”颜色方案 
  17. set ruler 
  18. set ignorecase “搜索忽略大小写 
  19. set smartcase 
  20. set hlsearch ”设置搜索字符高亮 
  21. set report=0 
  22. set mouse=a 
  23. set nobackup 
  24. set noeb 
  25. set history=1000 
  26. set nobackup 
  27. set noswapfile 
  28. set enc=utf-8 ”语言编码设置 
  29. set fencs=utf-8,ucs-bom,shift-jis,gb18030,gbk,gb2312,cp936 
  30. set langmenu=ZH_CN.UTF-8 
  31. set helplang=cn 
  32. set laststatus=2 
  33. set ruler “显示光标位置状态行 
  34. set cmdheight=1 ”设置命令行的高度 
  35. filetype on “侦测文件类型 
  36. filetype plugin on ”载入文件类型插件 
  37. filetype indent on 
  38. set viminfo+=! “保存全局变量 
  39. set iskeyword+=_,$,@,%,#,- ”带有这些字符的单词不要被换行分割 
  40. set linespace=0 “字符间插入像素行数目 
  41. set wildmenu ”命令行自动完成操作 
  42. set backspace=2 “使用回格键 
  43. set whichwrap+=<,>,h,l ”允许backspace和光标跨越行 
  44. set selection=exclusive 
  45. set selectmode=mouse,key 

vim常用使用方法:

:e:打开文件,例::e /opt/shell/status.sh

ZZ: 关闭文件

vim +18 /opt/status.sh :打开status.sh并跳到第18行 

  1. vim +18 /opt/status.sh :打开status.sh并跳到第18行 
  2.  
  3. 13 
  4. 14 ########## DEFINE FUNCTIONS ########## 
  5. 15 
  6. 16 function in_range_random_number() 
  7. 17 { 
  8. 18 #create a random number which is less than or equal to UPPER_LIMIT 
  9. 19 RANDOM_NUMBER=$(( $RANDOM % $UPPER_LIMIT + 1 )) 
  10. 20 echo -e "$RANDOM_NUMBER" 
  11. 21 } 
  1. 14 ########## DEFINE FUNCTIONS ########## 
  2. 15 
  3. 16 function in_range_random_number() 
  4. 17 { 
  5. 18 #create a random number which is less than or equal to UPPER_LIMIT 
  6. 19 RANDOM_NUMBER=$(( $RANDOM % $UPPER_LIMIT + 1 )) 
  7. 20 echo -e "$RANDOM_NUMBER" 
  8. 21 } 
  9. 22 

 

vim +/RANDOM_NUMBER /opt/shell/random_passwd_create.sh:打开脚本并从头开始查询,到第一个匹配模式的地方

 

vim +?RANDOM_NUMBER /opt/shell/random_passwd_create.sh 从文件尾搜索

  1. vim +/^# /opt/shell/random_passwd_create.sh 
  2. 160 
  3. 161 ########## start of main ########## 
  4. 162 
  5. 163 trap `trap_exit;exit 2` 1 2 3 15 
  6. 164 
  7. 165 check_for_and_create_keyboard_file  

7. 将文件部分写入另一个文件

  1. :5,15w /tmp/hshhs 

8. 删除空行

  1. :g/^$/d 

9. vimdiff 命令

技术分享

 

10.vimgrep命令

技术分享

更多相关信息见 专题页面 

本类排行

今日推荐

热门手游