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?
The way I do this is with block mode insertion.
i
of if
.<C-v>
to start insert mode, then jj
to move it down two lines.I
to enter "Visual-Block insert", which will insert text at the start of the block selection for every line (see v_b_I
).//<Esc>
to add //
and leave visual block insert mode.Which should give you exactly what you want: