Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/visual-studio-2010/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vim 如何将接下来的N个单词(Shift&x2B;J)连接到当前行,而不是整行?_Vim_Vi - Fatal编程技术网

Vim 如何将接下来的N个单词(Shift&x2B;J)连接到当前行,而不是整行?

Vim 如何将接下来的N个单词(Shift&x2B;J)连接到当前行,而不是整行?,vim,vi,Vim,Vi,有时,当我在代码中重新格式化块注释时,我发现我需要将单词上移一行。在下面的例子中,我想把单词“hoppy”移到第一行。通常我使用Shift+J连接整行,将光标移动到单词“hoppy”之后,然后按Enter将行的其余部分向下移动 /* Line 1 of some large comment block, hopefully it contains well written documentation. I'd now like 'hopefully' to be on the fir

有时,当我在代码中重新格式化块注释时,我发现我需要将单词上移一行。在下面的例子中,我想把单词“hoppy”移到第一行。通常我使用
Shift+J
连接整行,将光标移动到单词“hoppy”之后,然后按
Enter
将行的其余部分向下移动

/* Line 1 of some large comment block,
   hopefully it contains well written documentation. I'd
   now like 'hopefully' to be on the first line.
 */

尽管看起来不多,但如果有很多行需要这样的更改,我目前正在执行的命令序列会变得单调乏味。Join命令是否有一种变体,只能对下一行的前N个字执行此操作?像“2wJ”这样的东西加入下一行的前两个单词会很好。

让vim处理注释块的格式如何<代码>gqap只执行一段。如果它不符合您的要求,请查看
:help'fo
(格式选项),尤其是
:help'fo table
。例如,设置
:set fo+=j
可能有助于处理这些C块(在连接行时自动删除注释引线)。

让vim处理注释块的格式如何<代码>gqap只执行一段。如果它不符合您的要求,请查看
:help'fo
(格式选项),尤其是
:help'fo table
。例如,设置
:set fo+=j
可能有助于处理这些C块(在加入行时自动删除注释前导)。

坏消息

我不确定是否存在像你所说的那样的东西,尽管它肯定会——Vim每天都让我感到惊讶。我相信有人知道

好消息

有一个更好的方法来实现你想要的!或者,我想你想要什么。您可以告诉Vim对给定的文本区域执行格式化操作。您所要做的就是将
textwidth
设置为您想要的最大宽度,然后使用
gq{motion}
格式化
{motion}
移动的文本。例如:

/* Line 1 of some large comment block,
   hopefully it contains well written documentation. I'd
   now like 'hopefully' to be on the first line.
 */
您只需设置一个合理的
textwidth
——这里看起来您的价格大约是50——然后格式化注释。将光标放在第一个
/
上,然后

:set textwidth=50
gq%
捕获

但是等等,你说,那没用

哎呀。你说得对。这里发生的事情似乎是Vim不理解这种格式。当我按照自己的指示行事时,我看到了这一点,尽管您的里程数可能有所不同:

/* Line 1 of some large comment block,
   hopefully it contains well written 
documentation. I'd now like 'hopefully' to be on 
the first line.
 */
这看起来不对,而且也不对。但是,如果我将您的块注释重新格式化为Vim可以识别的内容(这就是我编写块注释的方式),例如:

/* 
 * Line 1 of some large comment block,
 * hopefully it contains well written documentation. I'd
 * now like 'hopefully' to be on the first line.
 */
然后我
gq%
(使用
textwidth
仍然是50)我得到:

这似乎奏效了。“现在”一词也从第三行移到了第二行,但我认为这是Vim的一个好决定。还有一点需要注意:如果您不喜欢前面的
*
s,那么Vim也会很好地使用这种格式:

/* 
   Line 1 of some large comment block,
   hopefully it contains well written documentation. I'd
   now like 'hopefully' to be on the first line.
 */
当我
gq%
这样做时,我得到:

/* 
   Line 1 of some large comment block, hopefully 
it contains well written documentation. I'd now 
like 'hopefully' to be on the first line.
 */
有点滑稽,但这很有道理。在我看来更像是一段话。希望这里有些有用的东西

编辑:


正如progo在另一个答案中轻松提到的那样,您可以在我上面使用的
gq%
任何地方使用
gqap
。谢谢

坏消息

我不确定是否存在像你所说的那样的东西,尽管它肯定会——Vim每天都让我感到惊讶。我相信有人知道

好消息

有一个更好的方法来实现你想要的!或者,我想你想要什么。您可以告诉Vim对给定的文本区域执行格式化操作。您所要做的就是将
textwidth
设置为您想要的最大宽度,然后使用
gq{motion}
格式化
{motion}
移动的文本。例如:

/* Line 1 of some large comment block,
   hopefully it contains well written documentation. I'd
   now like 'hopefully' to be on the first line.
 */
您只需设置一个合理的
textwidth
——这里看起来您的价格大约是50——然后格式化注释。将光标放在第一个
/
上,然后

:set textwidth=50
gq%
捕获

但是等等,你说,那没用

哎呀。你说得对。这里发生的事情似乎是Vim不理解这种格式。当我按照自己的指示行事时,我看到了这一点,尽管您的里程数可能有所不同:

/* Line 1 of some large comment block,
   hopefully it contains well written 
documentation. I'd now like 'hopefully' to be on 
the first line.
 */
这看起来不对,而且也不对。但是,如果我将您的块注释重新格式化为Vim可以识别的内容(这就是我编写块注释的方式),例如:

/* 
 * Line 1 of some large comment block,
 * hopefully it contains well written documentation. I'd
 * now like 'hopefully' to be on the first line.
 */
然后我
gq%
(使用
textwidth
仍然是50)我得到:

这似乎奏效了。“现在”一词也从第三行移到了第二行,但我认为这是Vim的一个好决定。还有一点需要注意:如果您不喜欢前面的
*
s,那么Vim也会很好地使用这种格式:

/* 
   Line 1 of some large comment block,
   hopefully it contains well written documentation. I'd
   now like 'hopefully' to be on the first line.
 */
当我
gq%
这样做时,我得到:

/* 
   Line 1 of some large comment block, hopefully 
it contains well written documentation. I'd now 
like 'hopefully' to be on the first line.
 */
有点滑稽,但这很有道理。在我看来更像是一段话。希望这里有些有用的东西

编辑:


正如progo在另一个答案中轻松提到的那样,您可以在我上面使用的
gq%
任何地方使用
gqap
。谢谢

vim中没有内置这样的东西

想想
2wJ
,vim会认为您想向前移动两个单词,然后加入

您可以使用旧方法执行此操作:
J2elr
,或者创建一个映射,接收要作为参数加入的字数,以使其更简单:

nnoremap <expr> <leader>jw 'J' . nr2char(getchar()) . 'elr<CR>'
nnoremap jw'J'。nr2char(getchar())。'elr'
因此,在这个映射中,如果您按下
jw
,vim将等待另一个输入键,即您想要“加入”的字数但这不是一个完美的解决方案,因为:

  • 这行不通