我正在使用Vim编写Python代码,每次我想运行我的代码时,我都会在Vim中输入:
:w !python
Run Code Online (Sandbox Code Playgroud)
这令人沮丧,所以我一直在寻找一种更快的方法来在Vim中运行Python代码.从终端执行Python脚本可能吗?我正在使用Linux.
Ken*_*ent 116
如何添加一个autocmd,何时FileType python创建一个映射:
nnoremap <buffer> <F9> :exec '!python' shellescape(@%, 1)<cr>
Run Code Online (Sandbox Code Playgroud)
然后你可以按下<F9>执行当前缓冲区python
cza*_*yas 25
我在.vimrc文件中有这个:
imap <F5> <Esc>:w<CR>:!clear;python %<CR>
Run Code Online (Sandbox Code Playgroud)
当我完成编辑Python脚本时,我只需按下<F5>.保存脚本,然后在空白屏幕中执行.
Fla*_*che 18
按下键进入正常模式,<esc>然后键入:
! clear; python %
Run Code Online (Sandbox Code Playgroud)
分步说明:
!允许您运行终端命令
clear将清空您的终端屏幕
;结束第一个命令,允许您引入第二个命令
python将使用python运行您的脚本(ruby例如可以替换它 )
%汇编当前文件名,将其作为参数传递给python命令
Foc*_*olf 15
我更喜欢将Python输出重定向到新的Vim窗口(如果该窗口保持打开状态,则在下次使用此函数执行Python代码时更新其内容):
" Bind F5 to save file if modified and execute python script in a buffer.
nnoremap <silent> <F5> :call SaveAndExecutePython()<CR>
vnoremap <silent> <F5> :<C-u>call SaveAndExecutePython()<CR>
function! SaveAndExecutePython()
" SOURCE [reusable window]: https://github.com/fatih/vim-go/blob/master/autoload/go/ui.vim
" save and reload current file
silent execute "update | edit"
" get file path of current file
let s:current_buffer_file_path = expand("%")
let s:output_buffer_name = "Python"
let s:output_buffer_filetype = "output"
" reuse existing buffer window if it exists otherwise create a new one
if !exists("s:buf_nr") || !bufexists(s:buf_nr)
silent execute 'botright new ' . s:output_buffer_name
let s:buf_nr = bufnr('%')
elseif bufwinnr(s:buf_nr) == -1
silent execute 'botright new'
silent execute s:buf_nr . 'buffer'
elseif bufwinnr(s:buf_nr) != bufwinnr('%')
silent execute bufwinnr(s:buf_nr) . 'wincmd w'
endif
silent execute "setlocal filetype=" . s:output_buffer_filetype
setlocal bufhidden=delete
setlocal buftype=nofile
setlocal noswapfile
setlocal nobuflisted
setlocal winfixheight
setlocal cursorline " make it easy to distinguish
setlocal nonumber
setlocal norelativenumber
setlocal showbreak=""
" clear the buffer
setlocal noreadonly
setlocal modifiable
%delete _
" add the console output
silent execute ".!python " . shellescape(s:current_buffer_file_path, 1)
" resize window to content length
" Note: This is annoying because if you print a lot of lines then your code buffer is forced to a height of one line every time you run this function.
" However without this line the buffer starts off as a default size and if you resize the buffer then it keeps that custom size after repeated runs of this function.
" But if you close the output buffer then it returns to using the default size when its recreated
"execute 'resize' . line('$')
" make the buffer non modifiable
setlocal readonly
setlocal nomodifiable
endfunction
Run Code Online (Sandbox Code Playgroud)
插件:jupyter-vim
因此,您可以将行 ( <leader>E)、视觉选择 ( <leader>e) 发送到正在运行的jupyter-client(替换ipython)
我更喜欢将编辑器和解释器分开(每个都在它的外壳中)。想象一下你发送了一个错误的输入读取命令......
基于先前的答案,如果您希望在查看代码输出时看到代码,则可以找到:ter(:terminal)命令。
autocmd Filetype python nnoremap <buffer> <F5> :w<CR>:ter python2 "%"<CR>
autocmd Filetype python nnoremap <buffer> <F6> :w<CR>:vert ter python3 "%"<CR>
Run Code Online (Sandbox Code Playgroud)
vert在第二行中使用时,代码以垂直分割而不是水平分割运行。
不利的是,如果不关闭代码运行所在的拆分窗口,则在多次运行后您将有很多拆分(在原始python IDLE中不会发生,重复使用同一输出窗口)。
(我将这些行留在里面/home/user/.vimrc)
请记住,您可以使用来重复上一次使用的命令@:,因此您只需要重复这两个字符即可。
或者,您可以将字符串保存w !python到一个寄存器中("a例如),然后单击:<C-R>a<CR>以将寄存器的内容插入a命令行并运行它。
或者你可以做我想做的,并映射<leader>z到:!python %<CR>运行当前文件。
如果您不想:exec python file.py每次都看到“”打印,请使用以下命令:
nnoremap <F9> :echo system('python2 "' . expand('%') . '"')<cr>
nnoremap <F10> :echo system('python3 "' . expand('%') . '"')<cr>
Run Code Online (Sandbox Code Playgroud)
它也没有弄乱我的电力线/ vim-airline 状态栏。
考虑使用 shebang line,这样您就可以将它与任何语言一起使用,而不仅仅是 Python。
添加舍邦:
将其添加到脚本的第一行:
#!/usr/bin/env python3
Run Code Online (Sandbox Code Playgroud)
或者,如果您使用的是 Python 2:
#!/usr/bin/env python2
Run Code Online (Sandbox Code Playgroud)
Vim 键盘映射:
将其添加到您的~/.vimrc:
nmap <F7> :w<cr>:!clear;"%:p"<cr>
Run Code Online (Sandbox Code Playgroud)
使文件可执行:
在 Vim 中输入:
:!chmod +x %
Run Code Online (Sandbox Code Playgroud)
或在终端中:
chmod +x script_name.py
Run Code Online (Sandbox Code Playgroud)
解释:
当在正常模式下按 F7 时,Vim 会尝试将当前文件作为 bash 脚本执行。然后 bash 解释器将看到 shebang 行并理解该文件应该传递给 Python(或任何其他程序,如果需要)。
您还可以使用脚本名称从终端运行脚本:
./script_name.py
Run Code Online (Sandbox Code Playgroud)
而不是这种方式(这也可以):
python3 script_name.py
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
91924 次 |
| 最近记录: |