Command line option to open mac formatted file in Vim - vim

I have a file with mac format, I know I can use :e! ++ff=mac to change to the correct fileformat. What I'd like to know is if there is a command line option I can pass when I open the file to open it directly with the correct fileformat.

You can do this using the command line, try:
$ vim -c "set fileformat=mac"
-c <command> executes before loading the first file. As jammessan has noted, this will only affect the first file that is loaded, subsequent files will require that you change fileformat for each buffer.
If that doesn't work, you can also try:
$ vim -c "e ++ff=mac"
But it's better to set this up in your .vimrc on that machine:
set fileformats=mac,unix,dos
specifies which formats to try when starting a new buffer. This also can influence what fileformat is chosen for existing files.
See help file-formats, :help 'fileformat' and :help 'fileformats' for more details.

The better solution would be to make Vim automatically recognize files in that format. You can do this by updating the 'fileformats' option in your ~/.vimrc
set fileformats=unix,dos,mac
This will make Vim check all three formats when determining which format a file is in.

Related

NeoVIm does not automatically load ~/.nvimrc file

I was looking to get into learning a text editor for programming. However, I've quickly run into a little snag that I can't seem to find a solution to.
I have modified my /home/user/.nvimrc file to add some plugins and I can load it using :source ~/.nvimrc, however, it never loads automatically. :scriptnames shows a list of scripts in /usr/, but mysteriously absent from the list is the .nvimrc file in my home directory. Again, I can load it in the command line, but I'd like to not have to use :so ~/.nvimrc every time I open a file.
I am not using sudo to run vim.
How can I solve this problem? Is this something everybody has to do?
Could be this issue: https://github.com/neovim/neovim/issues/3530
Summary:
New location is ~/.config/nvim/init.vim
To keep ~/.nvimrc you can source it from the new location:
mkdir -p ~/.config/nvim
echo 'source ~/.nvimrc' > ~/.config/nvim/init.vim
Instead of referring to your rc file directly, consider using $MYVIMRC:
:e $MYVIMRC
:source $MYVIMRC
Reference: Learn Vim the Hard Way/Editing your vimrc
:help config lists the paths for each OS:
Unix ~/.config/nvim/init.vim (or init.lua)
Windows ~/AppData/Local/nvim/init.vim (or init.lua)
$XDG_CONFIG_HOME $XDG_CONFIG_HOME/nvim/init.vim (or init.lua)

Opening file present in another folder which has same relative path as current opened file in vim using vertical diffsplit?

Say I have following 2 similar files in 2 different workspace on same linux machine.
/user1/ws1/ip/src/ip_main.c
/user1/ws2/ip/src/ip_main.c
Now I did,
cd user1/ws1/
vim ip/src/ip_main.c
then I press ESC then executed
:cd ../ws2
which shows my pwd as "user1/ws2"
Now I want to open ip/src/ip_main.c in pwd i.e "user1/ws2"
using ESC
:vertical diffsplit "some logic to get to ws2/ip/src/ip_main.c"
note after :cd ../ws2
:pwd command show "user1/ws2" but :echo $PWD command still show "user1/ws1"
How can i do it,Can anyone help ?
Diffing both files is easily done via shell globbing, e.g. in Bash:
$ vimdiff -O /user1/ws{1,2}/ip/src/ip_main.c
Withing Vim, you have to use relative paths (i.e. prepend ../.., then go down into the other hierarchy). <C-R>% on the command-line (cp. :help c_CTRL-R) inserts the current path; this may avoid retyping much of the similar path, especially when used with the command-line window (:help c_CTRL-F).
With my EditSimilar plugin, you can use this:
:DiffSplitSubstitute 1=2

gvim: change the default working directory

when I open gvim using Alt+F2 it takes as its default working directory my home folder.
How can I change the working folder after or while running gvim? can i pass that folder as a parameter when open gvim?
You could use a shortcut.
The simplest way, though, would be to
:edit $MYVIMRC
append a line
cd /home/user/my/work/dir
save (optionally execute :w|source % to immediately reload)
Inside vim
use
:pwd
:cd some/other/dir
To view/change current working directory.
Use e.g.
:cd %:h
to change to the directory containing the file loaded in the active window.
If you need/want to do this often, consider just setting 'autochdir'
:se autochdir
From the docs:
When on, Vim will change the current working directory
whenever you open a file, switch buffers, delete a
buffer or open/close a window. It will change to the
directory containing the file which was opened or
selected. This option is provided for backward
compatibility with the Vim released with Sun ONE
Studio 4 Enterprise Edition.
Note: When this option is on some plugins may not work.
You can pass an a folder to gvim (when you have NERDTree then it will be a file tree) You can cd before start to begin in directory you want or use :cd <path> command to change current working directory, which can be passed to -c flag when running Vim:
$ [g]vim -c 'cd <path>'
You can also check current dir using :pwd command.
You can change the working directory with the :cd command. You can also pass this in a command-line option like this:
vim -c "cd wherever"
If you like the working directory to always be the file you're currently editing you can use the set autochdir option. Put that in your ~/.vimrc or see :help autochdir.
I know I'm late, but I started using CDargs which is a bash tool to mark certain directories as bookmarks, then use cdb and press tab to list all the bookmarked directories.
There is a vim plugin that interacts with the settingsfile of this tool: vim-cdargs.
This combo works really nice for me to switch between projects.
Or after opening gvim to go quickly to some bookmarked folder, then use Ctrl-p plugin to quickly find the file I want to edit.
extra hint: I don't even want to type :Cdb so I abbreviated c to expand to :Cdb by adding this to my vimrc:
cnoreabbrev c Cdb
after which typing :c followed by a space, will expand into :Cdb.
EDIT: I now use vim-startify which provides a start page for vim that shows the most recent used files. And with the option let g:startify_change_to_vcs_root = 1 it will change the working directory to the outermost vcs root folder of the file you opened. Which is almost always what I want.
Furthemore, I created my own 'plugin' with some key mappings that will switch to the closest or furthest directory, in the path of the current buffer, containing a .git directory or file. In order to easily switch between searching for files in the current git submodule or in the overal supermodule.
Also I switched to fzf with fzf-vim instead of Ctrl-p, which works significantly faster and is more highly configurable.

Vim record history

Vim stores the list of commands that we applied using : for the current execution.
But when I close vim and start it again, the vim command history is lost.
I tried set history = 1000 in the .vimrc file but that did not help.
Where does Vim maintain the local command history?
What is the command to retain command history?
Just an issue that caught me out the other day, which may or may not be your problem:
On some Linux systems (e.g. Ubuntu), if the very first time you run VIM, you run it as a super-user, then the $HOME/.viminfo file gets created with root owner and your local user does not have permissions to write to it. This explained why my VIM was not storing command history when it was all configured up correctly.
Bottom line: on a *nix system, locate your .viminfo file, and make sure you have read/write permissions on it.
To check whether Vim supports the 'viminfo' file (which stores the history), :echo has('viminfo'). The corresponding setting must not be empty: :set viminfo?, and :set history? should be greater than one.
If there's a problem writing the viminfo file (though Vim should complain in that case), you could try passing a different location via vim -i /tmp/viminfo
You should check the permissions of the .viminfo file. You might need to change owner of the file to your current user using chown or sudo chown.
Mr. Baint has given the answer.
Go to $HOME directory.
give ls -l .viminfo to check permissions.
change permission so that group and owner also can have write
permission. use:
sudo chown yourUserId $HOME/.viminfo
It should fix the issue.
You will have to set the viminfo option. Set it in your $MYVIMRC
Update To find out where the option was last set/changed:
:verbose set viminfo?
See http://vimdoc.sourceforge.net/htmldoc/starting.html#viminfo-file
If you exit Vim and later start it again, you would normally lose a lot of
information. The viminfo file can be used to remember that information, which
enables you to continue where you left off.
This is introduced in section |21.3| of the user manual.
The viminfo file is used to store:
The command line history.
The search string history.
The input-line history.
Contents of non-empty registers.
Marks for several files.
File marks, pointing to locations in files.
Last search/substitute pattern (for 'n' and '&').
The buffer list.
Global variables.
The viminfo file is not supported when the |+viminfo| feature has been
disabled at compile time.
You could also use Session files.
I went round in circles on this one a bit on Ubuntu and set viminfo solutions proposed above resulted in errors.
I eventually did the command "version" in the command mode and it came back with "-" for most stuff including:
-cmdline_hist
-cmdline_info
I ran the following command and it all worked fine again:
sudo apt install vim
I had the same problem. The issue was that I had vim-minimal installed (this is a default with Fedora), which does not support history. I had to uninstall it and install the full vim instead. I now have history... and syntaxic coloration !
If you are using a vimrc file, check your folders and files for existence and correctness
set history=200
set undolevels=128
set undodir=~/.vim/undodir/
set undofile
set undolevels=1000
set undoreload=10000
If the specified folder (undodir in my case) does not exist, the history will not be saved.

Clear certain criteria from .viminfo file

How can I clear certain criteria from my .viminfo file?
I want to clear the command line history, file marks, jumplist, etc.
However, I want to keep the search string history.
Is there any way to do this?
I can think of 3 ways to do this.
1. Automatically
Run Vim,
Type: :set viminfo='0,:0,<0,#0,f0
'0 means that marks will not be saved
:0 means that command-line history will not be saved
<0 means that registers will not be saved
#0 means that input-line history will not be saved
f0 means that marks will not be saved
no % means that the buffer list will not be saved
no / means that the search history will be saved
These options are correct in Vim 7.2, but might be different in other versions. For more details on the format of the viminfo string, run :h 'viminfo'
Quit Vim. It will save a new version of the .viminfo file, containing only the information you want to keep.
2. Manually
Open the .viminfo file in vim,
:set viminfo= to turn off the auto-saving of info to the .viminfo file. If you don't do this, Vim will overwrite all your changes when you quit,
Remove everything you don't want (perhaps by using Johnsyweb's answer, or just by deleting the lines with manual edit commands), save the file, and quit vim,
3. In a different editor
Edit the .viminfo file in a different text editor and simply delete everything you don't want,
Open the .viminfo file.
The following command will remove all lines that are neither blank, comment nor search history:
:v/^\([#/?]\|$\)/d
#Rich's answer will help you prevent these lines being repopulated.
You can use vim itself to modify the file, and your changes will stay put if you invoke it like this:
$ vim -i NONE ~/.viminfo
Starting vim with the -i option sets which viminfo file to use for that session. The special value NONE means that nothing is saved at all. This can be handy for editing other auto-generating files or sensitive data.
From man vim:
-i {viminfo}
When using the viminfo file is enabled, this option sets the filename
to use, instead of the default ~/.viminfo. This can also be used to
skip the use of the viminfo file, by giving the name NONE.
VIM seems not having a built-in command for this job. But you can do it in a shell with "grep".
For example, the following command will clear the Command Line History,File marks, Jumplist:
bash $ grep -v "^[:'-]" .viminfo > .viminfo_clear
bash $ cp .viminfo_clear .viminfo
If you open the .viminfo, you will find that the command line history is started with ":", the file mark is started with "'", and the jumplist is started with "-".
Copy useful part out and delete .viminfo, then open/close Vim. It will regenerate .viminfo. Then copy useful part back.

Resources