首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

使用 Vim 建立 Python 及Markdown编辑环境

1、安装 python

Ubuntu 18.04 默认安装 python。

2、安装 VIM

Ubuntu 18.04 Desktop初始安装自带了vi,未带 vim 需要安装,已安装的略过。

sudo apt install vim

3、配置 vim

编辑 ~/.vimrc 文件,填入以下内容:

" 开启文件类型侦测

filetype on

" 根据侦测到的不同类型加载对应的插件

filetype plugin on

" 开启语法高亮功能

syntax enable

" 允许用指定语法高亮配色方案替换默认方案

syntax on

" 开启实时搜索功能

set incsearch

" 关闭兼容模式(去掉vi一致性, 可以使用更多vim功能)

set nocompatible

" vim 自身命令行模式智能补全

set wildmenu

" 总是显示状态栏

set laststatus=2

" 显示光标当前位置

set ruler

" 开启行号显示

set number

" 高亮显示当前行/列

"set cursorline

"set cursorcolumn

"增量式搜索

set incsearch

" 高亮显示搜索结果

set hlsearch

"文件自动检测外部更改

set autoread

"tab缩进

"制表符占4个空格

set tabstop=4

"编辑模式输入 Tab 时,插入字符数(可能是 Tab 和空格的混合宽度,

"比如 "tabstop=4,softtabstop=10,那么插入 Tab 时会将光标移动 10 个字符,

"可能会是两个 tab 加两个空格,这对 backspace 也有效。)

set softtabstop=4

"默认缩进4个空格大小

set shiftwidth=4

"用空格键替换制表符

"set expandtab

"在行首输入 Tab 时插入宽度为 shiftwidth 的空白,

"在其他地方按 tabstop 和 softtabstop 处理

"解决 shiftwidth 和 tabstop 不等时的问题

set smarttab

"c文件自动缩进

set cindent

"自动对齐

set autoindent

""智能缩进

set smartindent

"配色方案

colorscheme elflord

"浅色显示当前行

"autocmd InsertLeave * se nocul

"用浅色高亮当前行

"autocmd InsertEnter * se cul

au BufNewFile,BufRead *.py

set tabstop=4 |

set softtabstop=4 |

set shiftwidth=4 |

set textwidth=79 |

set expandtab |

set autoindent |

set fileformat=unix

"au BufRead,BufNewFile *.py,*.pyw,*.c,*.h match Error /s+$/

"设置中文编码

set fileencodings=utf-8,ucs-bom,gb2312,gbk,gb18030,cp936

set termencoding=utf-8

set fileformats=unix

set encoding=utf-8

4、安装 vim 插件管理器 Vundle

需要安装git,如果没安装的要安装。

mkdir -p ~/.vim/vundle/

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

5、配置 vundle

在~/.vimrc 中添加如下内容

set nocompatible " 去除VI一致性,必须

filetype off " 必须

" 设置包括vundle和初始化相关的runtime path

set rtp+=~/.vim/bundle/Vundle.vim

call vundle#begin()

" 另一种选择, 指定一个vundle安装插件的路径

"call vundle#begin('~/some/path/here')

" 让vundle管理插件版本,必须

Plugin 'VundleVim/Vundle.vim'

" 以下范例用来支持不同格式的插件安装.

" 请将安装插件的命令放在vundle#begin和vundle#end之间.

" 你的所有插件需要在下面这行之前

call vundle#end() " 必须

filetype plugin indent on " 必须 加载vim自带和插件相应的语法和文件类型相关脚本

" 忽视插件改变缩进,可以使用以下替代:

"filetype plugin on

" 简要帮助文档

" :PluginList - 列出所有已配置的插件

" :PluginInstall - 安装插件,追加 `!` 用以更新或使用 :PluginUpdate

" :PluginSearch foo - 搜索 foo ; 追加 `!` 清除本地缓存

" :PluginClean - 清除未使用插件,需要确认; 追加 `!` 自动批准移除未使用插件

"

" 查阅 :h vundle 获取更多细节和wiki以及FAQ

" 将你自己对非插件片段放在这行之后

6、使用 vundle 安装插件方法

把要安装的插件以合法格式添加到 ~/.vimrc 文件中,要装的插件命令放在vundle#begin和vundle#end之间。

然后使用 : :PluginInstall 命令安装插件

.vimrc 中具体安装插件的命令格式如下:

Github上的插件

格式为: Plugin '用户名/插件仓库名',如:

Plugin 'tpope/vim-fugitive'

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

格式为:

Plugin '插件名称' 实际上是:

Plugin 'vim-scripts/插件仓库名' 只是此处的用户名可以省略

如:Plugin 'L9'

由Git支持但不再github上的插件仓库 格式为:

Plugin 'git clone 后面的地址'

如:

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

本地的Git仓库(例如自己的插件) 格式为:

Plugin 'file:///+本地插件仓库绝对路径',

如:

Plugin 'file:///home/gmarik/path/to/plugin'

插件在仓库的子目录中.

正确指定路径用以设置runtimepath. 以下范例插件在sparkup/vim目录下

Plugin 'rstacruz/sparkup', {'rtp': 'vim/'}

如:安装L9,如果已经安装过这个插件,可利用以下格式避免命名冲突

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

7、使用 vundle 清除插件

在~/.vimrc 文件中清除对应的 Plugin 项

在 vim 中执行 :PluginClean

8、安装自动补全插件

需要 cmake、g++、make python3-devel (build-essential cmake python3-dev)

在 ~/.vimrc 中添加:(添加到 vundle#begin和vundle#end之间.)

Plugin 'Valloric/YouCompleteMe'

vim 中执行: :PluginInstall

:PluginInstall

时间有点长,等待下载完成后,编译 YouCompleteMe。

命令行执行:其中 --clang-completer 可选。

(如果系统安装两个Python版本,可以修改install.py使用的Python版本)

cd ~/.vim/bundle/YouCompleteMe

./install.py --clang-completer

编译时间也较长,请耐心等待。

在~/.vimrc 文件中加入如下配置

" YouCompleteMe配置

let g:SimpylFold_docstring_preview=1

let g:ycm_min_num_of_chars_for_completion=2 "开始补全的字符数"

let g:ycm_python_binary_path='python3' "jedi模块所在python解释器路径"

let g:ycm_seed_identifiers_with_syntax=1 "开启使用语言的一些关键字查询"

let g:ycm_autoclose_preview_window_after_completion=1 "补全后自动关闭预览窗>

9、安装 Python 自动补全插件 jedi-vim

安装 jedi-vim 插件实现Python自动补全

更新 jedi

pip3 install jedi

安装jedi-vim

cd ~/.vim/bundle/

git clone --recursive https://github.com/davidhalter/jedi-vim.git ~/.vim/bundle/jedi-vim

添加如下语句到~/.vimrc

Plugin 'davidhalter/jedi-vim'

vim 中执行 :PluginInstall

10、安装python pep8自动格式化插件

首先安装autopep8:

pip3 install autopep8

或者

pip install autopep8

安装 vim-autopep8 插件

~/.vimrc 在 添加:

Plugin 'tell-k/vim-autopep8'

vim 中执行 :PluginInstall

11、安装 python flake8-vim 插件

pip3 install flake8

在 ~/.vimrc 中添加:

Plugin 'andviro/flake8-vim'

let python_highlight_all=1

vim 中执行 :PluginInstall

12、安装 vimpdb

pip3 install vimpdb

在 ~/.vimrc 中添加

Plugin 'gotcha/vimpdb'

13、安装 bufexplorer 插件

vim-scripts/bufexplorer.vim

下载 bufexplorer.zip

解压 bufexplorer.zip

复制 bufexplorer.vim 到 /usr/share/vim/vim80/plugin

复制 bufexplorer.txt 到 /usr/share/vim/vim80/doc

14、安装 ctags、cscope 和 taglist 插件

安装 ctags 和 cscope

sudo apt install ctags cscope

````

taglist 插件

在 ~/.vimrc 中添加:

```vim

Plugin 'vim-scripts/taglist.vim'

vim 中执行: :PluginInstall

:PluginInstall

15、安装插件

编辑 ~/.vimrc 文件,在vundle#begin和vundle#end之间,加入如下内容:

"自动缩进插件

Plugin 'vim-scripts/indentpython.vim'

"PEP8 缩进

Plugin 'Vimjas/vim-python-pep8-indent'

"缩进指示线 indentLine

Plugin 'Yggdroot/indentLine'

"自动补全括号,引号等:auto-pairs

Plugin 'jiangmiao/auto-pairs'

"git 集成插件 vim-fugitive

Plugin 'tpope/vim-fugitive'

"目录树(文件管理)

Plugin 'scrooloose/nerdtree'

"状态栏

Plugin 'vim-airline/vim-airline'

"ctrlP 模糊查找

Plugin 'kien/ctrlp.vim'

"每次保存文件时Vim都会检查代码的语法

Plugin 'scrooloose/syntastic'

"语法高亮,文档查阅,语法检查,自动修正错误和重构等功能

Plugin 'python-mode/python-mode'

let g:pymode_python = 'python3'

" 插入模式、正常模式输入法自动切换插件

Plugin 'lilydjwg/fcitx.vim'

"python 自动补全插件(使用Tab)

Plugin 'rkulla/pydiction'

let g:pydiction_location = '~/.vim/bundle/pydiction/complete-dict'

"vim-vebugger 调试插件

Plugin 'idanarye/vim-vebugger'

在 vim 中使用 :PluginInstall

或者命令行使用 vim +PluginInstall +qall 安装以上插件。

16、安装 Markdown 插件

安装依赖软件和库

pip3 install markdown

sudo npm -g install instant-markdown-d

sudo npm -g install mdmath

"Markdown 插件(语法高亮)

Plugin 'gabrielelana/vim-markdown'

"为 Markdown 生成 TOC 的 Vim 插件

Plugin 'mzlogin/vim-markdown-toc'

"Markdown 公式支持

Plugin 'iamcco/mathjax-support-for-mkdp'

"Markdown 预览 依赖 markdown

Plugin 'iamcco/markdown-preview.vim'

"pandoc vim 插件

Plugin 'vim-pandoc/vim-pandoc'

"实时预览的插件 vim-instant-markdown 需要 Node.js 和 instant-makrdown-d

Plugin 'suan/vim-instant-markdown'

在 vim 中使用 :PluginInstall

17、目录插件:vim-makrdown-toc 用法记录:

插件 vim-markdown-toc 使用方法

生成目录

将光标移动到想在后面插入目录的那一行,然后运行下面的某个命令:

:GenTocGFM

生成 GFM 链接风格的目录。

适用于 GitHub 仓库里的 Markdown 文件,比如 README.md,也适用用于生成 GitBook 的 Markdown 文件。

:GenTocRedcarpet

生成 Redcarpet 链接风格的目录。

适用于使用 Redcarpet 作为 Markdown 引擎的 Jekyll 项目或其它地方。

:GenTocGitLab

生成 GitLab link 风格的目录.

:GenTocMarked

生成 iamcco/markdown-preview.vim 风格的目录

更新已存在的目录

通常不需要手动做这件事,保存文件时会自动更新已经存在的目录。

除非是在配置里关闭了保存时自动更新,并且维持插入目录 前后的 ,此时可使用。

:UpdateToc 命令手动更新。

删除目录

:RemoveToc 命令可以帮你删除本插件生成的目录

  • 发表于:
  • 原文链接https://kuaibao.qq.com/s/20181211A1JBGS00?refer=cp_1026
  • 腾讯「腾讯云开发者社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。
  • 如有侵权,请联系 cloudcommunity@tencent.com 删除。

扫码

添加站长 进交流群

领取专属 10元无门槛券

私享最新 技术干货

扫码加入开发者社群
领券