I am considering attaching a macro to the space bar to insert a space.

My question is whether this will change its normal mode behavior. For example, if I type 23<space> will it execute the macro 23 times or do the normal thing (move forward 23 spaces)?

I want to keep the old functionality for repetitive actions because the space navigation goes past end of line but 'L' navigation does not.

Just to clarify, this is the behavior I am envisioning:

  • typing "400< space bar >" moves forward 400 characters including past end of line
  • hitting the bar inserts one space in normal mode, ie:

    :nnoremap <space> i<space><esc>

Not sure if it possible to achieve both these objectives. If it is not possible, then maybe the best option is to change the normal space bar behavior to a different key, since I very rarely use the space bar for navigation. Maybe Shift+Spacebar for movement?

  • 1
    I'm not sure I understand what you mean by "attaching a macro to the space bar". A macro can be recorded in the registers {0-9a-zA-Z"} not in space. To change the behavior of the space bar in insert mode you can remap it with inoremap <Space> [...] is that what you are talking about? If so the answer is inoremap will change the behavior of the space bar only in insert mode, while nnoremap will change the behavior in normal mode.
    – statox
    Mar 23, 2016 at 15:20
  • @statox Yes, that is what I am talking about, but I would want the mapping to work in command mode, so maybe it is not feasible without breaking the normal functionality. Mar 23, 2016 at 15:24
  • 1
    When you say you want the mapping to work in command mode, you mean when typing :echo (or any other command) and then pressing Space you want the space bar to do something else than inserting only one space?
    – statox
    Mar 23, 2016 at 15:27
  • @statox I meant normal mode, I updated my answer. Mar 23, 2016 at 16:03
  • 1
    Ok it's more clear now. I think you can't get two different behaviors in the same mode. But a simple solution would be to use another mapping like < C-Space> for example.
    – statox
    Mar 23, 2016 at 16:12

2 Answers 2


Using a function, you can split the functionality of a key map. In this case, the determining factor is if a count is given to the command or not, which is stored in v:count.

function! SpaceOrSpace(cnt)
  if a:cnt > 0
    exec "normal " . a:cnt . "l"
    normal i ^[

nnoremap <silent> <Space> :<C-U>call SpaceOrSpace(v:count)<CR>

Note the ^[ character in the script is created by pressing CTRL+v CTRL+[

The function is pretty simple. If the count is greater than zero move the cursor right as many times. Otherwise insert a space and return to normal mode.


I'm under the impression that a macro on space would become an annoyance very quickly. (assuming a macro is external to vi bindings) I had tried something like that with a fn+key on my keyboard and found that it could be solved with some vimL

From the question it sounds like you are looking for a way to navigate in normal (having cursor wrap lines). I'd recommend staying within vim to accomplish your goal.

The first thing I would look at is other events that are triggered when you navigate to the end of the line using hjkl. On my config I hear the annoying bell which leads me to think there must be some event associated with cursor movement (assuming knowledge of autocmds). After running :help autocmd-events I stumbled on the following


After the cursor was moved in Normal or Visual mode. Also when the text of the cursor line has been changed, e.g., with "x", "rx" or "p". Not triggered when there is typeahead or when an operator is pending. For an example see |match-parens|. Careful: This is triggered very often, don't do anything that the user does not expect or that is slow.

Knowing that we have an event to drill into should give enough information to get started. If in doubt I'd head to google with that event in hand + site:github.com.

(let me know if you'd like a more complete walkthrough of a solution [can make an attempt when I get some coffee in the system])

  • :set whichwrap+=l
    – Rich
    Apr 25 at 15:26

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.