0

Having used text editors (mainly Notepad/++) and some IDEs on MS Windows, I've become familiar with their Alt and Ctrl shortcuts to the point that I use them automatically. Having used nano on a GNU/Linux machine for many years, I tend to automatically use its Ctrl key shortcuts (particularly ^O and ^X as nano represents them) when editing text in either nano or insert mode in vim. Consequently, text I type in vim tends to be littered with ^S and ^O, instead of actually saving (writing out the buffer to file) at that point. (It seems to me that <C-s> currently isn't mapped to anything, despite the fact that some terminals use it for a special purpose.)

To stop that happening, I'd like to create the following mappings in ~/.vimrc, but my efforts so far result in vim issuing a warning about not finding a mapping (because I'm clearly doing it wrong):

  • <C-s> to map to "in INSERT MODE, press <ESC> to enter NORMAL MODE; type ':w';<Enter> and return to INSERT MODE to append text"
  • <C-o> (letter O, not zero) to do the same. (It seems vim has already mapped this to <Esc>.)
  • If I can't use <C-s> or <C-o>, then use <F2>.
  • <Alt-X> (or <Alt-F>X) to map to "In INSERT MODE, press <ESC> to enter NORMAL MODE; type ':q';<Enter>"
  • <C-x> to do the same as above.

What I have is this: imap <C-s>=<C-[>:w<Enter>a (or inoremap <C-S> <Esc>:w<CR>a)

Even if I precede <C-[> with <C-V>, pressing <C-s> results in ^S being typed instead of saving the file.

I have read the Mapping Keys in Vim tutorial on Vim Tips, but I haven't gleaned anything helpful from it. (I'm not sure whether I need to include the =, type or press instructions.) I've yet to try the solution proposed to this question that seems to describe the issue I'm facing (because it's not clear if it resolves the issue).

I don't want to read two books (one on vim itself and another on VimScript) just to be able to create shortcuts for common commands that require a few more keystrokes. (The irony of typing all this here instead of just getting on with using colon commands is not lost on me.)

In case it makes a difference, I'm using vim 8.2.1847 on Ubuntu 22.04 running through WSL.

6
  • 1
    Here we go: github.com/vim/vim/blob/master/runtime/mswin.vim But it is rather much more beneficial to start using Vim native keys instead of trying to force the existing MS-Windows keybindings to Vim. Commented Nov 9, 2023 at 13:26
  • Thank vou. In an ideal world, I would start using Vim from scratch, free from all the bad habits I've picked up by using Windows since I was old enough to sit up and type my own name. However, old habits die hard. While doing things the right way is always harder and/or takes longer, I've worked as a SW and Web developer for many years, so I'm lazy AF (one of the great virtues, although I'm not so good with the other two) and will go for the easy route if there is one. Commented Nov 9, 2023 at 13:33
  • A similar question (OP was coming from VS Code) was asked a while ago and I wrote a rather lengthy answer devoid of solid advice. You may or may not find it useful: vi.stackexchange.com/questions/41902/…
    – Friedrich
    Commented Nov 9, 2023 at 13:39
  • 1
    Or you could use Vim how it is supposed to be used. Laziness is not a valid excuse for anything.
    – romainl
    Commented Nov 9, 2023 at 13:40
  • FTR, I think learning Vim properly is the lazy (and best) route. Some fancy mappings may help short-term but a few weeks in, they will get in your way and hamper your productivity. The earlier you drop the dead weight, the better. And no, you don't need to read two books, vimtutor should be enough to get you started.
    – Friedrich
    Commented Nov 9, 2023 at 13:48

1 Answer 1

-1

Thanks to @ChristianBrabant for directing me to the mswin.vim file in the GitHub repo for vim, I found that for which I was looking and added it (slightly modified) to ~/.vimrc:

" Add key mappings for those familiar to MS Windows
" backspace in Visual mode deletes selection
vnoremap <BS> d

if has("clipboard")
    " In visual mode, CTRL-X is Cut. In normal mode, it's exit/quit.
    vnoremap <C-X> "+x
    noremap <C-X> :q<CR>
    noremap <F4> :q<CR>

    " CTRL-C is Copy
    vnoremap <C-C> "+y

    " CTRL-V and SHIFT-Insert are Paste
    map <C-V> "+gP
    map <S-Insert> "+gP
    cmap <C-V> <C-R>+
    cmap <S-Insert> <C-R>+
endif

" Use CTRL-S for saving, also in Insert mode (<C-O> doesn't work well when
" using completions).
noremap <C-S> :update<CR>
noremap <F2> :update<CR>
vnoremap <C-S> <C-C>:update<CR>
vnoremap <F2> <C-C>:update<CR>
inoremap <C-S> <Esc>:update<CR>gi
inoremap <F2> <Esc>:update<CR>gi

There's a lot more in that file, but the above is the code I want.

2
  • 1
    The file mswin.vim comes with Vim. Read :help mswin.vim and use it the way it's described there: source $VIMRUNTIME/mswin.vim. That's the only line you need in your vimrc.
    – Friedrich
    Commented Nov 9, 2023 at 14:09
  • Perhaps, but I don't necessarily want all the other stuff that's there and I'm not going to fork the project just to make a slight customisation to one file (which I'll then have to maintain separately from upstream). I'd probably end up overriding it in ~/.vimrc anyway. Commented Nov 9, 2023 at 14:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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