在Vim中将文件行插入多行选择(视觉块模式)

在Vim中将文件行插入多行选择(视觉块模式),vim,Vim,假设文件“file.txt”包含: 31 37 14 我想将这些行插入Vim缓冲区的某些位置(可能使用带Ctrl-v的可视块模式)。 换句话说,如果文件“text.txt”包含 This is the beginning of a file with several lines Age of Person A: ; Other info... Age of Person B: ; Other info... Age of person C: ; Other info... This is th

假设文件“file.txt”包含:

31
37
14
我想将这些行插入Vim缓冲区的某些位置(可能使用带Ctrl-v的可视块模式)。 换句话说,如果文件“text.txt”包含

This is the beginning of a file with several lines
Age of Person A: ; Other info...
Age of Person B: ; Other info...
Age of person C: ; Other info...
This is the end of the file.
是否可以将“file.txt”的相应行插入“;”之前的位置每个选定行的字符

预期的结果将是:

This is the beginning of a file with several lines
Age of Person A: 31; Other info...
Age of Person B: 37; Other info...
Age of person C: 14; Other info...
This is the end of the file.

谢谢。

是的!只需在可视块模式下突出显示数字(
C-v
):

猛拉(
y
)并在正常模式下将光标放在我放置的
|
符号如下:

This is the beginning of a file with several lines
Age of Person A:|; Other info...
Age of Person B: ; Other info...
Age of person C: ; Other info...
This is the end of the file.
并使用put命令(
p
)。你最终会得到:

This is the beginning of a file with several lines
Age of Person A: 31; Other info...
Age of Person B: 37; Other info...
Age of person C: 14; Other info...
This is the end of the file.

假设您正在编辑
text.txt

  • 编辑
    file.txt

    :e file.txt<CR>
    
    <C-^> (or <C-6>, or :b#<CR>)
    
  • 切换回
    text.txt

    :e file.txt<CR>
    
    <C-^> (or <C-6>, or :b#<CR>)
    
  • 参考:

    :help :edit
    :help visual-block
    :help G
    :help l
    :help ctrl-^
    :help /
    :help P
    

    作为替代答案,您可能不知道(直到现在我才知道)您可以创建跨文件/拆分工作的Vim宏

    对于此问题,您可以创建一个拆分,将两个文件相邻放置在同一Vim会话中:

    然后在目标文件上,将光标移动到要修改的第一行,在本例中为
    人员A的年龄
    。现在按
    q
    和您选择的任何字母,以录制该字母处的宏。一旦录音完毕,我就输入了以下内容
    ^Whggdd^Wlpkf;d$jpkJ
    转到左窗口
    ^Wh
    ^W
    ctrl+W
    ),转到顶行
    gg
    ,剪切行
    dd
    ,移回右窗口
    ^Wl
    ,粘贴并重新排列行,使其按正确顺序排列
    pkf;d$jpkJ
    。然后我按下
    q
    停止宏,现在您可以使用
    @
    和存储宏的字母将宏应用到所需的每一行。在我的例子中,我只是在剩下的行上做了一个可视块,然后键入
    :norm@a
    ,因为
    a
    是我为宏选择的字母

    有点黑客,但是跨文件和拆分的宏可能是了解的有用特性

    /;<CR>
    
    P
    
    :help :edit
    :help visual-block
    :help G
    :help l
    :help ctrl-^
    :help /
    :help P