【问题标题】:running python on vim在 vim 上运行 python
【发布时间】:2013-03-28 00:39:23
【问题描述】:

在 askubuntu.com 上,有人告诉我如何使用 run python on Vim。我正在测试与using python to solve a non linear equation 的代码 1st 或 2nd 代码 python 代码一起使用的设置。

我唯一不同的是添加了 print(a) 作为最后一行。我昨天从 shell 运行了这个,它运行良好。有人可以告诉我出了什么问题吗?

好的,所以我用适当的问号更正了 vimrc,

chmod +x ~/path/to/file/hw6problem2.py

然后我从 vim 跑了

:Shell ./#

但我再次收到相同的语法错误。 (文件是否必须保存为 .sh,因为我无法运行任何 .py 文件?)

dustin@dustin:~$ vim /home/dustin/Documents/School/UVM/Engineering/OrbitalMechanics/hw6problem2.py

File "hw6problem2.py", line 14
a0 = max(s/2, (s - c)/2)
 ^
SyntaxError: invalid syntax

shell returned 1

Press ENTER or type command to continue

vimrc

syntax on
au BufWinLeave * mkview "records settings
au BufWinEnter * silent loadview "reloads settings


set nu "puts line numbers on
set ic "case insensitive
set foldmethod=syntax "for the latex-suite
set autoread "autoload when files in the buffer have been modified
set autochdir "autochange directory


"set wrap
set wrap
" set lines=50 columns=80

" resizes window
:map g1 :set lines=20<CR>:set columns=80<CR>
:map g2 :set lines=50<CR>:set columns=80<CR>
:map g3 :set lines=50<CR>:set columns=170<CR>
:map <F6> :! firefox % &<CR>
:map E Ea

"set autoindent

set tabstop=4
set shiftwidth=2
set expandtab
set smartindent
"
" Stuff for latex-suite 

" REQUIRED. This makes vim invoke Latex-Suite when you open a tex file.
" It also allows you to set different actions for different filetypes
" in ~/.vim/after/ftplugin/*.vim
filetype plugin on

set shellslash

" IMPORTANT: grep will sometimes skip displaying the file name if you
" search in a singe file. This will confuse Latex-Suite. Set your grep
" program to always generate a file-name.
set grepprg=grep\ -nH\ $*

" OPTIONAL: This enables automatic indentation as you type.
filetype indent on

" OPTIONAL: Starting with Vim 7, the filetype of empty .tex files defaults to
" 'plaintex' instead of 'tex', which results in vim-latex not being loaded.
" The following changes the default filetype back to 'tex':
let g:tex_flavor='latex'
let g:Tex_ViewRule_pdf = 'okular'


"""""""""""""""""""""""""""""""""""""""
" => Shell command
"""""""""""""""""""""""""""""""""""""""

command! -complete=shellcmd -nargs=+ Shell call s:RunShellCommand(<q-args>)

function! s:RunShellCommand(cmdline)
let isfirst = 1
let words = []
for word in split(a:cmdline)
if isfirst
  let isfirst = 0  " don't change first word (shell command)
 else
  if word[0] =~ '\v[%#<]'
    let word = expand(word)
  endif
  let word = shellescape(word, 1)
endif
call add(words, word)
endfor
let expanded_cmdline = join(words)
rightbelow new
setlocal buftype=nofile bufhidden=wipe nobuflisted noswapfile nowrap
call setline(1, 'You entered:  ' . a:cmdline)
call setline(2, 'Expanded to:  ' . expanded_cmdline)
call append(line('$'), substitute(getline(2), '.', '=', 'g'))
silent execute '$read !'. expanded_cmdline
1
endfunction

【问题讨论】:

  • 你能在第 14 行发布一些代码吗?
  • 这可能与 Vim 无关。一行 Python 也没有什么问题。请发布整个 Python 文件(或者如果它很大,请链接到它的要点),或者更多上下文。我怀疑你只是在引入一个错字之类的东西。
  • @PavelAnossov 代码在第二个链接
  • @JimStewart 代码在第二个链接
  • 链接中的代码是第 13 行,而不是第 14 行。

标签: vim python-2.7 numpy ubuntu-12.04 ipython


【解决方案1】:

这很可能是 python 问题。

另一方面,有一个很棒的 shell 函数用于执行脚本并将输出重定向到 vim 缓冲区(拆分窗口)。

执行当前脚本的语法(你应该有 chmod +x 和 shebang 行):

:Shell ./#

要添加功能,请将其添加到.vimrc

command! -complete=shellcmd -nargs=+ Shell call s:RunShellCommand(<q-args>)

function! s:RunShellCommand(cmdline)
  let isfirst = 1
  let words = []
  for word in split(a:cmdline)
    if isfirst
      let isfirst = 0  " don't change first word (shell command)
    else
      if word[0] =~ '\v[%#<]'
        let word = expand(word)
      endif
      let word = shellescape(word, 1)
    endif
    call add(words, word)
  endfor
  let expanded_cmdline = join(words)
  rightbelow new
  setlocal buftype=nofile bufhidden=wipe nobuflisted noswapfile nowrap
  call setline(1, 'You entered:  ' . a:cmdline)
  call setline(2, 'Expanded to:  ' . expanded_cmdline)
  call append(line('$'), substitute(getline(2), '.', '=', 'g'))
  silent execute '$read !'. expanded_cmdline
  1
endfunction

https://github.com/ruslanosipov/dotfiles/blob/master/.vimrc#L137

【讨论】:

  • 我将以上内容添加到我的 vimrc 中,然后使用 #!/usr/bin/env python 的 shebang 行运行代码,但收到以下错误:处理“* 的 BufWinEnter 自动命令时检测到错误” ": E32: 无文件名按 ENTER 或键入命令继续
  • bin/env 之间没有空格:#!/usr/bin/env python。尝试做:Shell ./%
  • 那是一个错字。现在它说在处理“*”的 BufWinEnter 自动命令时检测到错误:E32:处理函数 7_RunShellCommand 时检测到没有文件名错误:第 20 行:E499:“%”或“#”的空文件名,仅适用于":p:h": $read !./% 按 ENTER 或输入命令继续
  • @dustin 你确定.vimrc 中没有其他条目干扰工作流程吗?
  • @dustin,.vimrc 的第 2 行和第 3 行,将* 替换为?*,然后再次点击:Shell ./#ubuntuforums.org/showthread.php?t=1639591
猜你喜欢
  • 2019-05-05
  • 1970-01-01
  • 2013-07-31
  • 1970-01-01
  • 2014-09-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-20
相关资源
最近更新 更多