【问题标题】:How to execute a string of conditional commands which includes a shell command?如何执行包含 shell 命令的条件命令字符串?
【发布时间】:2015-12-23 10:17:17
【问题描述】:

我目前有以下 自动命令(在我的 vimrc 中)-

autocmd BufEnter *.c nmap <F5> :Makexec<CR> 
  \:if findfile( expand("%:p:r").".exe" ,expand("%:p:h") ) != ""<CR>
  \    :!%:p:r.exe<CR>
  \:else<CR>
  \    :vert topleft cwin<CR>
  \    :vert resize 50<CR>
  \:endif<CR> 

我想将此自动命令转换为执行单个字符串,其中该字符串包含链接在一起的上述命令。

  • 尝试 1

    let g:c_exe_str="Makexec | if findfile(expand(\"%:p:r\").\".exe\", expand(\"%:p:h\")) != \"\" | !%:p:r.exe | else | vert topleft cwin | vert resize 50 | endif"
    
    autocmd BufEnter *.c nmap <F5> :execute g:c_exe_str<CR>
    

    这并没有像我预期的那样工作,我在 shell 中收到以下错误 -

    C:\windows\system32\cmd.exe /c D:\NewFolder\c_experiments.exe |否则 | vert topleft cwin |垂直调整大小 50 |结束
    'else' 不是内部或外部命令、可运行程序或批处理文件。

  • 尝试 2

    let g:c_exe_str="Makexec | if findfile(expand(\"%:p:r\").\".exe\", expand(\"%:p:h\")) != \"\" | echo system(expand(\"%:p:r\").\".exe\") | else | vert topleft cwin | vert resize 50 | endif"
    
    autocmd BufEnter *.c nmap <F5> :execute g:c_exe_str<CR>
    

    这有点(原文如此)有效,但不会在 shell 窗口中显示可执行文件的输出;基本上它与原始自动命令的工作方式不同。

如何使用 execute() 准确地模仿原始自动命令的步骤?

【问题讨论】:

    标签: shell vim


    【解决方案1】:

    在尝试 1 中,尝试将 !%:p:r.exe 封装在 exe 命令中:

    ... | exe "!%:p:r.exe" | else ...
    

    否则栏,:! 命令之后的所有内容都会传递给 shell。

    【讨论】:

      猜你喜欢
      • 2016-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-03
      • 1970-01-01
      • 1970-01-01
      • 2011-06-15
      相关资源
      最近更新 更多