45

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

4

9 回答 9

75

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

:! wc %

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

:! perl %
于 2010-07-02T14:13:24.503 回答
17

vim 教程:在 Vim 中映射键

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

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

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

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

允许您在插入模式下调用相同的方法。

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

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

a) 写入当前文件:w

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

<CR>每个命令之后代表“回车”:执行特定命令的指令。imap 的作用与 map 相同,但在插入模式下听 Ctrl+p。

于 2013-09-02T15:33:55.707 回答
8

你可以直接从 vim 运行它:

:!./script.sh

于 2010-07-02T14:15:54.750 回答
7

这里的所有建议都只是展示了:!{cmd} %,它将当前缓冲区传递给 shell cmd。但是还有另一种选择:write !{cmd} ,例如该:write !sh命令的作用是在shell中执行当前缓冲区的每一行。
它通常很有用,例如,当您向缓冲区添加了几行,并希望立即查看执行结果而不先保存缓冲区时。
也可以执行某个范围,而不是缓冲区的全部内容:
:[range]write !{cmd}

于 2015-10-03T07:57:45.030 回答
6

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

例如。:

:!python %
于 2010-07-02T14:17:29.147 回答
5

听起来您正在寻找!

:!{cmd}{cmd}使用外壳执行。

%如果您需要将其传递给脚本,您可以使用它来表示当前文件名:

!proofread-script %

您还可以使用!范围,将命令用作过滤器:

!{motion}{filter}    " from normal mode
:{range}!{filter}    " from command mode

(在第一种情况下,与许多其他命令一样,当您键入动作时,它会将您传递到命令模式,将动作转换为范围,例如:.,.+2!

最后,如果您实际上不需要传递文件中的输入,但想要文件中的输出,那本质上是一个微不足道的过滤器,而最快的方法是!!{cmd}. 这将用命令的输出替换当前行。

于 2010-07-02T14:14:46.377 回答
4

这个小片段放在你的 .vimrc 中,用一个按键(如F5)执行当前文件,并将结果显示在一个新的拆分窗格缓冲区中。

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

虽然你可以做到这一点ctrl-z并将 vim 带回来,fg但这仍然意味着你需要大量切换上下文。

片段的工作方式是首先根据 猜测可执行文件filetype,然后使用当前文件作为参数运行它。

接下来,一个方便的实用程序方法获取输出并将其转储到新缓冲区中。

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

这是下面复制的代码段:

""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"""""""""""""""""""""""""" 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
于 2015-05-20T06:08:12.330 回答
4

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

:!./%

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

于 2018-03-21T13:51:47.803 回答
0

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

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

于 2010-07-02T15:01:40.803 回答