vim - 在 vim 上运行 python

标签 vim python-2.7 numpy ubuntu-12.04 ipython

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

我唯一不同的是在最后一行添加了 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

最佳答案

这可能是 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

关于vim - 在 vim 上运行 python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15672250/

相关文章:

Vim:反向视觉模式透视

c++ - 我应该如何与使用 Xcode 的开发人员协作?

python - Python 中 set() 的类型是什么?

python - 如何在 numpy 数组中将非常小的数字设置为零?

python-2.7 - 两个输入图像都必须在函数错误中具有 CV_8UC1

Vim:多种模式的重载映射

vim - 禁用VIM中的警告?

multithreading - 当 child 被杀死时,防止线程 subprocess.popen 终止我的主脚本?

python - 如何使用 Python Selenium 在文本框(输入)中定位和插入值?

python-3.x - 使用 Xarray 中的多个变量沿特定轴在坐标对上应用函数