3

askubuntu.com で、 Vim で python を実行する方法を教えてもらいました。python を使用して非線形方程式を解く際に、コード 1 番目または 2 番目のコード python コードで動作するセットアップをテストしています。

私が別のことをした唯一のことは、最後の行として print(a) を追加したことです。昨日これをシェルから実行したところ、完全に機能しました。誰かが何がうまくいかないのか教えてもらえますか?

わかりましたので、適切な疑問符でvimrcを修正しました。

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

次に、vimから実行しました

:Shell ./#

しかし、同じ構文エラーを再び受け取りました。(実行する .py ファイルを取得できないため、ファイルを .sh として保存する必要がありますか?)

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
4

1 に答える 1

2

これは Python の問題である可能性があります。

一方で、スクリプトを実行し、出力を 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

于 2013-03-28T04:00:45.817 に答える