zoukankan      html  css  js  c++  java
  • 如何用 vim 文本编辑器比较两文件

    使用 vim 文本编辑器比较两个文件的不同,可以采用两种打开方式:

    方式一,使用 vim 同时打开两个待比较的文件。

    比较通用方式是采用 vim -d 选项,具体命令,如下:

    vim -d <file1> <file2>

    在 Linux/Mac 下,还可以采用 vimdiff 封装命令, 具体如下:

    vimdiff <file1> <file2>

    但在 Window 版本的 gvim(Vi IMproved 8.1 版本) 中,没有名称 vimdiff 可以执行文件,可以采用 diff 命令按上面的方式执行,不过只返回命令行界面下的比较结果。

    方式二,先用 vim 打开一个文件,然后启动 diff mode,与另一个文件进行比较。

    1) 正常使用 vim 编辑一个文件 oneFile,命令如下:

    vim oneFile

    2) 然后采用 :diffthis:diffsplit 命令启动 diff mode。

    a) 采用 :diffthis 命令的具体示例

    在当前文件 oneFile 中,启动 diff mode,命令如下:

    :diffthis

     在新窗口中,打开另一个文件 otherFile。如果采用垂直切分(vertical split)方式打开文件 otherFile,命令如下:

    :vs otherFile

    如果采用水平切分(horizontal split)方式打开,命令如下:

    :split otherFile

    在当前文件 otherFile 中,启动 diff mode,命令如下:

    :diffthis

    到此,就可以看到两个文件的差异显示了。

    关闭 diff mode 的命令,如下:

    :diffoff

    b) 采用 :diffsplit 命令的具体示例

    一条命令即可以显示两文件之间的差异,如下:

    :diffsplit otherFile

    注:可以通过 :h diff:h vimdiff 查看更多帮助信息。

    diff mode 常用命令速查表(cheat sheet)

    [c    Jump to the previous diff 跳到前一个不同之处

    ]c    Jump to the next diff  调到下一个不同之处

    do    diffget: Pull the changes to the current file  将所有的不同之处拉到当前文件,使之与另一个文件内容相同

    dp    diffput: Push the changes to the other file 将所有的不同之处推到另一个文件

    :diffupdate  rescan files for differences  重新扫描文件之间的不同之处

    Folds 折叠命令

    zo/zO   Open 打开折叠 

    zc/zC   Close 关闭折叠

    za/zA   Toggle 在打开折叠和关闭折叠之间进行切换

    zv      Open folds for this line 为当前行打开折叠

    zM     Close all  关闭所有折叠

    zm     Fold more (foldlevel += 1)  更多地折叠

    zR     Open all  打开所有折叠

    zr    Fold less (foldlevel -= 1) 更少地折叠

    注:通过命令 :h fold-commands 查看更多关于折叠命令的帮助信息。

    参考资料

    [1] How do I use vim as a diff tool? https://vi.stackexchange.com/questions/625/how-do-i-use-vim-as-a-diff-tool

    [2] vim-diff cheatsheet. https://devhints.io/vim-diff 

  • 相关阅读:
    Leetcode Substring with Concatenation of All Words
    Leetcode Divide Two Integers
    Leetcode Edit Distance
    Leetcode Longest Palindromic Substring
    Leetcode Longest Substring Without Repeating Characters
    Leetcode 4Sum
    Leetcode 3Sum Closest
    Leetcode 3Sum
    Leetcode Candy
    Leetcode jump Game II
  • 原文地址:https://www.cnblogs.com/klchang/p/13441038.html
Copyright © 2011-2022 走看看