6

How to inspect the cursor position in Vim command line (Cmdline mode) from Vim script?

I would like to configure my Command-line mode key bindings in Vim to behave like in tcsh. In particular, i would like Ctrl-d to delete one character after the cursor except at the end of the command line, and to list all possible completions otherwise (what Ctrl-d does in Cmdline mode by default). The first thing that comes to mind is to make some mapping similar to the following one:

cnoremap <expr> <C-d> (col('.')==col('$')?'<C-d>':'<Del>')

Of course, this one does not do what i want because it is the cursor position in the text that is being inspected, not the cursor position in the command line.

So, what Vim functions can inspect the cursor position in Vim command line?

I would also be interested to know how to move the cursor in the command line and how to modify the command line from Vim script.

  • 3
    See :help getcmdpos() and the topics referred to there. – garyjohn Oct 30 '15 at 1:30
  • 2
    Thanks, this help. I have ended up with the mapping cnoremap <expr> <C-d> (getcmdpos()==len(getcmdline())+1?'<C-d>':'<Del>'). – Alexey Oct 30 '15 at 9:52
3

After a hint to use getcmdpos(), i have ended up with the mapping

cnoremap <expr> <C-d> (getcmdpos()==len(getcmdline())+1?'<C-d>':'<Del>')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.