Hot answers tagged

54 votes
Accepted

What are the differences between :wq<cr> :x<cr> and ZZ when exiting vim?

From Vim's documentation, :x and ZZ are equivalent; they only save the file if it has been modified, then quit Vim: Write current file, if modified, and quit (same as ":x"). (Note: If there are ...
akshay's user avatar
  • 6,397
29 votes

What are the differences between :wq<cr> :x<cr> and ZZ when exiting vim?

As akshay pointed out, Vim's documentation explains, that :x and ZZ are equivalent and only save a file if the associated buffer has been changed. Whereas :wq saves the buffer to the corresponding ...
m4110c's user avatar
  • 431
25 votes

Don't add new line at the end of a file

In new versions of Vim there's finally an option for this Vim 7.4.785 adds the 'fixeol' option that can be disabled to automatically preserve any missing EOL at the end of the file. (see wiki page:...
DarthVanger's user avatar
20 votes
Accepted

How to map a shortcut for saving the file

First of all stop using :w to save. Use :update instead. :update writes the file to disk only when there are changes. So, it could be really helpful especially if the file is huge. Now to map <C-S&...
Durga Swaroop's user avatar
17 votes

How to clang-format the current buffer on save?

You can add the following to your .vimrc 1. function FormatBuffer() 2. if &modified && !empty(findfile('.clang-format', expand('%:p:h') . ';')) 3. let cursor_pos = getpos('.') 4. ...
PragmaticProgrammer's user avatar
15 votes
Accepted

Why can't I write to a file in the home directory?

The tilde is only treated specially if it's used in commands like :w ~/event.log. When used in a string that is passed to a function -- even if this function treats the string argument as a file name -...
Jürgen Krämer's user avatar
13 votes

How do I save a file in a directory that does not yet exist?

Another way with a vanilla Vim (without extra conf or plugins). in Vim: :!mkdir -p /folder/you/want/ :w #save file or $ vim /folder/you/want/myfile.conf $ ctrl+z # switch to the terminal then ...
xaa's user avatar
  • 231
13 votes
Accepted

Write selected area to file

First, copy (also called 'yank' in vim parlance) your visual selection to the register of your choice. For example, to copy to register 'z', make your visual selection, and then type "zy. In the case ...
muru's user avatar
  • 24.7k
12 votes

How do I save a file in a directory that does not yet exist?

I can recommend a vim plugin from Tim Pope called vim-eunuch which defines many extremely useful commands when your work on UNIX/Linux with Vim (check out its features!). Let's say you open vim with ...
cbaumhardt's user avatar
  • 2,089
12 votes
Accepted

How to make sure no changes are made to a file

You can set the file to read only and not modifiable :set readonly :set nomodifiable For more help see: :h 'readonly' :h 'modifiable'
Peter Rincker's user avatar
12 votes
Accepted

Vim Error for Git Commit Message

It is possible that you have EDITOR left unset or to its default value of vi. Try making vim your editor. Although both vi and vim are the same executable in mac, I guess, vim behaves in a minimalist ...
Lakshman Kumar's user avatar
11 votes
Accepted

How do I save a read-only file in Vim for Windows?

As on other vi/vim versions, you can use :w! to force the write, as long as you have permissions to override the read-only property. For example, if you have set the file as read-only, the above will ...
rolinger's user avatar
  • 226
11 votes
Accepted

How to write each line into separate file?

Try a global command: :g/^/exe ".w! line".line('.').".txt" :g/^/ Do a command for every line (you can adjust this regular expression if you only want to save certain lines, i.e. . for non-empty ...
Tumbler41's user avatar
  • 7,706
10 votes

Is it possible to make Vim auto-save files?

I made a plugin vim-autosave which uses Vim 8 feature of Timers to periodically save your buffers.
Christian Brabandt's user avatar
10 votes

Why is vim save so slow?

The culprit in this particular situation was the Syntastic plugin. It's forking eslint. I load Syntastic on entering insert mode (using a vim-plug function loader). Vim is plenty snappy on its own....
Steven Lu's user avatar
  • 2,231
9 votes

How to make sure no changes are made to a file

The easiest way is to open the file using the -R flag when opening the file to set the readonly option. vim -R filename.txt vi -R filename.txt For Vim, you can use the command view when opening a ...
Wildcard's user avatar
  • 4,364
8 votes

How to show all unsaved files opened in vim?

:ls shows the current buffers (opened files) A + mark before file path shows that file is modified. Check :h :ls for more details
NikitQa's user avatar
  • 131
8 votes

How to write a value or variable into a file using only vimscript?

writefile() is the ticket, but has some quirks you have to account for, such as splitting on newlines. An example: call writefile(split(varname, "\n", 1), glob('/path/to/file'), 'b')
Steven Lu's user avatar
  • 2,231
7 votes

Don't add new line at the end of a file

What eventually worked for me was: vim -b <filename> Then in vim: :set noeol :wq Credit
Tom Hale's user avatar
  • 2,601
7 votes
Accepted

Is there any vim plugin that implement hot exit feature from sublime text

Vim can do this, though it may be a bit clunky and take some setup. romainl gets you partway with session. The missing bits are swap files, :preserve, and the & flag for cpoptions. Swap files ...
8bittree's user avatar
  • 1,487
7 votes
Accepted

How to auto save file when moving between split?

What you are looking for is a way to trigger the autocommand each time you leave a buffer to another location. Conveniently the autocommand event BufLeave was made precisely for that, so you just ...
statox's user avatar
  • 49.4k
7 votes
Accepted

Saving file from INSERT mode

Yes, just use the <C-O> keystroke to temporarily leave insert mode for a single command. You can then run :w<CR> to write the file and you'll be back to insert mode. You can use that to ...
filbranden's user avatar
  • 28.4k
7 votes
Accepted

Is there a way to write a file and skip the autocmd on write?

Generally speaking, that's exactly what the :noautocmd command modifier is for. Just prefix your command with that and all autocommands should be disabled for just the run of that command. :noautocmd ...
B Layer's user avatar
  • 19.6k
7 votes

How to explain ++opt in :[range]w[rite] [++opt] !{cmd}?

You can actually find more about that under :help ++opt, which lists the 5 or 6 possible keywords that can be used under that setting. The available settings typically control special file attributes, ...
filbranden's user avatar
  • 28.4k
6 votes

Why does `set nocompatible` result in vim saving extra, all-numeric temporary files?

When writing a buffer Vim will create a temporary file which it will fchown() to verify it has permission to create files in this directory; simplified version from buf_write() in src/fileio.c: /* * ...
Martin Tournoij's user avatar
6 votes

How do you disable saving in vim unless you quit as well?

You could play with the write option. From :h 'write': 'write' boolean (default on) global {not in Vi} Allows writing files. When not set, writing a file is not ...
statox's user avatar
  • 49.4k
6 votes
Accepted

What is the difference between :file and :saveas

:saveas does write to files, after all the help says: "Save the current buffer under the name {file}". Note also how it says that ! is needed to overwrite an existing file. You wouldn't need to talk ...
muru's user avatar
  • 24.7k
6 votes

Vim rewrite whole file on save?

Instead of :write you should use :update. According to vim's help: *:up* *:update* :[range]up[date][!] [++opt] [>>] [file] Like ":write", but only write ...
Mass's user avatar
  • 14k
6 votes
Accepted

Is it possible to have vim run a command every time save `:w` is invoked?

Does something like this exist? In Vim this is called an autocommand. The relevant help topic can be found by typing :h autocommand (sic!) An example code to put into vimrc: augroup typescript_save ...
Matt's user avatar
  • 20.4k
6 votes

Vim not firing inotify events when writing file

Which inotify events are fired depends on how Vim writes the file. This is controlled by multiple options. In a typical setup it works like this. Assume Vim wants to write the file e.txt Vim moves ...
Ralf's user avatar
  • 9,097

Only top scored, non community-wiki answers of a minimum length are eligible