LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Vim - saving from insert mode - no confirmation - help? (https://www.linuxquestions.org/questions/linux-software-2/vim-saving-from-insert-mode-no-confirmation-help-4175500435/)

petreza 04-03-2014 07:16 AM

Vim - saving from insert mode - no confirmation - help?
 
Hi,

I use the 'zz' key combination to save the file without exiting vim. To make it work in both normal and insert mode I use the following 2 lines in my .vimrc file:

Code:

imap zz <Esc>:w<CR>i<right>
nnoremap zz :w<CR>

When I press 'zz' in normal mode I get a message on the command bar that the file was saved. But in insert mode there is no way to tell if the save was really completed.

Is there some kind of a time delay function in vim so that I can have a line like this:

Code:

imap zz <Esc>:w<CR>:echo "Saved."<CR>  timedelay(500ms)  i<right>
Is there even a better a way to get similar effect - maybe some kind of popup in the middle of the screen?
(I am using vim in a terminal - not a GUI.)

bigrigdriver 04-03-2014 04:00 PM

In vim, you may also write without quiting with this command: :w!

When I used this option in a test, there was a statement displayed which gave the number of characters written to the file.

The vim manpage says this:
Code:

:w[rite]! [++opt]      Like ":write", but forcefully write when 'readonly' is
                        set or there is another reason why writing was
                        refused.
                        Note: This may change the permission and ownership of
                        the file and break (symbolic) links.  Add the 'W' flag
                        to 'cpoptions' to avoid this.

Use this method with care. Pay special attention to the Note:

Within a vim session, give the command ":help write" (without quotes) for more information.

haertig 04-03-2014 05:17 PM

Why would you want a key sequence like zz to work from within insert mode? Do you never expect to type "jazz" or "fuzz" and are OK with wiping them out of the dictionary now and forever?

Why not save without exiting in command mode by using :w like the majority of everyone else? And NOT try to save while in insert mode, like everyone else? There are already about ten bazillion alternate ways you can accomplish something in vi, why try to add even more non-standard ones?

That's like modifying your car so that pushing the accelerator to the floor actually applies the brakes. Because you never push the accellerator to the floor yourself, and desire an alternate and "more intuitive" (for you) way of applying the brakes. Then you get into somebody elses car that doesn't have your custom modifications and you are set up for a significant crash and burn.

jthill 04-03-2014 06:09 PM

The default status line shows "[+]" on the for modified buffers, or you could keep a running changes-since-saved count displayed using the `statusline` option, the `changedtick` variable and a `bufwrite` autocommand to track its value at the last save.

petreza 04-04-2014 01:02 AM

Quote:

Originally Posted by bigrigdriver (Post 5146153)
In vim, you may also write without quiting with this command: :w!

When I used this option in a test, there was a statement displayed which gave the number of characters written to the file.

The vim manpage says this:
Code:

:w[rite]! [++opt]      Like ":write", but forcefully write when 'readonly' is
                        set or there is another reason why writing was
                        refused.
                        Note: This may change the permission and ownership of
                        the file and break (symbolic) links.  Add the 'W' flag
                        to 'cpoptions' to avoid this.

Use this method with care. Pay special attention to the Note:

Within a vim session, give the command ":help write" (without quotes) for more information.

Thanks, but yes, I wouldn't want to use it to overwrite any 'readonly' file. Plus it does not really solve the problem - I tried it on a test file and after the saving operation I still only get '-- INSERT --' at the bottom.

petreza 04-04-2014 01:29 AM

Quote:

Originally Posted by haertig (Post 5146189)
Why would you want a key sequence like zz to work from within insert mode? Do you never expect to type "jazz" or "fuzz" and are OK with wiping them out of the dictionary now and forever?

Why not save without exiting in command mode by using :w like the majority of everyone else? And NOT try to save while in insert mode, like everyone else? There are already about ten bazillion alternate ways you can accomplish something in vi, why try to add even more non-standard ones?

That's like modifying your car so that pushing the accelerator to the floor actually applies the brakes. Because you never push the accellerator to the floor yourself, and desire an alternate and "more intuitive" (for you) way of applying the brakes. Then you get into somebody elses car that doesn't have your custom modifications and you are set up for a significant crash and burn.

Hi, thanks for your reply but I think any less-typing is good, especially for something like saving that happens so often. That's why I also have <Esc> mapped to 'jj' which is faster even if it is more typing, and even 'ZZ' to save-and-exit in insert mode. If I want to actually type 'zz', 'jj', and 'ZZ', I just have to wait a bit longer between the two presses of the letter (half a second or so) so that Vim stops expecting a command. Also there is a slight visual jerk when typing the letters 'j' 'z' and 'Z' as the cursor does not automatically advance for about a half a second or until I type the next letter but I got used to it and usually just blast through typing.

Imagine I am editing an HTML document. As soon as I am done I am pressing 'zz' with my right hand (Dvorak) while my left is reaching for the Alt-Tab to go to the browser to refresh. Total 2 "movements" - I count 'zz' as one and Alt-Tab as one. Compare that to 'jj'+:+w+<CR>+Alt-Tab = 5 by the same standard plus I have to press 'i' for insert mode when I get back = 6.

You are right, though, that if I move to someone else's computer I would be typing junk.

petreza 04-04-2014 01:38 AM

Quote:

Originally Posted by jthill (Post 5146205)
The default status line shows "[+]" on the for modified buffers, or you could keep a running changes-since-saved count displayed using the `statusline` option, the `changedtick` variable and a `bufwrite` autocommand to track its value at the last save.

Thanks! I started reading on these but it quickly became apparent that they are over my head.


All times are GMT -5. The time now is 08:56 AM.