Let's say I'm a terrible person and I'm thinking about remapping <CR> to do what : normally does (start command-line mode).
Vim's built-in netrw plugin also remaps <CR> to open the selected directory or file. Specifically, it appears to map it to
:call netrw#LocalBrowseCheck(<SID>NetrwBrowseChgDir(1, <SID>NetrwGetWord()))`)<CR>
The use of <SID> makes it impractical for me to simply reproduce this map in my .vimrc, which I would like to do, because I would like to
autocmd FileType netrw nnoremap <buffer> <CR> :
in order to access command-line mode consistently. What might be an effective way to move netrw's default <CR> functionality to another keystroke so I can still open files with it?
My experiments thus far have led to my trying to use maparg to do something like
function! s:ConfigureNetrw()
execute "nnoremap <buffer> o " . maparg("<CR>", "n")
nnoremap <buffer> <CR> :
endfunction
augroup configure_netrw
autocmd!
autocmd FileType netrw call s:ConfigureNetrw()
augroup end
However this seems to result in both <CR> and o doing the same thing when I check the output of nmap (they both get mapped to :). I could understand how this might happen if I used nmap, since the maparg result has a <CR> in it. But it confuses me how it doesn't seem to matter if I use nmap or nnoremap; the same thing happens.
EDIT: After a lot of frustrated poking around I have narrowed my apparent problem to this:
let g:prior = maparg("<CR>", "n")
nmap <buffer> <CR> -
I put this in ~/.vim/after/ftplugin/netrw.vim. I would expect that getting into a Netrw buffer and doing :echo g:prior would print the LocalBrowseCheck text that netrw maps <CR> to. Instead it prints -.
This happens if I put the same two lines in a function called from an autocmd, as I was originally doing. It happens if use nnoremap instead of nmap. It does not happen if I change the filename or autocmd so it associates with a different filetype (say, text); in that it prints what <CR> is originally bound to (:). I am now completely confused.
s:ConfigureNetrwI edited into my question (and eventually exactly those two lines in my latest edit, and described permutations). – Aug 27 '15 at 20:00<CR>in netrw starts command-line mode and:verbose map <CR>shows that mapping in that file. I have no idea what else you expect. – romainl Aug 27 '15 at 20:16execute "nnoremap <buffer> o " . maparg("<CR>", "n")? – romainl Aug 27 '15 at 20:25netrw's keybindings also since I use a customIJKLarrows instead of the usualHJKLones. The suggestion above didn't work at first, then I double checked in the documentation and it turned that I have a typo in my plugin folder, I've created~/.vim/after/ftpplugin/netrw.viminstead of~/.vim/after/ftplugin/netrw.vim.ftpluginfolder name is kinda unexpected though. Anyway, it's working now as intended.So, the suggested fix is indeed correct, and could use
– Ranel Padon Feb 28 '21 at 00:46nnoremapalso aside fromnmap.