Programing

Vi (m)에서 편집중인 파일을 실행하는 방법

lottogame 2020. 8. 28. 07:50
반응형

Vi (m)에서 편집중인 파일을 실행하는 방법


Vi (m)에서 편집중인 파일을 실행하고 SciTE와 같이 분할 창에서 출력을 얻는 방법은 무엇입니까?

물론 다음과 같이 실행할 수 있습니다.

:!scriptname

그러나 스크립트 이름을 작성하지 않고 화면 하단 대신 분할 창에서 출력을 얻는 방법을 피할 수 있습니까?


make명령은. makeprg옵션에 설정된 명령을 실행합니다 . %현재 파일 이름에 대한 자리 표시 자로 사용 합니다. 예를 들어, Python 스크립트를 편집하는 경우 :

:set makeprg=python\ %

네, 공간을 탈출해야합니다. 그 후에 간단히 실행할 수 있습니다.

:make

원하는 경우 autowrite옵션을 설정할 수 있으며 다음 을 실행하기 전에 자동으로 저장됩니다 makeprg.

:set autowrite

이것은 실행 부분을 해결합니다. 파일로의 리디렉션을 포함하지 않는 분할 창으로 출력을 가져 오는 방법을 모릅니다.


현재 버퍼의 파일 이름에 액세스하려면 %. 변수로 가져 오려면 expand()함수를 사용할 수 있습니다 . 새 버퍼로 새 창을 열려면 :new또는을 사용하십시오 :vnew. 명령의 출력을 현재 버퍼로 파이프하려면을 사용하십시오 :.!. 함께 모아서:

:let f=expand("%")|vnew|execute '.!ruby "' . f . '"'

분명히 ruby원하는 명령으로 대체하십시오 . execute파일 이름을 따옴표로 묶을 수 있도록 사용 했으므로 파일 이름에 공백이 있으면 작동합니다.


Vim에는 !VIM 창에서 직접 쉘 명령을 실행하는 ( "bang") 명령이 있습니다. 또한 파이프와 연결된 명령 시퀀스를 시작하고 stdout을 읽을 수 있습니다.

예를 들면 :

! node %

명령 프롬프트 창을 열고 명령을 실행하는 것과 같습니다.

cd my_current_directory 
node my_current_file

자세한 내용은 "Vim 팁 : 외부 명령 작업" 을 참조하십시오.


내 vimrc에 바로 가기가 있습니다.

nmap <F6> :w<CR>:silent !chmod 755 %<CR>:silent !./% > .tmp.xyz<CR>
     \ :tabnew<CR>:r .tmp.xyz<CR>:silent !rm .tmp.xyz<CR>:redraw!<CR>

이렇게하면 현재 버퍼를 쓰고 현재 파일을 실행 가능하게 만들고 (unix 만 해당) 실행 (unix 만 해당)하고 출력을 .tmp.xyz로 리디렉션 한 다음 새 탭을 만들고 파일을 읽은 다음 삭제합니다.

분석 :

:w<CR>                             write current buffer
:silent !chmod 755 %<CR>           make file executable
:silent !./% > .tmp.xyz<CR>        execute file, redirect output
:tabnew<CR>                        new tab
:r .tmp.xyz<CR>                    read file in new tab
:silent !rm .tmp.xyz<CR>           remove file
:redraw!<CR>                       in terminal mode, vim get scrambled
                                   this fixes it

내가 사용한 쉘 스크립트의 경우

:set makeprg=%

:make

맵을 통해 약간 더 침입적인 메커니즘을 사용합니다.

map ;e :w<CR>:exe ":!python " . getreg("%") . "" <CR>

저장하지 않아도되게 만든 다음 가십시오. 바로 가기.


vim의 플러그인 bexec을 사용할 수 있습니다 . 내가 아는 한 최신 버전은 0.5입니다.

그때:

$ mkdir -p ~/.vim/plugin
$ mv bexec-0.5.vba ~/.vim/plugin
$ vim ~/.vim/plugin/bexec-0.5.vba

.vba 파일을 편집하는 동안 vim 자체에서 다음을 수행합니다.

:so %

bexec.vim 이 문서와 같이 작성 되었음을 알리는 일부 출력이 표시됩니다 .

Now, you can test it by opening your (whatever language script that has an #! interpreter working properly) in vim and run

:Bexec 

Note: I wanted the split to be vertical rather than horizontal, so I did:

$ grep -i -n split ~/.vim/plugin/bexec.vim | grep -i hor
102:    let bexec_splitdir = "hor" " hor|ver
261:        exec {"ver":"vsp", "hor":"sp"}[g:bexec_splitdir]

and changed the value of from "hor" to "ver"..

I know it's an old question, but I hope this can help someone out there. I have been running in the same issue while taking Coursera's Startup Engineering course where professor Palaji uses Emacs and I don't like Emacs..


Vim 8 has an interactive terminal built in. To run the current bash script in a split pane:

:terminal bash %

or for short

:ter bash %

% expands to the current file name.

From :help terminal:

The terminal feature is optional, use this to check if your Vim has it:
    echo has('terminal')
If the result is "1" you have it.

Based on @SethKriticos and @Cyril answers I now use the following:

function! Setup_ExecNDisplay()
  execute "w"
  execute "silent !chmod +x %:p"
  let n=expand('%:t')
  execute "silent !%:p 2>&1 | tee ~/.vim/output_".n
  " I prefer vsplit
  "execute "split ~/.vim/output_".n
  execute "vsplit ~/.vim/output_".n
  execute "redraw!"
  set autoread
endfunction

function! ExecNDisplay()
  execute "w"
  let n=expand('%:t')
  execute "silent !%:p 2>&1 | tee ~/.vim/output_".n
  " I use set autoread
  "execute "1 . 'wincmd e'"
endfunction

:nmap <F9> :call Setup_ExecNDisplay()<CR>
:nmap <F2> :call ExecNDisplay()<CR>

Use F9 to setup the new window and F2 to execute your script and tee to your output file.

I also added the script name to the output file name, so that you can use this for multiple scripts at the same time.


In your .vimrc you can paste this function

function! s:ExecuteInShell(command)
  let command = join(map(split(a:command), 'expand(v:val)'))
  let winnr = bufwinnr('^' . command . '$')
  silent! execute ':w'
  silent! execute  winnr < 0 ? 'vnew ' . fnameescape(command) : winnr . 'wincmd w'
  setlocal buftype=nowrite bufhidden=wipe nobuflisted noswapfile nowrap number
  silent! execute 'silent %!'. command
  silent! redraw
  silent! execute 'au BufUnload <buffer> execute bufwinnr(' . bufnr('#') . ') . ''wincmd w'''
  silent! execute 'nnoremap <silent> <buffer> <LocalLeader>r :call <SID>ExecuteInShell(''' . command . ''')<CR>'
  silent! execute 'wincmd w'
  " echo 'Shell command ' . command . ' executed.'
endfunction
command! -complete=shellcmd -nargs=+ Shell call s:ExecuteInShell(<q-args>)
cabbrev shell Shell

After that, in vim run command :shell python ~/p.py as example. And you will get the output in splitted window. + After changes in p.py as example you will run the same command again, this function will not create new window again, it will display the result in the previous(same) splitted window.


@xorpaul

I was looking for this script (python/Windows) for quite some time. As there is no "tee" in Windows I changed it to:

function! Setup_ExecNDisplay()
  execute "w"
  let n=expand('%:t')
  execute "silent ! python % > d:\\temp\\output_".n ." 2>&1"
  execute "vsplit d:\\temp\\output_".n
  execute "redraw!"
  set autoread
endfunction

function! ExecNDisplay()
  execute "w"
  let n=expand('%:t')
  execute "silent ! python % > d:\\temp\\output_".n . " 2>&1"
endfunction

:nmap <F9> :call Setup_ExecNDisplay()<CR>
:nmap <F2> :call ExecNDisplay()<CR>

참고URL : https://stackoverflow.com/questions/953398/how-to-execute-file-im-editing-in-vim

반응형