在Vim中命令后自动缩进文件

时间:2016-01-21 16:09:39

标签: vim command indentation

我正在尝试编写一个名为Remove的自定义vim命令,该命令接受一个参数并删除该文件中与该参数完全相同的所有行。例如,如果文件是

int main() {
    int x = 3;
    int y = 4;
    cout << x << endl;
}

我使用命令

:Remove int y = 4;

输出为

int main() {
    int x = 3;
    cout << x << endl;
}

通过定义

我可以非常接近
:command -nargs=1 Remove :%s/<args>\n//g

但是使用这个命令,替换后文件没有正确缩进,这需要我之后运行gg = G.

有没有办法让gg = G自动作为命令的一部分运行?

2 个答案:

答案 0 :(得分:2)

不确定。您可以使用public class DBConnection { private static DBConnection instance = null; private DBConnection(){} private static final String SERVER = "server"; private static final String INSTANC = "instance"; private static final String BBDD = "db"; private static final String USER = "user"; private static final String PASS = "pass"; private static Connection connection = null; public static DBConnection getInstance(){ if(instance == null){ instance = new DBConnection(); } return instance; } private Connection conectar(){ Connection conn = null; String connString; try { Class.forName("net.sourceforge.jtds.jdbc.Driver"); connString = "jdbc:jtds:sqlserver://" + SERVER + ";" + "instance=" + INSTANC + ";" + "databaseName=" + BBDD + ";" + "user=" + USER + ";" + "password=" + PASS + ";"; conn = DriverManager.getConnection(connString); } catch (SQLException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } return conn; } public Connection getConnection(){ if(connection == null){ connection = conectar(); } return connection; } public static ResultSet devuelveResultSet(String query){ ResultSet result=null; try { Statement statement= connection.createStatement(); result=statement.executeQuery(query); } catch (SQLException e){ e.printStackTrace(); } return result; } } 连接多个命令。如果这太长,您可以将代码分解为01-21 09:48:28.908: E/AndroidRuntime(2178): FATAL EXCEPTION: main 01-21 09:48:28.908: E/AndroidRuntime(2178): java.lang.NoSuchMethodError: java.lang.String.isEmpty 01-21 09:48:28.908: E/AndroidRuntime(2178): at net.sourceforge.jtds.jdbc.SharedSocket.createSocketForJDBC3(SharedSocket.java:282) 01-21 09:48:28.908: E/AndroidRuntime(2178): at net.sourceforge.jtds.jdbc.SharedSocket.<init>(SharedSocket.java:251) 01-21 09:48:28.908: E/AndroidRuntime(2178): at net.sourceforge.jtds.jdbc.JtdsConnection.<init>(JtdsConnection.java:331) 01-21 09:48:28.908: E/AndroidRuntime(2178): at net.sourceforge.jtds.jdbc.Driver.connect(Driver.java:184) 01-21 09:48:28.908: E/AndroidRuntime(2178): at java.sql.DriverManager.getConnection(DriverManager.java:191) 01-21 09:48:28.908: E/AndroidRuntime(2178): at java.sql.DriverManager.getConnection(DriverManager.java:154) 01-21 09:48:28.908: E/AndroidRuntime(2178): at sql.DBConnection.conectar(DBConnection.java:43) 01-21 09:48:28.908: E/AndroidRuntime(2178): at sql.DBConnection.getConnection(DBConnection.java:57) 01-21 09:48:28.908: E/AndroidRuntime(2178): at com.engineering.ecg.ListinTelefonico.onCreate(ListinTelefonico.java:39) 01-21 09:48:28.908: E/AndroidRuntime(2178): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 01-21 09:48:28.908: E/AndroidRuntime(2178): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) 01-21 09:48:28.908: E/AndroidRuntime(2178): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 01-21 09:48:28.908: E/AndroidRuntime(2178): at android.app.ActivityThread.access$2300(ActivityThread.java:125) 01-21 09:48:28.908: E/AndroidRuntime(2178): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 01-21 09:48:28.908: E/AndroidRuntime(2178): at android.os.Handler.dispatchMessage(Handler.java:99) 01-21 09:48:28.908: E/AndroidRuntime(2178): at android.os.Looper.loop(Looper.java:123) 01-21 09:48:28.908: E/AndroidRuntime(2178): at android.app.ActivityThread.main(ActivityThread.java:4627) 01-21 09:48:28.908: E/AndroidRuntime(2178): at java.lang.reflect.Method.invokeNative(Native Method) 01-21 09:48:28.908: E/AndroidRuntime(2178): at java.lang.reflect.Method.invoke(Method.java:521) 01-21 09:48:28.908: E/AndroidRuntime(2178): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 01-21 09:48:28.908: E/AndroidRuntime(2178): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 01-21 09:48:28.908: E/AndroidRuntime(2178): at dalvik.system.NativeStart.main(Native Method) 。由于|普通模式命令,而不是像:function这样的 Ex命令,因此您需要通过=调用它; :substitute避免映射干扰。

:normal

答案 1 :(得分:1)

缩进是错误的,因为您的Remove命令不会删除找到的行中的前导空格/制表符。

您可以修改命令以删除空格,而不是重新输入所有文件:

:command -nargs=1 Remove %s/^\s*\V<args>\n//g
  • ^匹配行的开头;
  • \s*匹配可选空格或制表符;您甚至可以在\s*之前再次放置\n,以考虑末尾隐藏空格的行;
  • 我添加了\V,这可以避免在您的命令参数中使用特殊的正则表达式字符进行解释。如果你没有把它,请使用以下命令:

    :Remove char *my_str = 0;
    
    由于*

  • 会被误解