【问题标题】:Editing multiple lines in VIM with variable indent使用可变缩进在 VIM 中编辑多行
【发布时间】:2022-12-31 21:45:03
【问题描述】:

我需要一次编辑多行。我知道如何从 ctrl+v - down ... - shift+i 行的开头开始,但问题是我的变量长度不同,所以我不能用同样的技巧来添加结尾每个变量的。我当然可以在每一行上做.,但我想知道是否有更快的方法来实现相同的结果?

例子:我需要改变:

parser.add_argument('--name', type=str, help='The name of the experiment')
parser.add_argument('--debug', default=False, action='store_true', help=f'If the run is a debugging run')
parser.add_argument('--gpu_id', type=int, default=0 if torch.cuda.device_count() > 0 else -1, help='The ID of the GPU (if there is any) to run the network on (e.g., --gpu_id 1 will run the network on GPU #1 etc.)')

到:

name = 
debug = 
gpu_id = 

所以我这样做:

  1. 转到第一行,点击ctrl+vdowndowndownshift+i
  2. 然后删除所有内容,直到最后一个-,而不是esc
  3. 编辑每一行的结尾我可以到第一行的结尾,shift+cspace=
  4. 然后在另一行通过f+'.重复此操作

    谢谢

【问题讨论】:

    标签: vim


    【解决方案1】:

    在这种情况下我喜欢做的是:

    1. 选择您希望在视线模式下编辑的所有行 (Vjj)
    2. 通过按:.这将自动将范围设置为'<,'>,然后键入norm yi'VpxxA =

      所以整个命令变成:'<,'>norm yi'VpxxA =

      这将抽出每行中第一个单引号对的内部内容,用抽出的文本替换整行,删除前两个字符,然后附加 =

    【讨论】:

      【解决方案2】:

      使用两个简单的替换(比找出一个复杂的替换更快):

      " remove what comes before the variable name
      :,+2s/.{-}--
      
      " substitute what comes after it with an equal sign
      :'[,s/'.*/ =
      

      参见:help :range:help :s:help pattern-multi-items

      有很多方法可以做到这一点……

      【讨论】:

        猜你喜欢
        • 2017-10-25
        • 1970-01-01
        • 2022-09-28
        • 2023-03-18
        • 2015-12-27
        • 2011-10-21
        • 2012-04-08
        • 2012-07-31
        • 2010-09-29
        相关资源
        最近更新 更多