Search code examples
vimreformatting

How do I execute command similar to gg=G in Vim without going to the top of the file?


How can I reformat the whole buffer in Vim, the same way as I am doing using gg=G keys, without going the the top (which is caused by the gg)?


Solution

  • You can mark the current position with m<letter> command and then go back with `<letter>.

    mzgg=G`z
    

    The referenced duplicate uses more effective variant of this approach using the fact that double backtick goes to the last cursor position so you don't actually need to mark the current position:

    gg=G``
    

    Or you can install a plugin for text object of entire buffer (e.g. https://github.com/kana/vim-textobj-entire) and then do

    =ae
    

    (or equivalent with another plugin).