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!
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.
noremap <Esc> <Nop>
line. Don't know why, though.