2013-03-28 70 views
3

在askubuntu.com上我被告知如何run python on Vim。我正在測試可以在using python to solve a non linear equation處使用代碼第1或第2代碼python代碼的設置。在vim上運行python

我唯一做的不同是在最後一行添加了print(a)。我昨天從外殼跑出來,它運行得很好。有人能告訴我發生了什麼問題嗎?

好了,所以我糾正與適當的問號vimrc的,

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

然後從VIM我跑

:Shell ./# 

,但我又收到了同樣的語法錯誤。 (是否有文件被保存爲.SH,因爲我不能得到任何.py文件來運行?)

[email protected]:~$ 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 
+0

你可以在第14行發佈一些代碼嗎? – 2013-03-28 00:54:04

+1

這可能與Vim無關。 Python的這一行也沒有錯。請張貼整個Python文件(或鏈接到它的大小,如果它很大)或更多的上下文。我懷疑你只是介紹一個錯字或什麼的。 – 2013-03-28 00:54:36

+1

@PavelAnossov代碼在第二個鏈接 – dustin 2013-03-28 00:59:30

回答

2

這很可能是一個Python的問題。

另一方面,執行腳本和將輸出重定向到vim緩衝區(分割窗口)有很好的shell函數。

語法執行當前腳本(你應該使用chmod + x和家當線):

: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

+0

我將上面的代碼添加到我的vimrc中,然後用#!/ usr/bin/env python的shebang行運行代碼,但收到以下錯誤:處理BufWinEnter時檢測到錯誤「*」的自動命令: E32:名稱 按ENTER鍵或鍵入命令繼續 – dustin 2013-03-28 04:17:51

+0

'bin /'和'env':'#!/ usr/bin/env python'之間沒有空格。試着做':Shell ./%' – 2013-03-28 04:23:11

+0

這是一個錯字。現在它說檢測到「*」,而處理BufWinEnter自動命令的錯誤: E32:沒有文件名檢測 錯誤,同時處理功能 7_RunShellCommand: 線20: E499:爲「%」或「#」空文件名而已,使用「:p:h」:$ read!./% 按ENTER或鍵入命令繼續 – dustin 2013-03-28 04:25:28