Last call to make your voice heard! Our 2022 Developer Survey closes in less than a week. Take survey.

Hot answers tagged

63 votes
Accepted

How can I rename the file I'm editing?

You could save the file under a different name using :w newname. But this operation won't remove the old file and the 'old' file stays in your buffer, so any changes will be applied to your 'old' file....
user avatar
  • 3,226
48 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 ...
user avatar
  • 5,766
46 votes
Accepted

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

As far as I know there is no setting or some such to do this. But not all is lost, we can of course use the BufWritePre autocommand. This is executed before the buffer is written to the disk. So we ...
user avatar
38 votes
Accepted

Is it possible to make Vim auto-save files?

There isn't a builtin option, although :help 'autosave' (in todo.txt) refers to a description of what such an option could do. One possible way to implement this is to use the CursorHold autocommand ...
user avatar
  • 10.3k
24 votes
Accepted

How to save a file for which I have no write permissions?

The trick is to use an external call to sudo: :w !sudo tee % How this works: :w !<command> executes <command> with the contents of the buffer as stdin. tee duplicates stdin to a file &...
user avatar
23 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 ...
user avatar
  • 361
21 votes
Accepted

How to make vim automatically add a newline to the end of a file?

Basic info Vim doesn't show latest newline in the buffer but actually vim always place EOL at the end of the file when you write it, because it standard for text files in Unix systems. You can find ...
user avatar
21 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:...
user avatar
20 votes
Accepted

How do you save the file that you're working on as a new file?

You can give a parameter to the :w or :write command to save to a different file. For example to save the current buffer to /tmp/data.txt: :w /tmp/data.txt However, keep in mind that this does not ...
user avatar
  • 2,618
19 votes
Accepted

How do I edit crontab files with Vim (I get the error: 'temp file must be edited in place')

The problem is how Vim writes files. By default, it makes a copy of the file and overwrite the original one. You can observe this with: # Show the file's inode $ ls -i a 3156153 a # Open file in ...
user avatar
17 votes

How can I rename the file I'm editing?

You could drop to Netrw and rename the file there. If the file you're editing is in the current directory, then do: :edit . Navigate to the file, press R, and change the name. Press Enter to edit ...
user avatar
  • 2,618
16 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&...
user avatar
15 votes
Accepted

Don't touch file upon write if the file was not changed

Extending @lcd047's answer you can use cabbrev to modify w :cabbrev w <c-r>=(getcmdtype()==':' && getcmdpos()==1 ? 'up' : 'w')<CR> When you type :w you will notice that it will ...
user avatar
  • 2,500
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 -...
user avatar
14 votes

Don't touch file upon write if the file was not changed

Not directly (that is, the behaviour of :w can't be changed), but you can learn to use :up instead of :w. It does exactly what you want.
user avatar
  • 3,568
14 votes
Accepted

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

This isn't a full empty line, just a final newline at the end of the last line. Unix tools (like Vim's heritage) insist on adding that, whereas the Windows operating system is not so strict. You can ...
user avatar
  • 16.6k
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 ...
user avatar
  • 23.7k
13 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. ...
user avatar
12 votes

Is it possible to make Vim auto-save files?

Vim does have an auto-save feature, but it saves it to the "recovery" file. When you edit a document, e.g. MyFile.txt vim will create the revovery file .MyFile.txt.swp. If your system were to crash, ...
user avatar
  • 677
12 votes

How to save a file for which I have no write permissions?

I use the following mapping in my .vimrc, which I find useful: cnoremap w!! w !sudo tee % It's easy to remember, because w is "write," w! is "force write," and w!! is "super-duper-force write." :P
user avatar
  • 13.6k
12 votes
Accepted

Run bash command on writing a file with a particular extension?

You can use autocommands to run things automatically when certain events occur. In your case, you could do this: autocmd BufWritePost *.less !less <afile> This autocommand runs every time a ...
user avatar
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'
user avatar
11 votes
Accepted

Why does the cursor move to the start of the line after <Esc>?

Looking at the documentation for autoindent has an answer as to why and how to work around it. :help 'autoindent': Copy indent from current line when starting a new line (typing <CR> ...
user avatar
  • 7,992
11 votes
Accepted

Does Vim autosave?

Yes it saves to swap file. You can do a quick test to get a feel of this. Go to a directory. Say vim notest.txt and write some text. You do not even have to save the file. If you list files in the ...
user avatar
  • 1,207
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 ...
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 ...
user avatar
  • 7,406
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.
user avatar
10 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 ...
user avatar
  • 1,979
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....
user avatar
  • 2,081
10 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 ...
user avatar

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