无法通过 VIM 运行链接脚本

Can't run a linkage script through VIM

本文关键字:运行 链接 脚本 VIM      更新时间:2023-10-16
  1. 您好,我编写了一个编译多个 cpp 和目标文件的 bash 脚本在 G++ 中。我的目标是通过 :!在 vim 中运行脚本,但是它在 vim 中不起作用,只有当我在外面时。

  2. 此外,我想为什么在脚本中使用%没有给我当前文件,但给出了错误相反。

脚本:

#!/bin/bash
# Search for the main module and remove the ext.
delimain=`grep main *.cpp | cut -d. -f1`
# Checks if there are also object files
if [ -f ./*.o ]; then
g++ -g *.cpp *.o -o $delimain.exe
# If There are only cpp file
else
g++ -g *.cpp -o $delimain.exe
fi

谢谢!

回复您的评论

我正在使用 ubuntu 13.10 别名:别名链接"sh ~/bin/lin_script %"

  1. 你不应该使用显式解释器调用 shell 脚本;#!/bin/bash第一行已经告诉 shell 要使用哪个解释器。你显然是 Bash 的初学者;尝试阅读一些介绍以获得更好的理解。
  2. 别名在 Vim 中不起作用,因为它们只在交互式 shell 中定义,但从 Vim 启动的命令通常在非交互式 shell 中启动(因为这更快,并且附带更少不必要的东西)。
  3. 别名由 shell 解释,但%符号对 Vim 来说是特殊的。两者是不一样的。请参阅我的另一个答案如何将文件名传递给脚本。

您说得对,当提供给 Vim 中的 Ex 命令时%会自动扩展,但这不适用于外部脚本。你要做的是在调用外部脚本时传递当前文件,并在其中引用命令行参数:

维姆内部:

:!linkage.sh %

在您的脚本中:

if [ $# -gt 0 ]; then
    delimain=$1
else
    delimain=`grep ...