简体   繁体   中英

Simple vim keymapping not working

When I use map qq :wq in my .vimrc, I see "recording" in the status bar rather than seeing vim save and close the file. How can I achieve this mapping?

Rose, I believe your mapping doesn't work because qq command records typed characters into q register and it can't be remapped. Here is the excerpt from the help:

q{0-9a-zA-Z"}  Record typed characters into register {0-9a-zA-Z"}
               (uppercase to append).  The 'q' command is disabled
               while executing a register, and it doesn't work inside
               a mapping and |:normal|.  {Vi: no recording} 

Actually, I would recommend you to use other mappings instead. Here is what you can use, for example:

1) ZZ for writing current file, if modified, and quit. Also ZQ can be used to quit without checking for changes.

2) The following mapping:

noremap <silent> <Leader>x :x<CR>

here is what help says about :x command:

:[range]x[it][!] [++opt] [file]
            Like ":wq", but write only when changes have been
            made.
            When 'hidden' is set and there are more windows, the
            current buffer becomes hidden, after writing the file.

Additionally, probably you will find these mappings helpful if you need to have separate mappings for :w and :q! commands:

noremap <silent> <Leader>w :w<CR>
noremap <silent> <Leader>q :q!<CR>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM