用 Vim 打造輕量級 python IDE

A
12 min readApr 13, 2021

--

Vim?先上個google到的簡介:

Vim (Vi IMproved)是從 vi發展出來的一個文字編輯器。代碼補完、編譯及錯誤跳轉等方便編程的功能特別豐富,在程式設計師中被廣泛使用。和Emacs並列成為類Unix系統使用者最喜歡的編輯器。

簡而言之就是很方便、靈巧、便捷、快速、彈性高的文字介面編輯器啦!
詳細的使用方法請自行 google,只要學起來就覺得自己很潮?
這邊也貼上使用 vim 常用到的快捷鍵小抄

VIM
1. 普通模式快捷鍵
1-1. 跳到
w 後一個字開頭
e 後一個字結尾
b 前一個字開頭
shft -> 後一個字開頭
shft <- 前一個字開頭
$ 行尾
0 or ^ 行頭
shft 0 下個段落
shft 9 上個段落
gg 最開頭
G 最尾部
1-2. 輸入
i 從這個字元
a 從下個字元
I 從行頭
A 從行尾
o 向下插入一行
O 向上插入一行

1-3. 操作
前綴: 數字
後綴: w字, $尾, 0首, ^首, G檔尾, gg檔首
eg. 5dd, dw, d$, d0, d^, dG, egg

dd 刪除一行
x 刪除字元
X 向前...
yy 複製一行
p 貼至游標後
P 貼至游標前
~ 改變大小寫
. 重複上一動
u undo
ctrl r redo
shft 8 搜尋
shft >> 縮排
shft << 縮進
> 重複上個縮排縮進

ctrl w 上 跳到上方分割視窗
ctrl w 下 跳到下方…
ctrl w H 把當前分割視窗移到最右
ctrl w L …最左
1-4. 命令行
:w 儲存
:x 儲存離開
:q 直接離開
:qa! 不儲存離開
:num 跳到第num行
:15 d 刪除第15行
:15 copy 16 第15行複製到第16行
/char 往後尋找char
?char 往前尋找
%s/word_a/word_b/g 取代a->b
%s/word_a/word_b/gc 經使用者確認
:nohl 取消highlisht
:split 垂直分割視窗
:vsplit 水平分割視窗

1-5. 插件
\cc 一鍵註釋
2\cc 註釋兩行
\ci 切換註釋狀態
:NERDTree 開啟樹狀資料夾
ctrl p 搜尋檔案
1-6. 秘笈
“ + y 複製到系統剪貼簿

以下是筆者為了備齊IDE功能自行安裝的套件們

Plugin 'Valloric/YouCompleteMe'
Plugin 'Lokaltog/vim-powerline'
Plugin 'scrooloose/nerdtree'
Plugin 'jiangmiao/auto-pairs'
Plugin 'Yggdroot/indentLine'
Plugin 'tell-k/vim-autopep8'
Plugin 'scrooloose/nerdcommenter'
Plugin 'scrooloose/syntastic'
Plugin 'nvie/vim-flake8'
Plugin 'jistr/vim-nerdtree-tabs'
Plugin 'kien/ctrlp.vim'

但怎麼安裝呢?安裝其他套件前要先安裝Vundle :

Vundle 是個可以管理 Vim 套件的工具。只需要輸入套件的名字、儲存,Vundle 就會自動安裝,可以很方便的管理。

安裝方法也請自行 google 摟,總之就是要在 .vimrc 的設定檔裡面輸入下面的代碼,然後存檔後輸入vim +PluginInstall或是進到vim輸入
:PluginInstall 記得:不能不見哦!

在註解裡面(“後面)都有功能解說

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()
" alternatively, pass a path where Vundle should install plugins
"call vundle#begin('~/some/path/here')
" let Vundle manage Vundle, requiredPlugin 'VundleVim/Vundle.vim'call vundle#end() " required
filetype plugin indent on " required

接下來要安裝其他套件時,只要把其他套件輸入到粗體那行下面,然後一樣存檔後輸入vim +PluginInstall 就可以了。

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()
" alternatively, pass a path where Vundle should install plugins
"call vundle#begin('~/some/path/here')
" let Vundle manage Vundle, requiredPlugin 'VundleVim/Vundle.vim'

Plugin 'Valloric/YouCompleteMe'
Plugin 'Lokaltog/vim-powerline'
Plugin 'scrooloose/nerdtree'
Plugin 'jiangmiao/auto-pairs'
Plugin 'Yggdroot/indentLine'
Plugin 'tell-k/vim-autopep8'
Plugin 'scrooloose/nerdcommenter'
Plugin 'scrooloose/syntastic'
Plugin 'nvie/vim-flake8'
Plugin 'jistr/vim-nerdtree-tabs'
Plugin 'kien/ctrlp.vim'
call vundle#end() " required
filetype plugin indent on " required

安裝YouCompleteMe的過程可能會有一些問題,搜尋他的github解決

先上我的整個.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()
" alternatively, pass a path where Vundle should install plugins
"call vundle#begin('~/some/path/here')
" let Vundle manage Vundle, required
Plugin 'VundleVim/Vundle.vim'
Plugin 'Valloric/YouCompleteMe'
Plugin 'Lokaltog/vim-powerline'
Plugin 'scrooloose/nerdtree'
Plugin 'jiangmiao/auto-pairs'
Plugin 'Yggdroot/indentLine'
Plugin 'tell-k/vim-autopep8'
Plugin 'scrooloose/nerdcommenter'
Plugin 'scrooloose/syntastic'
Plugin 'nvie/vim-flake8'
Plugin 'jistr/vim-nerdtree-tabs'
Plugin 'kien/ctrlp.vim'
call vundle#end() " required
filetype plugin indent on " required
" YouCompleteMe
"默认配置文件路径"
let g:ycm_global_ycm_extra_conf = '~/.ycm_extra_conf.py'
"打开vim时不再询问是否加载ycm_extra_conf.py配置"
let g:ycm_confirm_extra_conf=0
set completeopt=longest,menu
"python解释器路径"
let g:ycm_path_to_python_interpreter='/usr/local/bin/python3'
"是否开启语义补全"
let g:ycm_seed_identifiers_with_syntax=1
"是否在注释中也开启补全"
let g:ycm_complete_in_comments=1
let g:ycm_collect_identifiers_from_comments_and_strings = 0
"开始补全的字符数"
let g:ycm_min_num_of_chars_for_completion=2
"补全后自动关机预览窗口"
let g:ycm_autoclose_preview_window_after_completion=1
" 禁止缓存匹配项,每次都重新生成匹配项"
let g:ycm_cache_omnifunc=0
"字符串中也开启补全"
let g:ycm_complete_in_strings = 1
"离开插入模式后自动关闭预览窗口"
autocmd InsertLeave * if pumvisible() == 0|pclose|endif
let g:ycm_autoclose_preview_window_after_completion=1
map <leader>g :YcmCompleter GoToDefinitionElseDeclaration<CR>
highlight PMenu ctermfg=0 ctermbg=242 guifg=black guibg=darkgrey
highlight PMenuSel ctermfg=242 ctermbg=8 guifg=darkgrey guibg=black
"NERDTree配置F2开启和关闭树"
let NERDTreeChDirMode=1
nnoremap <silent> <F2> :NERDTree<CR>
"显示书签"
let NERDTreeShowBookmarks=1
"设置忽略文件类型"
let NERDTreeIgnore=['\~$', '\.pyc$', '\.swp$']
"窗口大小"
let NERDTreeWinSize=25
"autopep8设置"
"缩进指示线"
let g:indentLine_char='┆'
let g:indentLine_enabled = 1
let g:autopep8_disable_show_diff=1
"普通設置“
"去掉vi的一致性"
set nocompatible
"显示行号"
set number
" 隐藏滚动条"
set guioptions-=r
set guioptions-=L
set guioptions-=b
"隐藏顶部标签栏"
set showtabline=0
"设置字体"
set guifont=Monaco:h13
syntax on "开启语法高亮"
filetype on
let g:solarized_termcolors=256 "solarized主题设置在终端下的设置"
set background=dark "设置背景色"
set nowrap "设置不折行"
set fileformat=unix "设置以unix的格式保存文件"
set tabstop=4 "设置table长度"
set shiftwidth=4 "同上"
set showmatch "显示匹配的括号"
set scrolloff=5 "距离顶部和底部5行"
set cindent
set laststatus=2 "命令行为两行"
set fenc=utf-8 "文件编码"
set backspace=2
set mouse=a "启用鼠标"
set selection=exclusive
set selectmode=mouse,key
set matchtime=5
set ignorecase "忽略大小写"
set incsearch
set hlsearch "高亮搜索项"
set noexpandtab "不允许扩展table"
set whichwrap+=<,>,h,l
set autoread
set cursorline "突出显示当前行"
set cursorcolumn "突出显示当前列"
set ai
filetype indent on"設定頁面分割“
set splitbelow
set splitright
"split navigations
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>
" Enable folding
set foldmethod=indent
set foldlevel=99
" Enable folding with the spacebar
nnoremap <space> za
set encoding=utf-8
set backspace=indent,eol,start
" For C++
nnoremap <C-F5> <Esc>:w<CR>:!g++ -std=c++11 % -o /tmp/a.out && /tmp/a.out<CR>
nnoremap <F7> <Esc>:w<CR>:!g++ -std=c++11 %<CR>
nnoremap <C-R> <Esc>:w<CR>:!g++ -std=c++11 -g % -o /tmp/a.out && gdb /tmp/a.out<CR>

--

--

A
0 Followers

重度自學成癮者。國樂團指揮/笙樂手/機器學習/深度學習