3

I have the following line in my config:

map <cr> G

So I can type 123<cr> and go to the line 123, but I don't really want to go to the end of file if I did not type any numbers. How do I map it so it only works with numbers typed ahead?

2
  • 1
    Just to clarify, you want <CR> with no count to behave the same as <CR> with no mapping?
    – DJMcMayhem
    Commented Sep 29, 2016 at 2:24
  • I the case it's just about the end of the file, you also have gg which will move you to the beginning of the file without any count. Commented Sep 29, 2016 at 8:42

1 Answer 1

8

You can use an expression mapping. From :h <expr>

                        *:map-<expr>* *:map-expression*
If the first argument to one of these commands is "<expr>" and it is used to
define a new mapping or abbreviation, the argument is an expression.  The
expression is evaluated to obtain the {rhs} that is used.  Example: >
    :inoremap <expr> . InsertDot()
The result of the InsertDot() function will be inserted.  It could check the
text before the cursor and start omni completion when some condition is met.

So the mapping you are looking for is

nnoremap <expr> <cr> v:count == 0 ? "\<cr>" : "G"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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