Vim Tips Wiki
(Change to TipImported template + severe manual clean)
m (Reverted edits by 96.232.165.38 (talk | block) to last version by 188.192.143.15)
(9 intermediate revisions by 7 users not shown)
Line 1: Line 1:
{{review}}
 
 
{{TipImported
 
{{TipImported
 
|id=803
 
|id=803
 
|previous=802
 
|previous=802
 
|next=804
 
|next=804
|created=October 6, 2004
+
|created=2004
 
|complexity=basic
 
|complexity=basic
|author=Anonymous
+
|author=
 
|version=6.0
 
|version=6.0
 
|rating=7/26
 
|rating=7/26
  +
|category1=
  +
|category2=
 
}}
 
}}
In order to save a file you'll have to come out of the insert mode, commonly by hitting the Esc key. Once you are up there to hit the Esc key, why not make it save the file too with a few more hits.
+
To save a file, you would normally first leave insert mode by hitting the Esc key one or more times. Then you type the following (and press Enter):
  +
<pre>
  +
:w
  +
</pre>
   
This can be done by simply adding the following line in the vimrc file:
+
You can save your file (if modified) and exit Vim at the same time with:
  +
<pre>
  +
:x
  +
</pre>
   
  +
You can save all modified buffers (all open files) with:
 
<pre>
 
<pre>
  +
:wa
map &lt;Esc&gt;&lt;Esc&gt; :w &lt;CR&gt;
 
 
</pre>
 
</pre>
   
  +
For a tweak, you can set Vim to automatically save the current buffer when you hit Esc twice. This can be done by simply adding the following line to your vimrc:
Note: Mapping with a single Esc is somehow causing problems with the Up/Down/Left/Right arrow keys.
 
  +
<pre>
  +
map <Esc><Esc> :w<CR>
  +
</pre>
  +
  +
Note: In a console, mapping with a single Esc interferes with the Up/Down/Left/Right arrow keys, since these are actually escape sequences and send an Esc followed by a key id. (The reason that hitting Esc in Vim does not always give an immediate response is that Vim first waits a little to see if a key id is following the Esc.)
   
 
==Comments==
 
==Comments==
 
This would also work.
 
This would also work.
 
 
<pre>
 
<pre>
ino &lt;leader&gt;:w &lt;esc&gt;:w&lt;cr&gt;a
+
ino <Leader>:w <Esc>:w<CR>a
 
</pre>
 
</pre>
   
 
----
 
----
 
I use the following:
 
I use the following:
 
 
<pre>
 
<pre>
nmap &lt;c-s&gt; :w&lt;CR&gt;
+
nmap <c-s> :w<CR>
vmap &lt;c-s&gt; &lt;esc&gt;&lt;c-s&gt;gv
+
vmap <c-s> <Esc><c-s>gv
imap &lt;c-s&gt; &lt;esc&gt;&lt;c-s&gt;
+
imap <c-s> <Esc><c-s>
   
nmap &lt;f2&gt; :update&lt;cr&gt;
+
nmap <F2> :update<CR>
vmap &lt;f2&gt; &lt;esc&gt;&lt;f2&gt;gv
+
vmap <F2> <Esc><F2>gv
imap &lt;f2&gt; &lt;c-o&gt;&lt;f2&gt;
+
imap <F2> <c-o><F2>
 
</pre>
 
</pre>
   
Line 45: Line 56:
   
 
----
 
----
Under UNIX/Linux, in a console or terminal, CTRL-S sends the signal SIGSTOP, which stops the process in foreground until SIGCONT is sent; this is done by CTRL-Q. I'm not sure whether there exists a workaround, because the system catches the keys before Vim gets them.
+
Under UNIX/Linux, in a console or terminal, CTRL-S sends the signal SIGSTOP, which stops the process in foreground until SIGCONT is sent; this is done by CTRL-Q. This behaviour can usually be disabled by executing <pre>stty -ixon</pre> (add it to your shell's profile or rc file so it will be executed whenever you log in).
 
----
 

Revision as of 14:36, 29 July 2015

Tip 803 Printable Monobook Previous Next

created 2004 · complexity basic · version 6.0


To save a file, you would normally first leave insert mode by hitting the Esc key one or more times. Then you type the following (and press Enter):

:w

You can save your file (if modified) and exit Vim at the same time with:

:x

You can save all modified buffers (all open files) with:

:wa

For a tweak, you can set Vim to automatically save the current buffer when you hit Esc twice. This can be done by simply adding the following line to your vimrc:

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

Note: In a console, mapping with a single Esc interferes with the Up/Down/Left/Right arrow keys, since these are actually escape sequences and send an Esc followed by a key id. (The reason that hitting Esc in Vim does not always give an immediate response is that Vim first waits a little to see if a key id is following the Esc.)

Comments

This would also work.

ino <Leader>:w <Esc>:w<CR>a

I use the following:

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>

Control-S always saves the file, and, if called from visual mode, restores the visual selection when done. It does not re-enter insert mode, though, so I use it as my quit-insert-mode-and-save macro.

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


Under UNIX/Linux, in a console or terminal, CTRL-S sends the signal SIGSTOP, which stops the process in foreground until SIGCONT is sent; this is done by CTRL-Q. This behaviour can usually be disabled by executing

stty -ixon

(add it to your shell's profile or rc file so it will be executed whenever you log in).