2

Unfortunately nnoremap ab gd: doesn't work on my machine.

When I hit ab, the gd: remap selects the words under the cursor as gd (or gD) should do but it doesn't enter into command mode after as : usually does. If I type the sequence gd: orgD: directly in neovim, it works fine and enters into command mode after selecting the words under the cursor.

When I replace gd with gg, nnoremap ab gg: works fine. It moves to the beginning and enters command mode. Any ideas why gg: works while gd: doesn't?

My goal is to make a map such as:

nnoremap gR gD:%s/<C-R>///gc<left><left><left>

for global search and replace

I am using:

$ vim --version

NVIM v0.2.2
Build type: RelWithDebInfo
LuaJIT 2.0.4
Compilation: /usr/bin/x86_64-linux-gnu-gcc -g -O2 -fPIE -fstack-protector-    strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -DDISABLE_LOG -Wdate-time -D_FORTIFY_SOURCE=2 -Wconversion -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=1 -DNVIM_UNIBI_HAS_VAR_FROM -O2 -g -DMIN_LOG_LEVEL=3 -Og -g -Wall -Wextra -pedantic -Wno-unused-parameter -Wstrict-prototypes -std=gnu99 -Wvla -fstack-protector-strong -fdiagnostics-color=auto -DINCLUDE_GENERATED_DECLARATIONS -D_GNU_SOURCE -I/build/neovim-HJnLxJ/neovim-0.2.2/build/config -I/build/neovim-HJnLxJ/neovim-0.2.2/src -I/usr/include -I/usr/include -I/usr/include -I/usr/include -I/usr/include -I/usr/include -I/usr/include -I/build/neovim-HJnLxJ/neovim-0.2.2/build/src/nvim/auto     -I/build/neovim-HJnLxJ/neovim-0.2.2/build/include
Compiled by pkg-vim-maintainers@lists.alioth.debian.org

Features: +acl +iconv +jemalloc +tui 
See ":help feature-compile"

   system vimrc file: "$VIM/sysinit.vim"
  fall-back for $VIM: "/usr/share/nvim"

Run :checkhealth for more info

My full plugin free ~/.config/nvim/init.vim looks like this (where ab doesn't work and ba does work):

set guicursor=
nnoremap ab gD:
nnoremap ba gg:

Update

nnoremap ab gD:

gD means 'go to definition'. This fails when this command is executed at the defintion itself. It works only when: the selected word is not the first occurence.

A simple workaround is to use a search with * instead of 'go to definition' nnoremap ab *:. search and replace would then be:

nnoremap gR *:%s/<C-R>///gc<left><left><left>
1

Not sure what's going wrong with your map. It works for me (but I don't use Neovim).

However, rather than relying the side effects of gd or * (that they set the search register "/), you could instead insert the word into the search command more directly:

nnoremap gR :%s/\V\<<C-R><C-W>\>//gc<left><left><left>

N.B. I'm using very-nomagic \V in the regular expression for consistency with your original command, gd.

This utilises the command-line editing command Ctrl-RCtrl-W, which inserts the word currently under the cursor into the command-line. (See :help c_CTRL-R_CTRL-W)

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.