基础配置

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

cd ~
touch .vimrc

首先做些简单的配置:

set nocompatible "关闭与vi的兼容模式
set number "显示行号
set nowrap    "不自动折行
set showmatch    "显示匹配的括号
set scrolloff=3        "距离顶部和底部3行"
set encoding=utf-8  "编码
set fenc=utf-8      "编码
set mouse=a        "启用鼠标
set hlsearch        "搜索高亮
syntax on    "语法高亮

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

au BufNewFile,BufRead *.py
\ set tabstop=4   "tab宽度
\ set softtabstop=4
\ set shiftwidth=4
\ set textwidth=79  "行最大宽度
\ set expandtab       "tab替换为空格键
\ set autoindent      "自动缩进
\ set fileformat=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

YouCompleteMe

非常好用的自动补全插件,就是比较重。

官网地址:http://valloric.github.io/YouCompleteMe/

github地址:https://github.com/Valloric/YouCompleteMe

YouCompleteMe安装后还需要手动编译,然后再在.vimrc中配置。

在ubuntu中使用,首先准备一些工具:

sudo apt-get install build-essential cmake

sudo apt-get install python-dev python3-dev

使用vundle安装:

Plugin 'Valloric/YouCompleteMe'

编译:

cd ~/.vim/bundle/YouCompleteMe
./install.py --clang-completer

参数 --clang-completer是为了加上C系列语言的自动补全,也可以不加:

cd ~/.vim/bundle/YouCompleteMe
./install.py

耐心等待吧,要花很长时间...

复制一下默认配置文件到用户主目录:

cp third_party/ycmd/examples/.ycm_extra_conf.py ~/

YCM常用的一些选项,可根据个人喜好调整:

let g:ycm_min_num_of_chars_for_completion = 2  "开始补全的字符数"
let g:ycm_python_binary_path = 'python'  "jedi模块所在python解释器路径"
let g:ycm_seed_identifiers_with_syntax = 1  "开启使用语言的一些关键字查询"
let g:ycm_autoclose_preview_window_after_completion=1 "补全后自动关闭预览窗口"

代码跳转:

nnoremap <leader>jd :YcmCompleter GoToDefinitionElseDeclaration<CR>

开关YCM:

let g:ycm_auto_trigger = 0   "turn off
let g:ycm_auto_trigger = 1   "turn on

支持vim8的补全插件

YouCompleteMe实际上是使用jedi-vim来补全python代码的,如果觉得YCM实在太重,可以使用支持vim8的maralla/completor.vim来补全代码:

下载:

Plugin 'maralla/completor.vim'

下载jedi:

pip install jedi

配置:

let g:completor_python_binary = '/path/to/python/with/jedi/installed'

设置起来比YCM简单很多了。

自动缩进插件

写python代码,自动缩进是必须的,可以使用indentpython.vim插件:

Plugin 'vim-scripts/indentpython.vim'

语法检查

安装syntastic插件,每次保存文件时Vim都会检查代码的语法:

Plugin 'vim-syntastic/syntastic'

添加flake8代码风格检查:

Plugin 'nvie/vim-flake8'

运行F7就可以进行flake8检查了。

配色方案

solarized配色方案已经流行很久了,github地址https://github.com/altercation/vim-colors-solarized。

手动下载:

$ cd ~/.vim/bundle
$ git clone git://github.com/altercation/vim-colors-solarized.git
$ mv vim-colors-solarized ~/.vim/bundle/

或者vundle下载:

Plugin 'altercation/vim-colors-solarized'

solarized有dark和light两种配色,配置:

syntax enable
set background=light or dark
colorscheme solarized

也可以根据gui模式和终端模式进行切换:

if has('gui_running')set background=light
elseset background=dark
endif

另外一种配色Zenburn方案:

Plugin 'jnurmine/Zenburn'

两种配色切换:

if has('gui_running')set background=darkcolorscheme solarized
elsecolorscheme Zenburn
endif

nerdtree

给vim添加一个树形目录,地址https://github.com/scrooloose/nerdtree。

下载:

Plugin 'scrooloose/nerdtree'

添加开关树形目录的快捷键:

map <C-n> :NERDTreeToggle<CR>

Ctrl+n就可以开启目录了。

设置忽略.pyc文件:

let NERDTreeIgnore=['\~$', '\.pyc$', '\.swp$']

为nerdtree添加git支持:

Plugin 'Xuyuanp/nerdtree-git-plugin'

如果你想用tab键:

Plugin 'jistr/vim-nerdtree-tabs'

vim-powerline

美化状态栏,可以显示当前的虚拟环境、Git分支、正在编辑的文件等信息。

Plugin 'Lokaltog/vim-powerline'

indentLine

缩进指示线,地址https://github.com/Yggdroot/indentLine。

安装:

Plugin 'Yggdroot/indentLine'

python是靠代码缩进来判断代码块的,缩进指示线还是很方便的。

vim-autopep8

自动格式化工具,安装后运行:Autopep8就可以自动依照pep8的标准自动格式化代码。

地址https://github.com/Yggdroot/indentLine。

首先安装autopep8:

$ pip install autopep8

Plugin 'tell-k/vim-autopep8'

可以设置快捷键F8代替:Autopep8:

autocmd FileType python noremap <buffer> <F8> :call Autopep8()<CR>

auto-pairs

自动补全括号和引号等,地址https://github.com/jiangmiao/auto-pairs。

Plugin 'jiangmiao/auto-pairs'

ctrlp.vim

搜索插件,在vim normal模式下,按下ctrl+p,然后输入你要寻找的文件就行了。

地址https://github.com/kien/ctrlp.vim。

Plugin 'kien/ctrlp.vim'

ag.vim

搜索引擎使用了the_silver_searcher

apt-get install silversearcher-ag
or
brew install the_silver_searcher

插件

Plugin 'rking/ag.vim'

使用

:Ag [options] {pattern} [{directory}]

vim-fugitive

git集成插件,可以在vim中运行git命令,https://github.com/tpope/vim-fugitive。

Plugin 'tpope/vim-fugitive'

  

使用vim打造自己的python编辑器相关推荐

  1. python编辑器vim下载_vim做python编辑器

    vim使用 tab设定: set tabstop=4 set softtabstop=4 set shiftwidth=4 other setting: set number | set nonumb ...

  2. python在线编辑器-在线python编辑器

    广告关闭 2017年12月,云+社区对外发布,从最开始的技术博客到现在拥有多个社区产品.未来,我们一起乘风破浪,创造无限可能. windows下python的ide选择:http:blog.csdn. ...

  3. 在线python编辑器-在线python编辑器

    广告关闭 2017年12月,云+社区对外发布,从最开始的技术博客到现在拥有多个社区产品.未来,我们一起乘风破浪,创造无限可能. windows下python的ide选择:http:blog.csdn. ...

  4. vim配置python开发环境_GitHub - TTWShell/legolas-vim: Vim配置,为python、go开发者打造的IDE。...

    legolas-vim 个人vim配置.支持python.go等自动提示,支持python.go的函数跳转(python支持虚拟环境). 最终效果图(函数列表的feature已移除,因为大项目会导致性 ...

  5. 保姆级教程!将 Vim 打造一个 IDE (Python 篇)

    从上周开始我就开始折腾 ,搞了一下 Vim IDE for Python & Go,我将整个搭建的过程整理成本篇文章分享出来,本篇是 Python 版本的保姆级教程,实际上我还写了 Go 版本 ...

  6. 用Tkinter打造自己的Python IDE开发工具(1)建立一个简单的代码编辑器

    用Tkinter打造自己的Python IDE开发工具(1)建立一个简单的代码编辑器 很多Python初学者,安装完Python后,已经包含了IDLE代码开发和调试系统.都会再安装各种Python开发 ...

  7. 用Tkinter打造自己的Python IDE开发工具(4)利用HP_tk模块设计自己的代码编辑器

    用Tkinter打造自己的Python IDE开发工具(4)利用HP_tk模块设计自己的代码编辑器 HP_tk.py模块是小白量化第二代量化系统中的开发模块.其中HP_tk.py模块是小白量化系统GU ...

  8. 用Tkinter打造自己的Python IDE开发工具(5)利用HP_tka模块设计自己的中文代码编辑器

    用Tkinter打造自己的Python IDE开发工具(5)利用HP_tka模块设计自己的中文代码编辑器 前面我们介绍了在Tkinter中使用exec()函数运行用户程序的方法.exec()采用多线程 ...

  9. 把vim打造成牛逼的C++ IDE

    把vim打造成牛逼的C++ IDE 对于windows上做C++开发的用户来说,vs为他们做了大量的工作,语法高亮.自动缩进.智能提示等等,当你happy的使用vs时,请记住一点,这是一个收费软件,虽 ...

最新文章

  1. Android AIDL示例-回调方法版
  2. 将项目导入eclipse中出现的jsp页面报错
  3. 【入门4】数组 (今天刷洛谷了嘛)
  4. 马云:不能把孩子放在温室里,光给孩子知识是不够的
  5. [ZT]Addison Wesley's Free Chapters and Articles
  6. 后端要学MySQL_后端程序员需要掌握MySQL数据库吗
  7. 广数系统加工中心编程_数控加工中心编程技巧一文通
  8. java big5转换为gbk_简体繁体转换代码(Big5-GB | GBK简体-GBK繁体)
  9. 基于机智云物联网平台的智能花盆2.0,实现了智能灌溉与自动补光
  10. 【offer谈判】如何做一朵盛世白莲花,让企业愿意为你花大价钱!
  11. excel 批量生成条码
  12. 最全的静态网站生成器(开源项目)
  13. 机器人运动控制(上)
  14. 爬虫:Scrapy分类爬取苏宁易购书城
  15. CDMA sid, nid, bid 含义解释
  16. HTML+CSS(精灵图)实现微信滑动门技术
  17. java alter session_java程序里面可以执行alter session之类的语句?
  18. 计算机语言的发展历程和发展趋势
  19. 将bilibili里面的缓存视频保存到电脑(b站mp4格式下载)
  20. 软件黑科技工具之代码生成器!(提高团队效率、加强团队管理、减少研发成本)

热门文章

  1. 【Linux驱动】ThinkPad笔记本wifi模块rtl8821ce在ubuntu16.04的驱动(默认没有)
  2. java 实体类包含list 怎么取值_2019 最新 500 道 Java 面试题
  3. python刷新页面_小伙利用Python制作浏览器,网友点评这小伙将来要进腾讯
  4. html css 显示数值_【CSS纯技术】20.03.05-CSS渲染的原理
  5. HashSet中的add()方法( 三 )(详尽版)
  6. flutter报错Could not connect to lockdownd, error code -
  7. CentOS 安装Apache
  8. 使用 fastlane 实现 iOS 持续集成(二)
  9. iOS图片精确提取主色调算法iOS-Palette(附源码)
  10. Spring MVC中DispatcherServlet理解总结(1)