0

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 Perrone
  • 61,572
  • 58
  • 208
  • 243
  • Note: I suspect you want to do `map qq :wq` instead, as this will actually run the command. – Matt Bryant Jan 01 '14 at 19:49
  • Nope, that didn't help. – Rose Perrone Jan 01 '14 at 19:57
  • 3
    Your command as given works on my box (Vim 7.4.131), so I'm not sure what's wrong. Is there a reason you aren't using ZZ? – Matt Bryant Jan 01 '14 at 20:01
  • I didn't know about that command, thanks. Is there a similar command for save-without-exit? – Rose Perrone Jan 01 '14 at 20:03
  • @RosePerrone are you sure that your vimrc is being loaded? – Explosion Pills Jan 01 '14 at 20:26
  • @RosePerrone save and exit: `ZZ`, exit without save: `ZQ`, did you try open your vim, and type your mapping command in vim? by `:map ....`? – Kent Jan 01 '14 at 21:00
  • @Kent The command didn't work when I ran the map command in vim. I want to create a shortcut for save without exit, not exit without save. – Rose Perrone Jan 01 '14 at 21:13
  • @RosePerrone the `:map...` line should work anyway, there must be some information you forgot telling here. and it is not very clear, what does `it didn't work` mean. can your open your vim, and test this two lines, 1) `:map qq :wq` then press Enter, after that, 2) `:map qq` report the output pls. To save the current buffer (don't exit): `:w` – Kent Jan 01 '14 at 21:18
  • Like others have said above, there's some level of miscommunication here. You need to type out all of `map qq :wq` instead of pressing Enter after `:wq` in the `.vimrc`. It's also advisable to use `nnoremap` instead of `map` in this case. Personally I map `nnoremap w :w` to satisfy my neurotic saving needs. I'm trying to wean myself off from using it so much though. – Conner Jan 01 '14 at 21:41
  • @Kent the output is ` qq :wq`, but still when I hit `qq`, I see `recording`. – Rose Perrone Jan 01 '14 at 21:46
  • `:wq` is "save and exit" and `qq` is quite possibly one of the most useful Vim shortcut, I'm not sure you *want* to shadow it. If you want "save without exit" you want `:w`. Anyway, `:map qq :wq` works perfectly. – romainl Jan 01 '14 at 21:54
  • Can you open Vim without your vimrc (`vim -u NONE`), apply just that mapping manually, and see if it works? [I agree with romainl, though -- I use `qq` very frequently to record a one-off macro. Very handy.] – Pandu Jan 02 '14 at 08:46
  • @pandubear When I do that, I type in qq, and `:wq` appears in the command bar, and typing characters appends to that message, but the action doesn't execute. The file is not saved and closed. – Rose Perrone Jan 02 '14 at 16:26

1 Answers1

0

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>
  • Thanks, I'll accept this answer, but it looks like I need to uninstall and reinstall plugins to get vim to properly recognize my keybindings again, according to http://stackoverflow.com/questions/11560294/vim-leader-not-working. – Rose Perrone Jan 05 '14 at 03:31
  • Probably, you don't need to reinstall plugins. Try just clear the content of ~/.viminfo file and if you're using a session plugin - clear the content of a created session file too. Then you need to restart Vim. – Konstantin D. Jan 05 '14 at 08:58
  • No luck. Vim refuses to recognize my mapleader, whether it's the default `\` or I include `let mapleader = ","`. Either way, when I hit the key in command mode, the "you made an error with your keyboard" alarm goes off and nothing happens. – Rose Perrone Jan 05 '14 at 17:34
  • Hmm, it's pretty strange. What `:echo mapleader` command prints for you? Does it print your mapleader key? Also, you can try to run Vim without plugins `vim --noplugin`. If your mapleader works in that mode, try to disable all your plugins and enable them one by one. So you will find a plugin that caused the problem. – Konstantin D. Jan 05 '14 at 18:00
  • Even when I run `vim` with `vim --noplugin`, when I execute `:echo mapleader`, I see: `E121: Undefined variable: mapleader`, and on a secod line: `E15: Invalid expression: mapleader`. – Rose Perrone Jan 12 '14 at 01:06
  • Well, I think it's time to reinstall Vim and all plugins. – Konstantin D. Jan 12 '14 at 10:46