This question already has an answer here:

I've been reading through Learn Vimscript the Hard Way and trying some exercises. When I got into the autocommands/autogroups part, I've thought of creating an autocommand to automatically trim trailing white spaces from my C files. So I created the appropriate section on my .vimrc file and tested with a simple test.c program. The behavior is that when I write the file, the trailing spaces get deleted correctly. But if I write and quit Vim with ZZ or :wq, and then open the file again with vim test.c to check the results, the next key press deletes some characters. With I press j, it will delete to the next line, if I press k to the previous one. It's like I've just pressed d and I'm in operator pending mode for deletion.

What am I doing wrong?

My ~/.vimrc:

set number relativenumber
set hlsearch incsearch nowrapscan nowrap
set showcmd
set wildmenu wildignorecase complete+=d
set path+=**
set sidescroll=1 sidescrolloff=10

nnoremap <Leader>ev :split $MYVIMRC<CR>
nnoremap <Leader>sv :source $MYVIMRC<CR>
inoremap jk <Esc>
map <Leader>y "+y

noremap <Esc> <Nop>

augroup Ccfg
    autocmd FileType c setlocal listchars=trail:.,tab:>-,extends:>,precedes:< list
    autocmd FileType c setlocal tabstop=4 shiftwidth=4 shiftround expandtab
    autocmd FileType c setlocal textwidth=80 colorcolumn=+1
    autocmd FileType c setlocal tagcase=match
    autocmd FileType c setlocal cscopetag
    autocmd FileType c setlocal breakindent
    autocmd FileType c iabbrev /*-*/ /*----------------------------------------------------------------------------*/
    autocmd BufWrite *.c,*.cpp,*.h %s/\v\s+$//e
augroup END

My test.c file:

#include <stdio.h>

int main(void)
    printf("Hello world!\n");    
    return 0;

Notice the trailing spaces after the printf call.

I'm trying to find a way to record my terminal session, but since I'm on Windows using Git Bash and it's Vim, I can't use asciinema or other solutions.

marked as duplicate by Rich, jamessan, Herb Wolfe, Martin Tournoij Jul 30 '18 at 23:51

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • I've found out by trying different configurations that the culprit is the noremap <Esc> <Nop> line. Don't know why, though. – Spidey Jul 29 '18 at 13:46
  • 1
    thats a weird mapping that is known to break some things. Ill look for a reference; in the meantime, you could self-answer if removing that line fixed your problem? – D. Ben Knoble Jul 29 '18 at 14:13
  • Here it is : stackoverflow — basically its because of the way terminal emulators process certain things. – D. Ben Knoble Jul 29 '18 at 14:18

I've found out by trying different configurations that the culprit is the noremap <Esc> <Nop> line.

Thanks to @D. Ben Knoble for finding Why does the combination of remapping to cause strange characters to be automatically inserted when starting vim with startinsert? that explains that Esc is part of many special key combinations and remapping it to Nop interfere with those.

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