循环时使vim正确缩进管道



我一直在用这行代码读取ip_list.txt中的ip1.1.1.1,存储在变量line中,然后打印出来:

if [ true == false ]; then # Example
ip="1.1.1.1"
grep -r $ip ip_list.txt | while read -r line ; do
echo "Line is: $line"
done
fi

代码运行良好,但vim没有正确缩进此代码。当我这样做时,g=GG,你可以看到done语法应该排在grep语法下面,但它与if语句一起向左移动。它将在vim:中这样缩进

if [ true == false ]; then
ip="1.1.1.1"
grep -r $ip ip_list.txt | while read -r line ; do
echo "Line is: $line"
done # Went to the left. Not lined up with grep
fi

即使我去掉了;,让底部的do像这样:

if [ true == false ]; then # Example
ip="1.1.1.1"
grep -r $ip ip_list.txt | while read -r line
do
echo "Line is: $line"
done
fi

在vim代码编辑器中,done语法仍然没有正确缩进(现在如果我执行g=GG(:

if [ true == false ]; then
ip="1.1.1.1"
grep -r $ip ip_list.txt | while read -r line
do
echo "Line is: $line"
done # not lined up with grep syntax
fi

有没有办法编辑这个代码,让vim可以正确地缩进它?

预期输出应为:

if [ true == false ]; then
ip="1.1.1.1"
grep -r $ip ip_list.txt | while read -r line ; do
echo "Line is: $line"
done
fi

或者应该是

if [ true == false ]; then
ip="1.1.1.1"
grep -r $ip ip_list.txt | while read -r line
do
echo "Line is: $line"
done
fi

vim的缩进regex不够智能。如果您愿意,您可以自己编辑语法文件:使用:scriptnames查看vim加载的文件,以查看syntax/sh.vim文件的完整路径。

一个更简单的方法是更改bash语法:

if [ true == false ]; then # Example
ip="1.1.1.1"
while read -r line; do
echo "Line is: $line"
done < <(grep -r $ip ip_list.txt )
fi

正确缩进到

if [ true == false ]; then # Example
ip="1.1.1.1"
while read -r line; do
echo "Line is: $line"
done < <(grep -r $ip ip_list.txt )
fi

最新更新