从Vim检测结果可执行文件



我有一个多文件项目,我使用Cmake构建系统。我已经将:make映射到一个键,因为我需要经常编译它。问题是,我需要同样频繁地运行生成的可执行文件。然而,键入:!./variable_program_name是非常乏味的。

是否有任何方法可以检测/获取生成的可执行文件名?

推荐的方法是通过Makefile中的一个单独的目标来实现这一点,例如:make只触发构建,:make run触发可执行文件的(构建和)运行。毕竟,Makefile最清楚它在构建什么,所以如何运行构建工件(可能还有传递的参数)的决策最好委托给它

备选方案

要从Makefile"返回"可执行文件,:make输出将被解析并填充快速修复列表。您可以定义一个自定义映射(用于qf文件类型,它是为此类窗口设置的),从当前快速修复行解析可执行文件名,甚至可以使用getqflist()解析整个输出。这需要Makefile以可检测的方式打印出可执行文件的名称(和路径)。

备选方案

如果您甚至不能从输出中可靠地获得可执行文件的名称,但知道可执行文件生成的目录,则可以在运行:make之前创建一个文件列表(使用glob()),然后再次创建,并比较这两个列表以获得可执行文件的名称。如果您不想从Vim中删除以前的可执行文件,文件时间检查(getftime())可能会有所帮助。

对于任何查看此的人。。我也有同样的需求,决定用vim脚本/插件来解决它。vim目标

扩展Ingo Karkat的想法,这个脚本应该能做到(我不擅长vimscript,所以我在bash中写了它)

#!/bin/sh
# This script tries to build the project using Makefile, and if that fails
# it tries to generate the Makefile with Cmake.
# Then it finds the latest executable file and runs it
# Remember to manually run cmake after changing CMakeLists.txt as a new 
# Makefile will not be automatically regenerated
if [[ -e "Makefile" ]] || [[ -e "makefile" ]]; then
make
if [[ $? -ne 0 ]]; then
echo "Error when running make"
exit
fi
else
if [[ -e "CMakeLists.txt" ]]; then
cmake .
if [[ $? -ne 0 ]]; then
echo "Error when running cmake"
exit
fi
make
if [[ $? -ne 0 ]]; then
echo "Error when running make"
exit
fi
else
echo "CMakeLists.txt doesn't exist"
exit
fi
fi
# Find latest executable file
unset latest
for file in "${1:-.}"/*
do
if [[ -f "$file" ]]; then
latest=${latest-$file}
find "$file" -executable -prune -newer "$latest" | read -r dummy && latest=$file
fi
done
if [[ -x "$latest" ]]; then
./$latest
else
echo "Latest file $latest is not executable"
fi

只需将这个脚本放在$PATH中,并映射一个密钥来执行它

最新更新