Hot answers tagged

40

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 several windows for the current file, the file is written if it was modified and the window is closed). :wq on the other hand writes the file and quits Vim (...


36

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 can create the directory there if it doesn't exist yet. For example: augroup Mkdir autocmd! autocmd BufWritePre * \ if !isdirectory(expand("<afile>...


32

You could save the file under a different name using :w. 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. :saveas saves your new file and opens it in a new buffer. But it doesn't delete the old file. I use tpope/vim-eneuch to :Move files. :Move: Rename a buffer and ...


30

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 event. This event is triggered when the user hasn't pressed a key for 'updatetime' milliseconds. autocmd CursorHold * update The :update command only saves ...


24

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 & stdout; % expands to the current filename.. You prefix this with sudo for root permissions. You're not really saving the file with Vim, rather, you're ...


19

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 Vim, :wq # inode changed! $ ls -i a 3155322 a crontab gets confused by this (see :help crontab) You need to use :set backupcopy to yes to make Vim overwrite ...


17

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 switch your buffer to that other file. So if you keep editing and do just :w, that will save to the current file, not to the other one. To switch to the other ...


15

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 be replaced with :up when you press return.


14

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.


14

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 more information about this here. In short you don't have to worry about the absence a new lines at the end of the file in vim. Experiment 1 You can do this ...


12

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


12

You can set the file to read only and not modifiable :set readonly :set nomodifiable For more help see: :h 'readonly' :h 'modifiable'


11

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 read more on that at Why should files end with a newline? Unfortunately, it's quite complex to prevent Vim from writing the final newline. My PreserveNoEOL ...


11

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 work. If Windows itself or an administrator has set it read-only, you will need another option. As @dash-tom-bang commented, one good example is within ...


10

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 the file. There's a caveat though: the original buffer remains in the list of buffers. If you switch to it, it's empty. If the file you're editing is in a ...


10

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, or your session were to die, then you can reconnect, and recover from essentially where you left off, by typing: vim -r MyFile.txt This will open the file for ...


10

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> in Insert mode or when using the "o" or "O" command). If you do not type anything on the new line except <BS> or CTRL-D and then type ...


10

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 buffer with a name that ends in .less is written. The command that is being run is a bash command, and <afile> is the name of the file that is being ...


9

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 working directory you will find a swap file. Copy it to a different directory (where you have no "notes.txt" file, as say vim notest.txt. You get a note about ...


9

Instead of using the BufWritePre autocmd, you can use the BufWriteCmd autocmd, from :help BufWriteCmd: Should do the writing of the file and reset 'modified' if successful, unless '+' is in 'cpo' and writing to another file cpo-+. We can just call :write from the autocmd. This will take care of setting modified. So the logic would then be modified to ...


9

You may try the following 3 commands: :redir > filelist.txt | :ls | :redir END redi[r][!] > {file} Redirect messages to file {file}. The messages which are the output of commands are written to that file, until redirection ends. To stop the messages and commands from being echoed to the ...


9

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 file, even if it is unchanged. In both cases, the contents of the buffer will be saved to disk. Obviously the outcome is the same, so why bother, right? But wait....


9

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 file, which is equivalent to vim -R: view filename.txt Note that the readonly option doesn't prevent a forced write. If you use :w! the file will be written ...


9

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 lines) exe "" execute the following command .w! save the current line and overwrite if already exists. (Remove ! if you don't want to auto-overwrite everything) "...


8

As Peter Rincker points out, cmaps can expand in other places as well, so a cnoreabbrev would be better: cnoreabbrev w' w Or, the safest, again thanks to Peter: cnoreabbrev <expr> w' getcmdtype() == ":" && getcmdline() == "w'" ? "w" : "w'" By explicitly checking if the command line contains only w', unwanted expansions in situations can be ...


8

I made a plugin vim-autosave which uses Vim 8 feature of Timers to periodically save your buffers.


8

Vim only omits the EOL if the file is opened as 'binary' and the 'endofline' option is reset (when the binary file didn't have an EOL when opening, or you explicitly reset the option). In other words, Vim only honors a missing EOL for binary files. For text files, it always writes with an (added) EOL. (That's why you need solutions like my PreserveNoEOL ...


8

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: http://vim.wikia.com/wiki/Do_not_auto-add_a_newline_at_EOF) In your ~/.vimrc add this line: set nofixeol Relaunch vim, now it shouldn't add the newline at ...


7

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 you give, you could do this without moving the cursor, by putting the cursor on the first square brace, and then typing v%"zy. Breakdown: v -> start visual ...


6

I've been using the Rename2 plugin for this for years. It renames both the current buffer, and the file on disk: :Rename {newname} EDIT: I found this a .vimrc file on github: function! RenameFile() let old_name = expand('%') let new_name = input('New file name: ', expand('%'), 'file') if new_name != '' && new_name != old_name ...


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