Tuesday, July 9, 2013

quick vim trick for fixing indentation and returning to where you were

So, many people have noted that you can use gg=G to fix indentation in a file.

gg makes the program go to the top of the file, and =G idents until the bottom.   however this places your cursor at the first line in the file, which isn't something you probably wanted to do when you just wanted to fix indentation.

But you can use 2<C-o> to go back to the place you typed the command. <C-o> jumps back, but you need two jumps backward to get back to where you were.

Therefore I've placed nmap <leader>g gg=G2<C-o> in my .vimrc to fix indentation as needed without needed to go back to where you were.

No comments:

Post a Comment