Search code examples
vimtext-editorneovim

How can I make Neovim's 'e' map make an extra position?


I am trying to remap 'e' keybind with :nnoremap e mynewcommand, but I am a bit lost about how to make the current "end of word" finish one extra position.

For example, right now if I have word1 Xword2 word3 (X being the current position), it ends here: word1 wordX word3.

Is there a way to remap it so that it ends on end of word + 1 position? Like this: word1 word2X word3

Thanks!

Edit1: For some weird reason, when doing the remap: :nnoremap <S-l> e , works like a charm - but, the :nnoremap <S-j> b doesn't work at. Does anyone have any idea?

Edit2: As requested, here is my vim.init:

:nnoremap t r
:nnoremap <SPACE> <INSERT>
:vnoremap i <UP>
:vnoremap <SPACE> <INSERT>
:nnoremap i <UP>
:nnoremap k <DOWN>
:nnoremap j h
:nnoremap <S-l> e 
:nnoremap <S-j> b 
:nnoremap a <S-A>
:nnoremap vv <S-V>
:vnoremap k <DOWN>
:vnoremap j <LEFT>
:nnoremap dw daw
:inoremap jh <Esc>

" Settings.

set nocompatible
set showmatch
set ignorecase
set tabstop=4
set softtabstop=4
set expandtab
set shiftwidth=4
set autoindent
set number 
set clipboard=unnamedplus
set ttyfast

" Vim-plug (Plugin Manager)

call plug#begin()
Plug 'itchyny/lightline.vim'
Plug 'nvim-treesitter/nvim-treesitter', {'do': ':TSUpdate'}
call plug#end()

Solution

  • First: Vim (and Neovim) doesn't have "maps" so what you are doing are not "remaps", they are "mappings" (or "maps", if you really want).

    Second: as someone already pointed out, f<Space> already covers your need so you probably shouldn't spend so much energy on this.

    Third: <S-l> and <S-j> are convoluted and unnecessary ways to say L and J. Keep your config simple.

    Fourth: :help J and :help L are two very useful commands in their own right so you should think hard about the relative benefits of overriding them. Especially for something as trivial as this.

    Fifth: The in nnoremap <key> e is not very readable and is likely to disappear after some hasty formatting. You should consider these alternatives notations:

    nnoremap <key> e<Space>
    nnoremap <key> el
    nnoremap <key> e<Right>
    

    or maybe even:

    nnoremap <key> f<Space>
    

    Sixth: The way you explained your situation is not very clear:

    word1 Xword2 word3
    word1 wordX word3
    

    Where did the 2 go? Which character was the cursor on, exactly? Could you rewrite it in a more explicit way? Something like:

    Current behaviour:

    word1 word2 word3
         ^               " position of the cursor before e
              ^          " position of the cursor after e
    

    Desired behaviour:

    word1 word2 word3
         ^               " position of the cursor before <key>
               ^         " position of the cursor after <key>
    

    Seventh: I can, sort of, understand the desire for nnoremap L e<Space> because wanting the cursor to land right after the current word seems like a reasonable thing to want. You may want to put something, there, or insert a bracket, or whatever. But I fail to imagine a scenario common enough to justify nnoremap J b<Space>. What, exactly are you trying to do with that mapping? What problem are you trying to solve? And, more importantly, what does "doesn't work" mean?

    Are you trying to mirror your L mapping by landing the cursor before the word? Since <Space> moves the cursor to the right, then b<Space> is obviously not going to work but the following might:

    nnoremap <key> b<Left>
    nnoremap <key> bh
    nnoremap <key> b<BS>
    

    or maybe even:

    nnoremap <key> F<Space>