Vim Tips Wiki
(Created page with 'When editing a git commit message (<tt>.git/COMMIT_EDITMSG</tt>) you often won't start on the first line due to [[Restore cursor to file position in previous editing session|Vim …')
 
(Added a suggestion of usage of a nested autocommand.)
 
(6 intermediate revisions by 4 users not shown)
Line 1: Line 1:
  +
{{TipNew
When editing a git commit message (<tt>.git/COMMIT_EDITMSG</tt>) you often won't start on the first line due to [[Restore cursor to file position in previous editing session|Vim remembering your last position in that file]]. An easy way to fix this is to add the following line to your [[vimrc]] file:
 
  +
|id=1636
 
  +
|previous=1635
  +
|next=1637
  +
|created=2009
  +
|complexity=basic
  +
|author=Khym Chanur
  +
|version=7.0
  +
|subpage=/200910
 
|category1=VersionControl
  +
|category2=
  +
}}
 
When editing a git commit message (<code>.git/COMMIT_EDITMSG</code>) you often won't start on the first line due to [[Restore cursor to file position in previous editing session|Vim remembering your last position in that file]]. An easy way to fix this is to add the following line to your [[vimrc]]:
 
<pre>
 
<pre>
 
autocmd FileType gitcommit call setpos('.', [0, 1, 1, 0])
 
autocmd FileType gitcommit call setpos('.', [0, 1, 1, 0])
 
</pre>
 
</pre>
   
Alternatively, you can create a file named <tt>gitcommit.vim</tt> in <tt>[[Keep your vimrc file clean|~/.vim/ftplugin]]</tt> and make it contain the following line:
+
Alternatively, you can create a file named <code>gitcommit.vim</code> in <code>[[Keep your vimrc file clean|~/.vim/ftplugin]]</code> containing the following line:
 
 
<pre>
 
<pre>
 
call setpos('.', [0, 1, 1, 0])
 
call setpos('.', [0, 1, 1, 0])
 
</pre>
 
</pre>
  +
[[Category:VersionControl]]
 
  +
==Comments==
  +
We have two other short git tips:
  +
*[[VimTip1262|1262 Git grep]]
  +
*[[VimTip1614|1614 Using Git from Vim]]
  +
Why is this tip needed when it is so easy to type <code>gg</code> to go to the first line? [[User:JohnBeckett|JohnBeckett]] 05:01, May 15, 2010 (UTC)
  +
  +
Probably because muscle-memory might lead someone to immediately try to insert text, without looking? ~Anon, 2012-08-21 8:47-0700
  +
  +
----
  +
Also, you should hook other event, because FileType is too early, and cursor position will be overwritten using info from .viminfo:
  +
<pre>
  +
function MyBufEnter()
  +
" don't (re)store filepos for git commit message files
  +
if &filetype == "gitcommit"
  +
call setpos('.', [0, 1, 1, 0])
  +
endif
  +
endfunction
  +
au BufEnter * call MyBufEnter()
  +
</pre>
  +
or just:
  +
<pre>
  +
au FileType gitcommit au! BufEnter COMMIT_EDITMSG call setpos('.', [0, 1, 1, 0])
  +
</pre>

Latest revision as of 10:25, 22 September 2012

Tip 1636 Printable Monobook Previous Next

created 2009 · complexity basic · author Khym Chanur · version 7.0


When editing a git commit message (.git/COMMIT_EDITMSG) you often won't start on the first line due to Vim remembering your last position in that file. An easy way to fix this is to add the following line to your vimrc:

autocmd FileType gitcommit call setpos('.', [0, 1, 1, 0])

Alternatively, you can create a file named gitcommit.vim in ~/.vim/ftplugin containing the following line:

call setpos('.', [0, 1, 1, 0])

Comments[]

We have two other short git tips:

Why is this tip needed when it is so easy to type gg to go to the first line? JohnBeckett 05:01, May 15, 2010 (UTC)

Probably because muscle-memory might lead someone to immediately try to insert text, without looking? ~Anon, 2012-08-21 8:47-0700


Also, you should hook other event, because FileType is too early, and cursor position will be overwritten using info from .viminfo:

function MyBufEnter()
  " don't (re)store filepos for git commit message files
  if &filetype == "gitcommit"
    call setpos('.', [0, 1, 1, 0])
  endif
endfunction
au BufEnter * call MyBufEnter()

or just:

au FileType gitcommit au! BufEnter COMMIT_EDITMSG call setpos('.', [0, 1, 1, 0])