vim编辑器安装及配置

文章部分内容转自《使用vim打造自己的python编辑器》https://www.cnblogs.com/linxiyue/p/7834817.html 感谢

参考文章:https://zhuanlan.zhihu.com/p/68111471

一、安装

    yum -y install vim*

    git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim

    如果~/.vim/bundle目录不存在,则新建目录:

    cd ~

    mkdir .vim

    cd .vim

    mkdir bundle

二、新建vim配置文件

    vim的配置是在用户主目录下的~/.vimrc文件中完成的,如果没有的话,需要自己新建一下:

    cd ~

    touch .vimrc

    vim .vimrc

三、配置.vimrc

Vim 常用技巧:

将回车由默认的8个空格改为4个空格:

命令:set sw=4

修改tab为4空格:

命令:set ts=4

设置每一级的缩进长度:

命令:set shiftwidth=4

设置文件的编码:

set fileencoding=utf-8

set  nocompatible "关闭与vi的兼容模式

set number "显示行号

set nowrap    "不自动折行

set showmatch    "显示匹配的括号

set scrolloff=3        "距离顶部和底部3行"

set encoding=utf-8  "编码

set   fenc=utf-8      "编码

set mouse-=a        "启用鼠标  -=a 右键可进行黏贴处理等

set hlsearch        "搜索高亮

syntax on    "语法高亮


为py文件添加下支持pep8风格的配置:

au BufNewFile,BufRead *.py

\ settabstop=4   "tab宽度

\ setsofttabstop=4 

\ setshiftwidth=4  

\ settextwidth=79  "行最大宽度

\ setexpandtab       "tab替换为空格键

\ setautoindent      "自动缩进

\ setfileformat=unix   "保存文件格式


分割窗口

vim在编辑的时候就可以打开多个文件:

:vs  或者 :vsplit  将当前窗口竖直分割,并在上面新窗口中显示当前文件

:vs filename 将当前窗口竖直分割,新文件在新窗口中显示

:sp 或者:sv或者:split  将当前窗口水平分割,并在左边新窗口中显示当前文件

:sp filename 将当前窗口竖直分割,新文件在左边新窗口中显示

:new 新建文件并竖直分割

:vnew 新建文件并水平分割

如果想让新窗口在右边或者下方打开,添加配置:

set splitbelow

set splitright

在窗口之间切换可以用鼠标,如果不想用鼠标,切换按键如下:

Ctrl-w-j 切换到下方的分割窗口

Ctrl-w-k 切换到上方的分割窗口

Ctrl-w-l 切换到右侧的分割窗口

Ctrl-w-h 切换到左侧的分割窗口

觉得三个按键多的话可以设置快捷键:

nnoremap <C-J> <C-W><C-J>

nnoremap <C-K> <C-W><C-K>

nnoremap <C-L> <C-W><C-L>

nnoremap <C-H> <C-W><C-H>

这样就不用按w键了。

代码折叠

当代码行数很多的时候,代码折叠是很必须的:

set foldmethod=indent

set foldlevel=99

使用zc按键来创建折叠,使用za来打开或者关闭折叠。

za经常会误输入,可以用空格键来替代za:

nnoremap <space> za

一键执行python代码

如果想直接在vim中执行python代码,可以添加(来自https://www.zhihu.com/question/20271508):

map <F5> :call RunPython()<CR>

func! RunPython()

    exec "W"

    if &filetype == 'python'

        exec "!time python2.7 %"

    endif

endfunc

这样,按F5键python代码就可以自动执行了

插件

vim插件中最主要的就是vundle了,vundle用来管理vim的其它插件

Vundle

Vundle 是 Vim bundle 的简称,使用git来管理vim插件,有了它,安装其它插件就方便很多。

项目地址https://github.com/VundleVim/Vundle.vim。

首先下载源码:

git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim

如果~/.vim/bundle目录不存在,则新建目录:

cd ~

mkdir .vim

cd .vim

mkdir bundle

然后将下列配置放在.vimrc文件的开头:

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()

" let Vundle manage Vundle, required

Plugin 'VundleVim/Vundle.vim'

" All of your Plugins must be added before the following line

call vundle#end()            " required

filetype plugin indent on    " required

如果想下载某个插件,比如自动缩进indentpython.vim插件,需要将

Plugin 'vim-scripts/indentpython.vim'

置于call vundle#begin()和call vundle#end()之间,保存配置后在vim中执行

:PluginInstall

即可以自动下载indentpython.vim插件了。

bundle可以管理下载几种不同的插件,方式如下:

github上的插件

Plugin 'tpope/vim-fugitive'

来自于http://vim-scripts.org/vim/scripts.html的插件

Plugin 'L9'

非github上的git插件

Plugin 'git://git.wincent.com/command-t.git'

本地插件

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/'}

有旧插件的情况下,下载新的插件并重命名以避免冲突

Plugin 'ascenator/L9', {'name': 'newL9'}

下载方式除了在vim中运行:PluginInstall外,还可以在命令行中运行:

vim +PluginInstall +qall

其它常用的命令:

: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


常用插件:

10 Plugin 'VundleVim/Vundle.vim'

11 Plugin 'vim-scripts/indentpython.vim'          "自动缩进"

12 Plugin 'vim-syntastic/syntastic'                "语法检查"

13 Plugin 'altercation/vim-colors-solarized'      "配色方案"

14 Plugin 'scrooloose/nerdtree'            "树形目录"

15 Plugin 'Lokaltog/vim-powerline'        "状态栏"

16 Plugin 'Yggdroot/indentLine'            "缩进指示线 开关 :IndentLinesToggle"

17 Plugin 'jiangmiao/auto-pairs'          "自动补全括号和引号等"

18 Plugin 'kien/ctrlp.vim'                "搜索插件,在vim normal模式下,按下ctrl+p,然后输入你要寻找的文件就行了"

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容