从所选行中删除vim中的前N个字符

时间:2016-01-13 19:51:45

标签: regex vim

/**
 * Definition for an interval.
 * struct Interval {
 *     int start;
 *     int end;
 *     Interval() : start(0), end(0) {}
 *     Interval(int s, int e) : start(s), end(e) {}
 * };
 */

我有这个格式代码,想要将vim中的struct取消注释为:

/**
 * Definition for an interval.
 */
struct Interval {
    int start;
    int end;
    Interval() : start(0), end(0) {}
    Interval(int s, int e) : start(s), end(e) {}
};

现在我只是使用它:%s / \ s * \ s // gc删除'*',但它也会影响行'*定义一个间隔。',不确定vim是否有路选择我想要替换的行。或者,如果有另一种更好的方法来移动*以进行评论。

3 个答案:

答案 0 :(得分:4)

完全 47.337种方法可以做你想要的。这是一个......

移动 - :m - 匹配*/的第一行 - /\*\/ - 位于匹配struct的第一行 - ?struct? - 上方:

:/\*\//m?struct?-

结果:

/**
 * Definition for an interval.
 */
 * struct Interval {
 *     int start;
 *     int end;
 *     Interval() : start(0), end(0) {}
 *     Interval(int s, int e) : start(s), end(e) {}
 * };

剪切当前一行{ - 1}}正下方线之间的每一行的前三个字符:normal 3x - 以及匹配+的第一行 - }; - :

/};/

结果:

:+,/};/norm 3x

答案 1 :(得分:1)

您可以使用可视线模式 Shift v 标记线条,然后使用以下其中一项:

NSString *smiley = @"\U0001F642";
self.myTextField.stringValue = smiley;

然后将评论关闭" Remove leading space, asterisk, space :s/\s\*\s// " Remove leading 3 characters on line, whatever they are :s/^...// 向上移动到评论现在应该结束的位置。

答案 2 :(得分:1)

另外47.337种方式......使用视觉模式。

首先将光标移动到第3行(3G),然后再移动

<ctrl-v>6jlldO */<esc>

或更便携的方式:

<ctrl-v>/*\/<cr>ldO */<esc>

顺便说一下:如果您使用6j

启用相对行号,则可以轻松找出要向下移动的行数(:set relativenumber - 部分)