How to add nobomb in vimrc - vim

I want to set nobomb in my vimrc config but it wont work. Other commands work but this one doesn't. It works when I use the command in the window with the file opened but not in the config.

This is an option local to the buffer. It is set by Vim (once upon file read) if the file on disk has the BOM. Same for file encoding, file format, last EoL presence and such. You get what you had.

Related

vim overrides default settings when adding new .vimrc file

I have recently tried to aid in automating my workflow in vim. Since there wasn't a .vimrc file in my home folder by default, I've made a new one and put my code in. After starting up vim again I was surprised to see my C code unhighlighted. After removing .vimrc file, everything was back to normal. Knowing this, I would assume vim falls back to some internally defined .vimrc file when there isn't one in the user folder. Is there a way to load a user-made .vimrc file without overriding system default settings? If not, is there a list of commands that I can put into my .vimrc file to restore those settings?
If not, is there a list of commands that I can put into my .vimrc file to restore those settings?
Yes, simply add the line runtime defaults.vim on top of your vimrc.

How to execute a file everytime before opening Vim

I have recently started using vim and I really like it. I have added a few easy mappings in my vimrc file.
But the problem is I get to use a lot of remote machines a lot of time and I can't copy my vimrc on to them but most of the times I won't have enough permissions to do that.
So, I was wondering if there is any way I can put all my vim mappings in a file and tell vim to run it every time it loads, just like a vimrc?
The action that is "parsing" the .vimrc is called source.
In runtime, you can reapply/reparse your .vimrc by using
:source ~/.vimrc
So if you can somehow copy your .vimrc, even if not in your home, but a folder like /tmp you should be able to source it from there, with
:source /tmp/.vimrc
This question has more details and solutions.
One option would be to specify an alternative .vimrc file while launching the program.
The vim man pages has this to say about specifying a vimrc file:
-u {vimrc}
Use the commands in the file {vimrc} for initializations. All the other initializations are skipped. Use this
to edit a special kind of files. It can also be used to skip all
initializations by giving the name "NONE".
See ":help initialization" within vim for more details.
Note that this option overrides the default vimrc file, so you'll have to specify all of your settings/options in this file.
As was mentioned in another answer, you can place your custom vimrc file anywhere you want (or have access to) and then specify the the -u option with the path to your vimrc file.
You could even combine this method with managing your custom vimrc file in an online version control system (like github) - this way you will be able to wget your file from the web instead of having to manually copy it from machine to machine.

Recover a vim file from the .un~ file without the undo command

How can I restore a vim file from the undo file without hitting undo?
I had a vim file that I saved while adding text. Then I ran a python command that emptied the file's contents, and I can see some of the words the file contained in the file's .un~ file. When I try to undo in the file, it says Already at latest change. I can't find the swap file in my swap files directory.
As the other answers have noted, you can't recover the whole file from vim's undo files, simply because vim seems to only keep diffs in the undo files, not the whole contents. (That makes a lot of sense, as it's space efficient.)
One thing you can try though, is to extract what's possible from your undo file:
$ strings <undo-file>
The output will not be pretty, but you could end up finding something that's valuable to you.
You can't. The undo information is linked to Vim's last knowledge of the file's contents; when they don't correspond any more, Vim cannot re-apply the changes. This is documented at :help undo-persistence:
Vim will detect if an undo file is no longer synchronized with the file it was
written for (with a hash of the file contents) and ignore it when the file was
changed after the undo file was written, to prevent corruption.
Best you can do is try to manually salvage recognizable bits in the undo file, e.g. with a hex editor, or Vim's binary mode.
It is not exactly possible, as the undo file only contains the text that was changed in a single change. If you at some point reloaded the file, the undofile should contain the complete buffer for that and starting from there one could theorectically recover the file (by going through the undo states).
I have written about this before at the vim_use mailinglist here and here (which even contains a patch, that let's you force reading in the undo-file)
You could try to patch vim and see if you can recover at least some data.
A reminder that if you have set in your .vimrc file
set backupdir=$HOME/tmp
You may have temp copies of the files that are readable and that can be renamed

Stop Vim from saving a file if content has not changed

In GUI text editors I've seen, when text has not changed (or has changed and been reverted to its original state), the Save option is greyed out to indicate so.
For something similar, can Vim disable saving a file in such a case? That is, doing the :w would not actually change the mtime of the file.
You can use the :update command (:up for short) which writes the file only if the buffer has been modified. If you felt strongly you could add an update option to gvim's File menu, but maybe the Ex command is sufficient.
WRITING WITH MULTIPLE BUFFERS *buffer-write*
*:wa* *:wall*
:wa[ll] Write all changed buffers. Buffers without a file
name or which are readonly are not written. {not in
Vi}
:wa[ll]! Write all changed buffers, even the ones that are
readonly. Buffers without a file name are not
written. {not in Vi}
:wa will save all changed files
:xa will save all changed files, and then quit vim.

Vim --remote-silent always opens [No Name] buffer for first file

I want to be able to open files using the same instance so I added --remote-silent when opening files. But the first time it loads, Vim will open an empty buffer, then my file. So now I have 2 buffers.
Upon further investigation, I noticed that setting nohidden will solve this problem. BUT, not only is it against my liking, it will cause the first buffer to have no syntax highlighting.
This doesn't happen without the --remote-silent option.
Any help appreciated. Thanks!
Doing $ vim --servername FOO --remote[-silent] filename without an instance already running launches a new instance first then opens the file: it is not like $vim filename. You have to find a way to completely remove the first empty buffer.
From my limited testing, adding set bufhidden=wipe to your ~/.vimrc may solve the problem.
set bufhidden=wipe, being local to a buffer, is applied only to the first empty buffer and reset afterwards.
See :h bufhidden.
This will certainly cause some problems when you run Vim normally, though.
edit
Yes, set bufhidden=wipe causes obvious problems. When launched "normally" (with $vim file1) the first buffer is wiped when you edit a second file which is not what you want.
A simple check on the name of the buffer resolves that problem:
if bufname('%') == ''
set bufhidden=wipe
endif
Syntax highlighting works in every situation, here. Could you post the content of your ~/.vim/ and ~/.vimrc somewhere?
The --remote family of options allows for an additional Ex command to be performed after opening the file.
What you can do here is add :bd1 which deletes buffer #1 which is the [No Name] buffer.
example: --remote-silent +:bd1 {file}

Resources