直接在vim / mvim / gvim中执行脚本

时间:2010-07-02 14:06:13

标签: vim vi macvim

TextMate有一个很好的功能,允许您从当前上下文中执行脚本,并在单独的窗口中显示输出。这使您可以随时编写和测试代码。我几乎可以肯定MacVim / gVIM有类似的功能,但我不确定它是什么。目前我将缓冲区保存到磁盘,然后转到命令行并在这方面执行脚本。如何使用vim改进该工作流程?

9 个答案:

答案 0 :(得分:68)

您可以使用!命令在vim中执行此操作。例如,要计算当前文件中的单词数,您可以执行以下操作:

:! wc %

%被当前文件名替换。要运行脚本,您可以在文件上调用解释器 - 例如,如果您正在编写perl脚本:

:! perl %

答案 1 :(得分:16)

vim教程:Mapping keys in Vim

您可以映射键,以便perl执行上面jts建议的当前脚本。

map <C-p> :w<CR>:!perl %<CR>

将Ctrl + P映射到写入文件并通过perl

运行
imap <C-p> <Esc>:w<CR>:!perl %<CR>

允许您在插入模式下调用它。

你的vim / home文件夹中应该有.vimrc(_vimrc for Windows)文件。它有关于vim应该如何表现的说明。

map <C-p> :w<CR>:!perl %<CR>只是将Ctrl + p映射到:

的说明

a)写入当前文件:w

b)使用%(当前打开的文件)作为参数:!perl %

运行命令(perl) 每个命令代表&#34;回车&#34;:执行特定命令的指令后

<CR>。 imap与map相同,但在插入模式下侦听Ctrl + p。

答案 2 :(得分:6)

你可以直接从vim运行它:

:!./script.sh

答案 3 :(得分:6)

保存文件并使用解释器调用脚本

例如:

:!python %

答案 4 :(得分:4)

听起来你正在寻找!

  

:!{cmd}使用shell执行{cmd}

如果需要将%传递给脚本,可以使用!proofread-script % 来表示当前文件名:

!

您还可以将!{motion}{filter} " from normal mode :{range}!{filter} " from command mode 与范围一起使用,以将该命令用作过滤器:

:.,.+2!

(在第一种情况下,与许多其他命令一样,当您键入动作时,它会将您带入命令模式,将动作转换为范围,例如!!{cmd}

最后,如果你实际上不需要传递文件中的输入,但想要文件中的输出,那实际上是一个简单的过滤器,最快的方法是{{1}}。这将使用命令的输出替换当前行。

答案 5 :(得分:3)

this small snippet放入.vimrc中,用一次击键(如F5)执行当前文件,并将结果显示在新的拆分窗格缓冲区中。

:!没问题,但您需要切换到终端才能看到结果。

虽然您可以使用ctrl-z执行此操作并将vim带回fg,但仍然意味着您需要大量切换上下文。

这个snippet的工作方式是首先根据filetype猜测可执行文件,然后以当前文件作为参数运行它。

接下来,handy utility method获取输出并将其转储到新缓冲区中。

它并不完美,但对于常见的工作流程来说非常快。

这是以下复制的代码段:

""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"""""""""""""""""""""""""" RUN CURRENT FILE """""""""""""""""""""""""""""
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Execute current file
nnoremap <F5> :call ExecuteFile()<CR>

" Will attempt to execute the current file based on the `&filetype`
" You need to manually map the filetypes you use most commonly to the
" correct shell command.
function! ExecuteFile()
  let filetype_to_command = {
  \   'javascript': 'node',
  \   'coffee': 'coffee',
  \   'python': 'python',
  \   'html': 'open',
  \   'sh': 'sh'
  \ }
  let cmd = get(filetype_to_command, &filetype, &filetype)
  call RunShellCommand(cmd." %s")
endfunction

" Enter any shell command and have the output appear in a new buffer
" For example, to word count the current file:
"
"   :Shell wc %s
"
" Thanks to: http://vim.wikia.com/wiki/Display_output_of_shell_commands_in_new_window
command! -complete=shellcmd -nargs=+ Shell call RunShellCommand(<q-args>)
function! RunShellCommand(cmdline)
  echo a:cmdline
  let expanded_cmdline = a:cmdline
  for part in split(a:cmdline, ' ')
     if part[0] =~ '\v[%#<]'
        let expanded_part = fnameescape(expand(part))
        let expanded_cmdline = substitute(expanded_cmdline, part, expanded_part, '')
     endif
  endfor
  botright new
  setlocal buftype=nofile bufhidden=wipe nobuflisted noswapfile nowrap
  call setline(1, 'You entered:    ' . a:cmdline)
  call setline(2, 'Expanded Form:  ' .expanded_cmdline)
  call setline(3,substitute(getline(2),'.','=','g'))
  execute '$read !'. expanded_cmdline
  setlocal nomodifiable
  1
endfunction

答案 6 :(得分:3)

此处的所有建议仅展示:!{cmd} %,它将当前缓冲区传递给shell cmd。 但还有另一种选择:write !{cmd} 例如,:write !sh命令的效果是当前缓冲区的每一行都在shell中执行。
例如,当你为缓冲区添加了几行时,它通常很有用。 ,并希望立即看到执行结果而不先保存缓冲区。
也可以执行一些范围,而不是缓冲区的整个内容:
:[range]write !{cmd}

答案 7 :(得分:2)

要执行当前的可执行脚本,请使用

:!./%

!执行shell命令,%是当前文件名,而./在前面添加当前目录。

答案 8 :(得分:0)

这取决于你的操作系统 - 实际上我没有在M $ Window $上测试它 - 但是Conque是那里最好的插件之一:http://code.google.com/p/conque/

实际上,它可以更好,但有效。您可以在vim“窗口”中嵌入一个shell窗口。