Soumen
Soumen

Reputation: 1078

TAB not working when changing CTRL+S to save file in vim

I tried to save a file in vim by using CTRL+S. I came across this link http://vim.wikia.com/wiki/Map_Ctrl-S_to_save_current_or_new_files and according to it added these pieces of codes in .basrc and vimrc respectively:

vim()
{
local STTYOPTS="$(stty --save)"
stty stop '' -ixoff
command vim "$@"
stty "$STTYOPTS"
}

and

nmap <C-s> :wq!<cr>

Ok now CTRL+S saves the file. But TAB doesn't work now in insert mode. When I press TAB, cursor returns to first column of current line!! Any solutions?

Upvotes: 0

Views: 311

Answers (1)

Ingo Karkat
Ingo Karkat

Reputation: 172540

:nmap only defines the mapping in normal mode. For insert mode, use :imap (in general, prefer :inoremap unless you really need remapping to take place), and first leave insert mode by prepending <C-o> (for a single command) or <Esc> (more appropriate here, since you want to leave the buffer with :quit, anyway).

:inoremap <C-s> <C-o>:wq!<cr>

BTW, I find it interesting that you also want to quit the buffer. For me, the mapping is helpful because I can quickly type it in the middle of editing, so that I can frequently persist the changes.

" Use CTRL-S for saving, also in Insert mode
:nnoremap <C-S>     :<C-U>update<CR>
:vnoremap <C-S>     :<C-U>update<CR>gv
:cnoremap <C-S>     <C-C>:update<CR>
:inoremap <C-S>     <C-O>:update<CR>

Upvotes: 2

Related Questions