REPL: 读取-求值-输出”循环(英语:Read-Eval-Print Loop,简称REPL)是一个简单的,交互式的编程环境。
python作为一个动态语言,REPL在开发过程中起到了很大的作用。
可是一直以来用vim写python没有一个很好的,简便的提供repl功能。也有类似的插件,比如vim-slime,但是它已经很久没有更新并且使用起来需要tmux非常的复杂。
最近发现vim8提供了terminal功能和通讯方式。我使用这个功能实现了一个vim的repl插件。
sillybun/vim-replgithub.com
插件运行环境要求
- MacOS, Windows, Linux
- vim +terminal, +timers
- vim最好有+python3或者+python支持
插件的功能
这个插件的功能是在vim中打开一个terminal,然后在文件中可以通过快捷键把选中的文本扔给terminal中进行执行。
通过按<leader>r打开一个REPL窗口⬇️:
在普通模式下按<leader>w把当前行发送到REPL窗口⬇️:
在普通模式下在代码块的第一行按<leader>w,把一块代码发送到REPL窗口⬇️:
在选择模式下选中多行代码按<leader>w把一块代码发送到REPL窗口⬇️:
在python程序中进行代码调试⬇️:
可以这样设置:
autocmd Filetype python nnoremap <F12> <Esc>:REPLDebugStopAtCurrentLine<Cr>
autocmd Filetype python nnoremap <F10> <Esc>:REPLPDBN<Cr>
autocmd Filetype python nnoremap <F11> <Esc>:REPLPDBS<Cr>
如此后快捷键的作用为:
- F12: 在当前行设置断点并运行
- F10: 运行一行(不进入函数)
- F10: 运行一行(进入函数)
安装方式:
Plugin 'sillybun/vim-repl'
推荐配置:
Plug 'sillybun/vim-repl'
let g:repl_program = {'python': 'ipython','default': 'zsh','r': 'R','lua': 'lua',}
let g:repl_predefine_python = {'numpy': 'import numpy as np','matplotlib': 'from matplotlib import pyplot as plt'}
let g:repl_cursor_down = 1
let g:repl_python_automerge = 1
let g:repl_ipython_version = '7'
nnoremap <leader>r :REPLToggle<Cr>
autocmd Filetype python nnoremap <F12> <Esc>:REPLDebugStopAtCurrentLine<Cr>
autocmd Filetype python nnoremap <F10> <Esc>:REPLPDBN<Cr>
autocmd Filetype python nnoremap <F11> <Esc>:REPLPDBS<Cr>
let g:repl_position = 3
可选参数,等号后面是默认值(None表示没有缺省值):
let g:repl_width = None "窗口宽度
let g:repl_height = None "窗口高度
let g:sendtorepl_invoke_key = "<leader>w" "传送代码快捷键,默认为<leader>w
let g:repl_position = 0 "0表示出现在下方,1表示出现在上方,2在左边,3在右边
let g:repl_stayatrepl_when_open = 0 "打开REPL时是回到原文件(1)还是停留在REPL窗口中(0)
推荐配置:
tnoremap <C-h> <C-w><C-h>
tnoremap <C-j> <C-w><C-j>
tnoremap <C-k> <C-w><C-k>
tnoremap <C-l> <C-w><C-l>