Remap ⌘S to save a file in VIM - vim

I have recently switched from using MacVim to just using Vim on the command line. One feature I miss is being able to save a file with ⌘S rather than having to go back into normal mode and then :wq
I am using Vim inside iTerm2. Is there any way to get ⌘S to save a file both in insert and normal mode?

You can use iTerm2 to map Cmd-S to some other combination that the shell and Vim will recognize (e.g. Ctrl+S) , then you can simply map that command via a .vimrc file as Andrew pointed out.
In summary:
edit iTerm2 Keys with a new entry that maps Cmd-S to Ctrl+S.
Add commands for mapping Ctrl+S to vimrc file (like Andrew describes) or like this site.

Here is an even simpler way to do it: using iTerm2's "Send Text". Basically you can map Cmd-S to send a text to the current vim session :w\n.

Well, I'm not sure that command-line Vim can register the ⌘ key, and I'm not on my Mac to confirm, but you can map ⌘S in .vimrc like this.
inoremap <D-s> <ESC>:w<CR>i "insertmode
nnoremap <D-s> :w<CR> "normalmode
Again, this will only work if command-line vim can recognize the ⌘key.

It is not possible to map to ⌘ in the console version of Vim. If you want to be able to use ⌘ in a mapping you'll need to switch back to MacVim.

Related

Vim using <c-6> to switch alternative file is not functional on Mac terminal

Vim using <c-6> to switch alternative file is not functional on Mac terminal while MacVim works.
The current version of Vim is 7.3.
To switch to the alternate file you can use control-shift-6 (<C-^>) or :e #. Both of these work in the Terminal.
Take a look at :h CTRL-^
It's not possible to map <C-#> in terminal mode. It's a limitation of terminals that they can't handle most Ctrl key combinations. The only ones that work are A-Z and just a couple others. Similarly, you can't detect combinations like Ctrl+Shift+a; they just come through as <C-a>.
You'll either have to remap the key to something else, or configure your terminal emulator to send a different key combination when you press that. Either way, you'd have to configure your Vim to accept something other than <C-6> when using a terminal.

Key mapping confusion in VIM,How to make vim works in a smart way?

I want to bind Ctrl-w with :q command in vim That is to say, I want to use Ctrl-w to close the current window of vim,
So I add this to my ~/.vimrc :
nmap <C-W> :q<cr>
When I use this to close a window that has something unsaved,this command cannot work.
So I want to make it works in a smart way: When the content is saved, just close the window. When the content remains unsaved, ask me whether to close the window directly like this command
:0,$s/a/b/gc
I don't know if this is clear enough for you, but thanks in advance.
There's the :confirm command for that. Just put it in front of :quit:
:nmap <C-W> :confirm q<cr>
The simplest thing to do would be to set confirm in your vimrc - this will prompt you before closing without saving (and a few other things, too; see :help confirm).
Otherwise, you could write a little vim script which uses &modified and confirm() (see :help confirm()).

Mapping Alt+Tab in Vim

In Vim, I have mapped F9 key to :w<CR>. After pressign F9, I usually switch back to firefox to see the result. is it possible to map a key to Alt+Tab so I can save the file and switch, with pressing a single key? for example nmap :w<Cr><M-Tab>
Vim handles vim keybindings, not system keybindings. So in short, no you can't do that. However, you can use something else to accomplish this. For example xdg-open on Linux or open on Mac are the standard "use the default program to open this" commands. You could use this to open the current file (or a link) in your web browser.
nnoremap <F9> :w<CR>:!open %<CR>
or
nnoremap <F9> :w<CR>:!open http://mysite.com<CR>
If you're on a mac you could use some AppleScript via osascript to simply "activate" Firefox if you don't want to open a new window. I'm sure there's something similar for Linux it's just been a couple years since I've had to do that.

vim mapping key does not work

I have been trying to map ctrl s to save during insert mode in vim. It never seem to work
http://vim.wikia.com/wiki/Map_Ctrl-S_to_save_current_or_new_files (and many other tutorials) does not seem to work
I am using iterm, vim 7, and I have .vimrc file under my current user. I have reloaded my .vimrc with :source $MYVIMRC
Did I do something wrong?
Same thing with mapping any other key. mapping just does not seem to work.
Many keys cannot be intercepted on terminal, because they are interpreted by the terminal and not passed to vim at all. Ctrl-S is one of those keys, meaning "suspend" to most terminals. Either use the GUI version of vim, or select key combinations that work on your terminal.

mapping already exists for ^I in vim

I have added the following lines to the vimrc file so that i could use s-tab for indenting
map <esc>[Z <s-tab>
ounmap <esc>[Z
After i have added this , i get the following error. I use SnippetsEmu plugin
Error detected while processing function <SNR>15_SnipMapKeys:
line 10:
E227: mapping already exists for ^I
How do i solve this conflict.Why does this happen
A helpful way you can detect whether your version of vim is capable of differentiating between Tab and Shift-Tab is to:
Go into insert mode, press Ctrl-v* then Shift-Tab. I get <S-Tab> when I do that (gvim 7.2 on Windows XP).
If you don't get that then I don't think you can map Shift-Tab separately from Tab with your current setup.
*Ctrl-v will take the next key combination you press and output the key combination that vim actually sees.
EDIT: If you're sourced mswin.vim then you'll want to use Ctrl-q instead.

Resources