Hot answers tagged

46

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 (...


28

Having recently run into this problem (via another way: Vim running on a remote server, and I'd forgotten screen), I decided to hunt for a way. The first idea was to look up the file descriptors used by Vim and try writing to it. Vim's fds point to the psedoterminal opened by the terminal emulator, naturally enough: $ ls -l /proc/$(pgrep -n vim)/fd/ total ...


27

This happens when vim is invoked and it's connected to the previous pipeline's output, instead of the terminal and it's receiving different unexpected input (like NULs). The same happens when you run: vim < /dev/null, so reset command in this case helps. This is explained well by grawity at superuser. If you're using find to pass file names to edit, you ...


24

By default VIM, when terminating, sends the string configured with the option t_te to the hosting terminal to tell it to clear the screen. To avoid it just :set t_te= to send nothing to the terminal and avoid screen clearing. See :help term form more information about terminal capabilities.


22

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....


22

Try this: run vim file1 in a terminal, then run vim -p file1 file2 in a different terminal. The second command will prompt you as above. If you answer Quit, you still get to edit file2. If you answer Abort you just quit Vim, thus "aborting any further commands".


21

Use the command :qall!, :qa! for short, or its safer alternative :qall that prevent to discard modified buffers. To save all buffers before quitting use the command :wqall. See :help window-exit for the full set of commands to quit multiple windows at once.


17

A process can block, ignore, or catch signals. You can see how a given process handles signals with this shell command: $ cat /proc/PID/status | grep -E '^Sig(Blk|Ign|Cgt):' │ │ │ │ │ └ caught signals │ └ ignored signals ...


13

To solve this nuisance with netrw permanently, I added this to my .vimrc: " Per default, netrw leaves unmodified buffers open. This autocommand " deletes netrw's buffer once it's hidden (using ':q', for example) autocmd FileType netrw setl bufhidden=delete From Tim Pope.


13

You can send commands to vim externally if you're running... Vim servers For example, doing: vim --servername vim will cause vim to launch a server with the name "vim". Call it twice and the new server will be called "vim1", call it thrice and it will be "vim2", etc. You might want to make an alias of that command. You can know what server a particular ...


12

Workaround suggestion: use a buffer as a filesystem navigator Use the vim - command to read a list of paths from stdin. Vim's :help -- explains this:1 Start editing a new buffer, which is filled with text that is read from stdin. The commands that would normally be read from stdin will now be read from stderr. Example: find . -name "*.c" -print | ...


12

The option confirm does what you describe for modified files. With set confirm in my vimrc, it gives the following prompt when I :quit a modified NoName buffer: Save changes to "Untitled"? [Y]es, (N)o, Save (A)ll, (D)iscard All, (C)ancel: For me, if it's an unnamed buffer and you answer (Y)es to the prompt, it opens it. You could then :write filename, ...


11

Install the reptyr command using the system's package manager, such as: sudo apt install reptyr pacman -Sy reptyr Then use the reptyr command to switch the remote tty to the local (new) tty, as follows: ssh user@remote-hostname ps auxw | grep -i vim reptyr PID Where PID is the process ID from the ps command output. Upon the error: Unable to attach to ...


10

You can set a persistent undo with the following settings: set undodir=~/.vim/undodir set undofile This is avaible in Vim 7.3 an above


9

This usually happens, if there are two or more buffers which are modified and Vim then usually toggles between them and shows the error message. So when :q! would abort the current buffer, it wouldn't not abort the other modified buffer, so therefore Vim protects you from losing changes and gives this error message. If you are absolutely sure, you want to ...


8

Besides reset, you can try: stty sane which should also make your terminal usable again. See here for explanations. And somehow this can be considered a vim misbehavior, at least Neovim doesn't have this issue at the moment.


8

Running process in a terminal buffer is treated mostly the same way as "an unsaved file". So you can do :qa! or :set confirm etc.etc. Well, anything to allow exit from Vim with buffers unsaved. But, of course, that will modify Vim's behaviour with respect to all buffers, not only terminals. So if you only want to allow silent terminal closing, you should ...


7

From your post it sounds like you have a problem of exiting Vim. You mention a plugin that may help, but lets get to the root of the problem which is you have a created bad habit. As with all bad habits you need to break them. Maybe stick with using :w to write files and using :close/<c-w>c to close windows until you fully break your habit. You also ...


7

Update: I created a confirm_quit.vim plugin; it has some additional options, like only asking for confirmation when quiting the last buffer (rather than closing any buffer). I will leave the below code as-is because it demonstrates how you can achieve something like this in a simple way, but I recommend you use the code from the link :-) Update2: After re-...


6

You can change vim - to vim -R -. From the manual: Read-only mode. The 'readonly' option will be set. You can still edit the buffer, but will be prevented from accidently overwriting a file. If you do want to overwrite a file, add an exclamation mark to the Ex command, as in ":w!". The -R option also implies the -n option (see ...


5

Try :bdelete! or :bd! for short. This stands for "buffer delete" and will close the buffer you have open. (Credit goes to Emil Asmussen) After that, you should be able to do :q! as normal.


5

You are piping content into Vim, so it's starting a new unsaved buffer. If you don't need to save the content, you can add +'set buftype=nofile' to your command: find $HOME -type f -name "*.tex" -exec grep -il "$1" {} + | vim +'set buftype=nofile' - Vim will treat the buffer as if it has no associated file. Which would be good if you plan to use Vim as a ...


5

I agree with you, and this seems like it could be improved, because one of those you would think would not create the buffer, and I just verified that I see the same behavior you describe, but if I were in your shoes what I would do to guarantee the buffer is immediately destroyed and which comes close to automatic is [O]pen Read-Only followed immediately by ...


5

Since vipe, git commit (and many other programs which invoke an editor) use the VISUAL and EDITOR variables (unless you specify an editor for git with git config core.editor), you can use that variable to invoke Vim in such a way that you can detect it: export EDITOR='env called=1 vim' Then, in Vim, $called will have a value of 1, which you can use to ...


5

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 allowed. Can be used for a view-only mode, where modifications to the text are still allowed. Can be reset with the |-m| or |-M| command line ...


5

This is something I wanted when I first started using Vim, but I no longer do. From looking at your account, I can see you've been using Vim for several years, but for the benefit of other readers who are new users, I thought I'd explain why I no longer feel the need for this. Vim offers several different methods for closing windows and buffers, and these ...


4

@FDinoff suggested checking the vim help. In case anyone wanted to see these more easily: WHAT TO DO? *swap-exists-choices* If dialogs are supported you will be asked to select one of five choices: Swap file ".main.c.swp" already exists! ~ [O]pen Read-Only, (E)dit anyway, (R)ecover, (Q)uit, (A)bort, (D)elete it: ~ ...


4

I don't think there is a save/quit automated, but as a reference to several nix-oriented tasks, there is a plugin that covers much of it (e.g. SudoWrite). (plugin: eunuch) To combine the two tasks without using sudo you can use :wq of course. I attempted using :w !sudo tee % | q which failed. Using the eunuch plugin instead allows: SudoWrite | q which ...


4

I don't know how reliable it is but I found how to get the PID of the parent of a process here: ps -o ppid= -p <pid of the process> And how to get the name of a process from its PID here: ps -p 1337 -o comm= One can get the pid of the Vim's process with the getpid() function, so combining the previous 2 commands, maybe this expression could get the ...


4

As you have stated in the comments that you need to prevent the automatic saves you perform under muscle memory, you could just remap the particular combination(s) that you perform without thinking. e.g. nnoremap :w<cr> <nop> You can start Vim with this mapping in place by using the "-c" command line option: vim -c "nnoremap :w<cr> <...


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