I was reading that the way to indent in vim is a gg=G
sequence. This however loses my cursor position.
How can I indent my file without losing my cursor position?
I was reading that the way to indent in vim is a gg=G
sequence. This however loses my cursor position.
How can I indent my file without losing my cursor position?
You could define a custom command using :help winsaveview()
:
command! -range=% Equal let b:wv = winsaveview() | silent execute <line1> . ',' . <line2> . 'normal! ==' | call winrestview(b:wv)
:Equal like gg=G
:12,23Equal like :12,23normal! ==
v{motion}:Equal like v{motion}=
Expanding on Karl Yngve Lervåg's answer:
maHmbgg=G`bzt`a
Explanation:
ma - Set mark 'a'
H - Jump to the top of the window
mb - Set mark 'b'
gg=G - Indent from the first line to the last
`b - Jump to mark 'b'
zt - Scroll the cursor line to the top of the window
`a - Jump to mark 'a'
This will preserve your scroll position after indenting.
If you want to preserve the relative cursor position after the cursor line has been indented, you have to update the new cursor position after indenting. The following script expands on romainl's answer:
function! s:reindent(line1, line2)
let view = winsaveview()
let text = getline('.')
execute a:line1.','.a:line2.'normal! =='
let view.col -= strchars(text) - strchars(getline(view.lnum))
call winrestview(view)
endfunction
command! -range=% Reindent call s:reindent(<line1>, <line2>)
You should use a mark, e.g.:
magg=G`a
Here ma
will set the a
mark at the current position, then gg=G
indents the file, and `a
will jump to the mark.
In order to put the cursor at the center of the screen afterwards, you could finish this off with zz
. See :h scroll-cursor
for more info and for similar scroll mappings.
'a
will go back first non-whitespace character of the line, use `a
to go to exact position marked
– Sundeep
Jun 23 '16 at 5:09
maHmbgg=G`bzt`a
. It sets two marks, a
for the current cursor position, and b
for the top of the buffer. After indenting, it jumps the b
mark and uses zt
to scroll it to the top. Then jump to the a
mark.
– Tommy A
Jun 23 '16 at 5:13
zz
to center the window around the cursor.
– Karl Yngve Lervåg
Jun 23 '16 at 6:40
:normal! gg=G
? – SibiCoder Jun 23 '16 at 4:09