duanbi3385 2016-12-01 18:12 采纳率: 100%
浏览 149

使用可以正常使用“ go fmt”工具的Vim进行注释

Golang has a tool that's called 'gofmt' which formats your code. I'm using the vim-go plugin which fires the 'gofmt' tool every time when I save a file.

Golang uses tabs for indention. I marked the tabs as [ ] in the following examples.

Here's a problem which I've run:

I have the following code:

func main() {
[  ]if true {
[  ][  ]do.Something()
[  ]}
}

I need to comment the if clause. So, if I comment it like the following(I'm using nerd-commenter):

func main() {
//[  ]if true {
//[  ][  ]do.Something()
//[  ]}
}

Gofmt formats it to:

func main() {
[  ]//[  ]if true {
[  ]//[  ][  ]do.Something()
[  ]//[  ]}
}

If I do this:

func main() {
[  ]//if true {
[  ][  ]//do.Something()
[  ]//}
}

Gofmt formats it to this:

func main() {
[  ]//if true {
[  ]//do.Something()
[  ]//}
}

I need to comment the if clause like the following:

func main() {
[  ]//if true {
[  ]//[  ]do.Something()
[  ]//}
}

Then Gofmt won't change the commented block. How I can do this?

  • 写回答

1条回答 默认 最新

  • duanlan4801 2016-12-04 03:39
    关注

    The way I do this is with block mode insertion.

    • Put your cursor on the i of if.
    • Press <C-v> to start insert mode, then jj to move it down two lines.
    • Press I to enter "Visual-Block insert", which will insert text at the start of the block selection for every line (see v_b_I).
    • Press //<Esc> to add // and leave visual block insert mode.

    Which should give you exactly what you want:

    enter image description here

    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分