Tuesday, February 23, 2021

Re: mapping :W to :w...

On Wed, Feb 24, 2021 at 5:06 AM <tuxic@posteo.de> wrote:
>
> Hi,
>
> if my PC is heavily compiling things and I am editing and saving
> things repeatedly if often hit :W instead of :w (that is, I am
> still pressing SHIFT when hitting w.
>
> So I tried to map :W to :w...but it does not work for me.
>
> I tried
>
> [n]map W w
> and
> [n]map :W :w
>
> .
>
> Is there any way to map the accidentally hit :W
> command to :w somehow?
>
> Cheers!
> mcc

Like Salman said, you could define a user-comand W executing just w ;
but the reason your nmap didn't work is that by the time you'tr typing
the W, you aren't in Normal mode anymore, but in Command-line mode;
OTOH you don't want any W in the middle of a command's parameters to
always lowercased; so another possibility is a command-mode
abbreviation, as follows:

:cabbrev <expr> W ((getcmdtype() == ':' && getcmdpos() <= 4)? 'w' : 'W')

Testing getcmdtype() avoids lowercasing the W in a search command, and
testing getcmdpos() allows doing it only near the left margin. A
user-command is more elegant however; but in order to make it behave
like the w command you may want to define that :W command with the
appropriate parameters, as follows (untested):

:command -nargs=* -complete=file -range=% -bang -bar W
<line1>,<line2>w<bang> <args>

which, after all, makes it somewhat less elegant; so you may choose a
command or an abbrev to suit youor taste.

Best regards,
Tony.

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/CAJkCKXvzVvZZaZ8vEY9grSZNrO_soPwE0oGczrVy1yHF8_m0pA%40mail.gmail.com.

No comments:

Post a Comment