1

I have the following one-line ~/.vimrc file:

Line 1/1: nnoremap <silent> <ESC> :nohlsearch<CR><ESC>

Whenever I edit a new document, now, I am placed immediately in Replace mode after opening the file (vim file.txt opens the file in Vim placing me directly in replace mode). Does anyone know what might be happening (I have included my Vim version below for more information).

VIM - Vi IMproved 7.4 (2013 Aug 10, compiled Jan  2 2014 19:39:32)
Included patches: 1-52
Modified by [email protected]. Compiled by buildd@

Huge version without GUI.  Features included (+) or not (-):

+acl             +farsi           +mouse_netterm   +syntax
+arabic          +file_in_path    +mouse_sgr       +tag_binary
+autocmd         +find_in_path    -mouse_sysmouse  +tag_old_static
-balloon_eval    +float           +mouse_urxvt     -tag_any_white
-browse          +folding         +mouse_xterm     -tcl
++builtin_terms  -footer          +multi_byte      +terminfo
+byte_offset     +fork()          +multi_lang      +termresponse
+cindent         +gettext         -mzscheme        +textobjects
-clientserver    -hangul_input    +netbeans_intg   +title
-clipboard       +iconv           +path_extra      -toolbar
+cmdline_compl   +insert_expand   -perl            +user_commands
+cmdline_hist    +jumplist        +persistent_undo +vertsplit
+cmdline_info    +keymap          +postscript      +virtualedit
+comments        +langmap         +printer         +visual
+conceal         +libcall         +profile         +visualextra
+cryptv          +linebreak       +python          +viminfo
+cscope          +lispindent      -python3         +vreplace
+cursorbind      +listcmds        +quickfix        +wildignore
+cursorshape     +localmap        +reltime         +wildmenu
+dialog_con      -lua             +rightleft       +windows
+diff            +menu            -ruby            +writebackup
+digraphs        +mksession       +scrollbind      -X11
-dnd             +modify_fname    +signs           -xfontset
-ebcdic          +mouse           +smartindent     -xim
+emacs_tags      -mouseshape      -sniff           -xsmp
+eval            +mouse_dec       +startuptime     -xterm_clipboard
+ex_extra        +mouse_gpm       +statusline      -xterm_save
+extra_search    -mouse_jsbterm   -sun_workshop    -xpm
system vimrc file: "$VIM/vimrc"
    user vimrc file: "$HOME/.vimrc"
2nd user vimrc file: "~/.vim/vimrc"
    user exrc file: "$HOME/.exrc"
fall-back for $VIM: "/usr/share/vim"
Compilation: gcc -c -I. -Iproto -DHAVE_CONFIG_H     -g -O2 -fstack-protector --param=ssp-buffer-size=4 -Wformat -Werror=format-security -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=1      
Linking: gcc   -Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,--as-needed -o vim        -lm -ltinfo -lnsl  -lselinux  -lacl -lattr -lgpm -ldl    -L/usr/lib/python2.7/config-x86_64-linux-gnu -lpython2.7 -lpthread -ldl -lutil -lm -Xlinker -export-dynamic -Wl,-O1 -Wl,-Bsymbolic-functions `
2
  • 1
    I remember seeing a related comment here: github.com/tpope/vim-pathogen/issues/102#issuecomment-45413916. Maybe you can just use the InsertLeave autocommand instead of that mapping?
    – akshay
    May 13, 2015 at 2:53
  • 1
    vim communicates the the terminal using escape sequences. Mapping escape breaks those escape sequences. Solution. Don't map escape.
    – FDinoff
    May 13, 2015 at 5:18

1 Answer 1

4

Mapping anything to Esc is a really bad idea. I suggest someyhing like this instead:

nnoremap <M-\>  :set hlsearch!<CR><Bar>:echo 'hlsearch: ' . {0:'Off', 1:'On'}[&hlsearch]<CR>

Here Meta-\ toggles hlsearch.

5
  • I feel like {0:'Off', 1:'On'}[&hlsearch] reads better than strpart
    – FDinoff
    May 13, 2015 at 5:41
  • Although you do realize that nohlsearch does something different from set nohlsearch?
    – FDinoff
    May 13, 2015 at 5:44
  • @FDinoff: To be honest, I copied that part from somewhere else many years ago, without giving it much though. But you're right, of course; I'll edit my answer.
    – lcd047
    May 13, 2015 at 5:47
  • @FDinoff: "nohlsearch does something different from set nohlsearch" - Yes, but I think toggling hlsearch is far more useful.
    – lcd047
    May 13, 2015 at 5:51
  • You guys were totally right. Mapping to escape is a bad idea. I ended up changing the command to nnoremap <silent> <Leader>` :let @/=""<CR>. This clears the search register - it does not set the search register to an empty string as that would match everywhere. Doing this sets up the search register in the same state as when vim starts (I like that). I got the idea from here. I'm sorry I didn't see it there first. I did look around before I posted (posting takes up my time, too). Thanks very much, all. May 27, 2015 at 16:41

Your Answer

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

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