9

Occasionally, my pinky will graze the ' key while reaching for enter, resulting in :w'<Enter>.

I've tried:

cabbrev w' :w
cabbrev w\' :w
cabbrev "w'" :w

None of them work. Is there a way to alias :w' to :w?

I also use cmdwin (:help cmdwin + nnoremap : :<C-F>) instead of the normal command-line, so bonus points if it works there too.

8

As Peter Rincker points out, cmaps can expand in other places as well, so a cnoreabbrev would be better:

cnoreabbrev w' w

Or, the safest, again thanks to Peter:

cnoreabbrev <expr> w' getcmdtype() == ":" && getcmdline() == "w'" ? "w" : "w'"

By explicitly checking if the command line contains only w', unwanted expansions in situations can be avoided.

You can use a cmap:

cmap w' w

You'd still have to press Enter, but an accidental ' should be ignored now.

If you're using cmdwin, a inoremap set by autocmd might be useful:

autocmd CmdwinEnter * inoremap w' w
autocmd CmdwinLeave * iunmap w'
  • I ran :cmap w' w, but there was no change in behavior. :w' still results in writing to a file named '. "'" [New] 0L, 0C written. – mwcz Feb 19 '15 at 22:39
  • @mwcz Does the ' actually show up when you press it after w? It shouldn't if the cmap was successfully created - a cmap LHS is automatically filled in with the RHS, so if you press a ', it should still only show w. – muru Feb 19 '15 at 22:48
  • 1
    Be careful with bare cmap's and cabbrev's. They will execute/expand in other modes and other places. e.g. :s/'follow'/foo and /'follow'. Please see the following vim change :x function to delete buffer instead of save & quit or use cmdalias.vim – Peter Rincker Feb 19 '15 at 23:20
  • 1
    Better safe than sorry: cnoreabbrev <expr> w' getcmdtype() == ":" && getcmdline() == "w'" ? "w" : "w'" – Peter Rincker Feb 19 '15 at 23:27
  • Yes, the ' still shows up, but I just figured out it's because I'm using cmdwin to enter commands, and cmdwin is just a regular buffer, so cmap/cnoreabbrev don't work there. Updating the answer with noreabbrev option for cmdwin users. :) Thanks!!! – mwcz Feb 19 '15 at 23:42
0

This method works for me:

:ca w' w

Alternatively you can set up your vim to save the current buffer when you hit Esc twice, e.g.

:map <Esc><Esc> :w<CR>

Or use the following mapping to save file either by Ctrl+s or F2:

nmap <c-s> :w<CR>
vmap <c-s> <Esc><c-s>gv
imap <c-s> <Esc><c-s>

nmap <F2> :update<CR>
vmap <F2> <Esc><F2>gv
imap <F2> <c-o><F2>

Note: F2 only saves if necessary, and returns the user to insert mode (or restores their visual selection), as needed.

Source: Saving a file at vim wikia

Or use the following alternatives:

  • :up (:update) for :w
  • ZQ for :q
  • ZZ for :wq or :x
  • Notice the quotation mark in the question. :w', not :w. I want to use :w without accidentally hitting ', which writes a file I must then rm \'. Good tips, though. – mwcz Feb 20 '15 at 0:09
  • 1
    Ctrl-S doesn't send a sigstop - if the program stops, it's probably because a buffer was full. See unix.stackexchange.com/questions/13629/…' – muru Feb 20 '15 at 0:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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