Cursor isn't at right position for Commit message

153 views
Skip to first unread message

Kaartic Sivaraam

unread,
Jun 6, 2017, 12:46:12 AM6/6/17
to v...@vim.org
Hello all,

I am currently using Vim 8.0. When I use vim as my preferred editor for
git. 

When I try to commit a change in git it open up vim to type the commit
message. Most of the time the text cursor seems to be positioned
somewhere in the middle of the commit template (the comments). As a
result, I am unable to type in the commit message directly after
switching to "insert" mode in vim. Any reasons for this misbehaviour ?

Environments:
Operating System: Debian GNU/Linux 9 (stretch)
Shell: bash

--
Regards,
Kaartic Sivaraam <kaarticsiv...@gmail.com>

Eric Christopherson

unread,
Jun 6, 2017, 1:18:00 AM6/6/17
to vim...@googlegroups.com
Do you have an autocmd to resume the last position in a file when
opening that file later? I had one like that (I forget where I copied it
from), which I eventually modified to exclude gitcommit files. See the
solutions at
https://stackoverflow.com/questions/2393671/vim-restores-cursor-position-exclude-special-files
.

>
> --
> --
> You received this message from the "vim_use" maillist.
> Do not top-post! Type your reply below the text you are replying to.
> For more information, visit http://www.vim.org/maillist.php
>
> ---
> You received this message because you are subscribed to the Google Groups "vim_use" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+u...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

--
Eric Christopherson

Christian Brabandt

unread,
Jun 6, 2017, 2:37:24 AM6/6/17
to v...@vim.org

On So, 04 Jun 2017, Kaartic Sivaraam wrote:

> Hello all,
>
> I am currently using Vim 8.0. When I use vim as my preferred editor for
> git. 
>
> When I try to commit a change in git it open up vim to type the commit
> message. Most of the time the text cursor seems to be positioned
> somewhere in the middle of the commit template (the comments). As a
> result, I am unable to type in the commit message directly after
> switching to "insert" mode in vim. Any reasons for this misbehaviour ?
>
> Environments:
> Operating System: Debian GNU/Linux 9 (stretch)
> Shell: bash

It could be, that your system sources defaults.vim which sets up the
last-position-jump (see :h last-position-jump) autocommand.

Read :h defaults.vim how to prevent loading it (it is usually only
loaded, when no vimrc has been found)

Best,
Christian
--
In jedem Falle wird bei gleicher Anlage das falsche Vertrauen auf
Talente mehr hervorbringen als das falsche Mißtrauen in sie; jenes
spannt, dieses lähmt.
-- Jean Paul

Christian Brabandt

unread,
Jun 6, 2017, 3:19:18 AM6/6/17
to vim...@googlegroups.com, Bram Moolenaar

On So, 04 Jun 2017, Kaartic Sivaraam wrote:

> Hello all,
>
> I am currently using Vim 8.0. When I use vim as my preferred editor for
> git. 
>
> When I try to commit a change in git it open up vim to type the commit
> message. Most of the time the text cursor seems to be positioned
> somewhere in the middle of the commit template (the comments). As a
> result, I am unable to type in the commit message directly after
> switching to "insert" mode in vim. Any reasons for this misbehaviour ?
>
> Environments:
> Operating System: Debian GNU/Linux 9 (stretch)
> Shell: bash

Perhaps, we should disable the last-position jump for commit messages in
general. Here is a patch:

diff --git a/runtime/defaults.vim b/runtime/defaults.vim
index 6fd43db51..fdac666e6 100644
--- a/runtime/defaults.vim
+++ b/runtime/defaults.vim
@@ -109,7 +109,7 @@ if has("autocmd")
" Don't do it when the position is invalid or when inside an event handler
" (happens when dropping a file on gvim).
autocmd BufReadPost *
- \ if line("'\"") >= 1 && line("'\"") <= line("$") |
+ \ if line("'\"") >= 1 && line("'\"") <= line("$") && &ft !~# 'commit' |
\ exe "normal! g`\"" |
\ endif

diff --git a/runtime/doc/eval.txt b/runtime/doc/eval.txt
index a0cc971fe..b4b6bbfef 100644
--- a/runtime/doc/eval.txt
+++ b/runtime/doc/eval.txt
@@ -5460,7 +5460,8 @@ line({expr}) The result is a Number, which is the line number of the file
< *last-position-jump*
This autocommand jumps to the last known position in a file
just after opening it, if the '" mark is set: >
- :au BufReadPost * if line("'\"") > 1 && line("'\"") <= line("$") | exe "normal! g`\"" | endif
+ :au BufReadPost * if line("'\"") > 1 && line("'\"") <= line("$") &&
+ \ &ft !~# 'commit' | exe "normal! g`\"" | endif

line2byte({lnum}) *line2byte()*
Return the byte count from the start of the buffer for line


Best,
Christian
--
In feinen Gesellschaften wird nur der abwesende persifliert, in
gemeinen spaßet man über d(en) gegenwärtigen.
-- Jean Paul

Bram Moolenaar

unread,
Jun 6, 2017, 4:25:32 PM6/6/17
to Christian Brabandt, vim...@googlegroups.com
I suppose that this is better in most cases. There might be some other
filetypes that only apply to temp files.

--
hundred-and-one symptoms of being an internet addict:
14. You start introducing yourself as "Jim at I-I-Net dot net dot au"

/// Bram Moolenaar -- Br...@Moolenaar.net -- http://www.Moolenaar.net \\\
/// sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
\\\ an exciting new programming language -- http://www.Zimbu.org ///
\\\ help me help AIDS victims -- http://ICCF-Holland.org ///

Ken Takata

unread,
Jun 8, 2017, 11:27:13 PM6/8/17
to vim_use, Br...@moolenaar.net
Hi,

I write the following setting in my .vimrc to avoid the problem:

augroup vimrc-git
autocmd!
autocmd BufRead {COMMIT_EDITMSG,MERGE_MSG,git-rebase-todo,NOTES_EDITMSG}
\ delmarks \" | 1
augroup END


Another option is changing the setting of Git to always open the first line:

$ git config --global core.editor "vim +1"


Regards,
Ken Takata

Reply all
Reply to author
Forward
0 new messages