0

In my .vimrc file I have

autocmd BufWritePre *.c :g/./ normal gqq

with text width 119 in order to hardwrap the lines before saving the buffer.

I tried

autocmd BufWritePre *.c :g/./ normal gqq``zz

to move the cursor back to where it was and bring it in the centre but it seems like taking me to some arbitary location.

Can anyone help me with this?

4
  • Other issues aside, it would be more robust to set a mark explicitly and jump to that, IMO.
    – B Layer
    Jan 21, 2019 at 11:10
  • How to do that ?
    – MaNyYaCk
    Jan 21, 2019 at 11:18
  • If you want to mark whatever the currrent line is before the global command is run, for instance, then kx | exe ':g/./ ... etc' | normal 'xzz
    – B Layer
    Jan 21, 2019 at 11:30
  • Note that I arbitrarily chose x for the mark. Use whatever you want. (You could even use, believe it or not, ` ... though I'd probably avoid it to prevent confusion.)
    – B Layer
    Jan 21, 2019 at 11:40

1 Answer 1

1

What your command does :g/./ normal gqq``zz is "for each line do normal command which is format line and goto last position and recenter.

What you have to do is to separate :g/../normal gqq command and the rest.

Smth like (tested) exe ':g/./normal gqq' | normal ``zz

  1. exe is to execute a string as a command (:h execute)
  2. | is to add another command to the line
  3. normal to execute normal mode commands from command mode :)

So the end result should be (not tested)

autocmd BufWritePre *.c :exe ':g/./normal gqq' | normal ``zz

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.