how to make vimrc changes permanent on Neovim - vim

I am new to nvim editor. i need help setting up my ~/.vimrc file. i have seen some tutorials and followed them. and came up with this:
> vim ~/.vimrc
have added this to my vimrc.
syntax on
set noerrorbells
set tabstop=4 softtabstop=4
set shiftwidth=4
set expandtab
set smartindent
set nu
and then source the file
:source %
the changes get applied instantly. but when i exit the file and open it again the changes disappear. if i source it again it works. is there any way i can make these changes permanent.
[note] : when i exit the file i used :x
UPDATE:
when i try to open ~/.config/nvim/init.vim using vim. i am getting this error.
E325: ATTENTION
Found a swap file by the name "~/.local/share/nvim/swap//%home%manoj%.config%nvi
m%init.vim.swp"
owned by: manoj dated: Wed Mar 31 16:01:46 2021
file name: ~manoj/.config/nvim/init.vim
modified: YES
user name: manoj host name: manoj
process ID: 15099
While opening file "/home/manoj/.config/nvim/init.vim"
CANNOT BE FOUND
(1) Another program may be editing the same file. If this is the case,
be careful not to end up with two different instances of the same
file when making changes. Quit, or continue with caution.
(2) An edit session for this file crashed.
If this is the case, use ":recover" or "vim -r /home/manoj/.config/nvim/init
.vim"
to recover the changes (see ":help recovery").
If you did this already, delete the swap file "/home/manoj/.local/share/nvim
/swap//%home%manoj%.config%nvim%init.vim.swp"
to avoid this message.
Swap file "~/.local/share/nvim/swap//%home%manoj%.config%nvim%init.vim.swp" alre
ady exists!

~/.vimrc is the default configuration file for vim. For neovim, simply copy the file to ~/.config/nvim/init.vim (You may need to create the nvim directory first).
Taken from the arch wiki, although the most info is in the official documentation.

in my ~/.config/nvim/init.vim, there are the line :
source ~/.vimrc
I change it to :
source ~/.vim/vimrc
and neovim (v0.6.1) use my ~/.vim/vimrc file settings

Related

Saving preferences VIM

Hi I'm a new user to vim text editor (love it) and would like to save my preferences in VIM ie font and size and background colour, ive searched and the only things i can find say modify the rc file however they do not specify how except to advise on customisations such as line spacing. Please could someone advise?
On a Linux machine, you can start making a .vimrc file in your home folder by running the following in the terminal:
vim ~/.vimrc
This is the "rc" file that they are talking about.
Regarding the formatting of this file, you can put every setting on a separate line:
" This line is a comment, the following 2 lines are settings:
colorscheme desert
set history=500
While still in vim, save with :wq.
The next time you start vim, it will use the settings that you just specified in your ~/.vimrc.
Here is the $ vimtutor section dedicated to that very subject:
Lesson 7.2: CREATE A STARTUP SCRIPT
** Enable Vim features **
Vim has many more features than Vi, but most of them are disabled by
default. To start using more features you have to create a "vimrc" file.
1. Start editing the "vimrc" file. This depends on your system:
:e ~/.vimrc for Unix
:e ~/_vimrc for Windows
2. Now read the example "vimrc" file contents:
:r $VIMRUNTIME/vimrc_example.vim
3. Write the file with:
:w
The next time you start Vim it will use syntax highlighting.
You can add all your preferred settings to this "vimrc" file.
For more information type :help vimrc-intro

How to jump back to netrw when splitbelow is on

If I open a file in a current directory using netrw and then reopen the netrw using :Sexplore the netrw window opens below (I have splitbelow set), but the cursor remains in the file being edited and I cannot move to the netrw window using CTRL-W combinations.
Am I doing something wrong or is this a bug?
Steps to reproduce
run vim .
netrw opens, highlight a file in the current directory (let's say test.txt) and press Return, file test.txt opens
run :Sexplore cmd
netrw opens below but the cursor remains in the file opened previously.
My .vimrc
set nocompatible
set splitbelow
VIM version
VIM - Vi IMproved 7.4 (2013 Aug 10, compiled Dec 8 2015 23:11:08)
MacOS X (unix) version
Included patches: 1-944
This is how it looks like:
I cannot reproduce with version 154a (use :echo g:loaded_netrwPlugin to check your netrw version).
Try with the latest version.
Edit:
It is possible that the problem is cause by some other plugin interference or settings. You could try following the approach described at Vim FAQ 2.5:
2.5. I have a "xyz" (some) problem with Vim. How do I determine it is a
problem with my setup or with Vim? / Have I found a bug in Vim?
First, you need to find out, whether the error is in the actual
runtime files or any plugin that is distributed with Vim or whether it
is a simple side effect of any configuration option from your .vimrc
or .gvimrc. So first, start vim like this:
vim -u NONE -U NONE -N -i NONE
This starts Vim in nocompatible mode (-N), without reading your
viminfo file (-i NONE), without reading any configuration file (-u
NONE for not reading .vimrc file and -U NONE for not reading a .gvimrc
file) or even plugin.
(...)
If the error does not occur when starting Vim this way, then the
problem is either related to some plugin of yours or some setting in
one of your local setup files. You need to find out, what triggers the
error, you try starting Vim this way:
vim -u NONE -U NONE -N
If the error occurs, the problem is your .viminfo file. Simply delete
the viminfo file then. If the error does not occur, try:
vim -u ~/.vimrc --noplugin -N -i NONE
This will simply use your .vimrc as configuration file, but not load
any plugins. If the error occurs this time, the error is possibly
caused by some configuration option inside your .vimrc file. Depending
on the length of your vimrc file, it can be quite hard to trace the
origin within that file.
The best way is to add :finish command in the middle of your .vimrc.
Then restart again using the same command line. If the error still
occurs, the bug must be caused because of a setting in the first half
of your .vimrc. If it doesn't happen, the problematic setting must be
in the second half of your .vimrc. So move the :finish command to the
middle of that half, of which you know that triggers the error and
move your way along, until you find the problematic option. If your
.vimrc is 350 lines long, you need at a maximum 9 tries to find the
offending line (in practise, this can often be further reduced, since
often lines depend on each other).
Obviously you will have to load netrw manually (which can be done by sourcing plugin/netrwPlugin.vim -- if you are using some plugin manager you will have to move the plugin to ~/.vim/ or change your runtime path due to the autoload functions) and set nocompatible and splitbelow.
Actually there is an adapted version of this procedure in :help netrw-debug:
Step 2: assuming that you've installed the latest version of netrw,
check that your problem is really due to netrw. Create a file called
netrw.vimrc with the following contents:
set nocp
so $HOME/.vim/plugin/netrwPlugin.vim
Then run netrw as follows:
vim -u netrw.vimrc --noplugins [some path here]
Perform whatever
netrw commands you need to, and check that the problem is still
present. This procedure sidesteps any issues due to personal .vimrc
settings and other plugins. If the problem does not appear, then you
need to determine what setting in your .vimrc is causing the conflict
with netrw or which plugin.
Step 3: If the problem still is present, then get a debugging trace
from netrw:
(...)

Error after 'source ~/.vimrc' after setting up pathogen

I get this error when I source my .vimrc file,
> source ~/.vimrc
bash: /home/dev/.vimrc: line 1: syntax error near unexpected token `('
bash: /home/dev/.vimrc: line 1: `call pathogen#infect()'
The contents of my .vimrc file are,
call pathogen#infect()
syntax on
filetype plugin indent on
set smartindent
set tabstop=4
set shiftwidth=4
set expandtab
set backupdir=~/.vimbackup
set directory=~/.vimdir
I've also tried using 'execute pathogen#infect()' in the chance that it doesn't like the 'call' keyword.. and I've tried passing in the path to my bundle file as follows,
call pathogen#infect('~/.vim/bundle/{}')
I've also tried just putting 'bundle/{}' as the argument to infect... But I don't understand vim/bash well enough to know what direction to go in, would appreciate any help...
The reason I'm trying to source my .vimrc file is because everytime I use vim to edit a file I get litter from the file in the form of files being left that have this format,
.file_i_just_worked_on.un~
which is really annoying making my dir messy. I've read that I need to have
set backupdir=~/.vimbackup
set directory=~/.vimdir
to not have those files being dropped by vim everywhere... Would appreciate any help with this issue.
You're asking bash to read your .vimrc file, but it isn't equipped to parse that.
You instead need to have vim read it. You can either start a new instance of vim, or type :source ~/.vimrc from normal mode within a running vim session.
If vim is still creating backup files in your current directory, that may be because you haven't created the ~/.vimbackup or ~/.vimdir directory.
As qqx mentioned, I need to ":source ~/.vimrc" inside of vim, not use bash...
To get rid of the .file.un~ files I need to have a
set undodir=path/to/dir
in my vimrc

vim line numbers - how to have them on by default?

I can :set number from within a file I'm editing but how can I have them always be on by default?
Add set number to your .vimrc file in your home directory.
If the .vimrc file is not in your home directory create one with
vim .vimrc and add the commands you want at open.
Here's a site that explains the vimrc and how to use it.
To change the default setting to display line numbers in vi/vim:
vi ~/.vimrc
then add the following line to the file:
set number
Either we can source ~/.vimrc or save and quit by :wq, now future vi/vim sessions will have numbering :)
set nu
set ai
set tabstop=4
set ls=2
set autoindent
Add the above code in your .vimrc file. if .vimrc file is not present please create in your home directory (/home/name of user)
set nu -> This makes Vim display line numbers
set ai -> This makes Vim enable auto-indentation
set ls=2 -> This makes Vim show a status line
set tabstop=4 -> This makes Vim set tab of length 4 spaces (it is 8 by default)
The filename will also be displayed.
Terminal > su > password > vim /etc/vimrc
Click here and edit as in line number (13):
set nu
If you don't want to add/edit .vimrc, you can start with
vi "+set number" /path/to/file
I did not have a .vimrc file in my home directory. I created one, added this line:
set number
and that solved the problem.
in home directory you will find a file called ".vimrc" in that file add this code "set nu" and save and exit and open new vi file and you will find line numbers on that.
I'm using Debian 7 64-bit.
I didn't have a .vimrc file in my home folder. I created one and was able to set user defaults for vim.
However, for Debian 7, another way is to edit /etc/vim/vimrc
Here is a comment block in that file:
" All system-wide defaults are set in $VIMRUNTIME/debian.vim (usually just
" /usr/share/vim/vimcurrent/debian.vim) and sourced by the call to :runtime
" you can find below. If you wish to change any of those settings, you should
" do it in this file (/etc/vim/vimrc), since debian.vim will be overwritten
" everytime an upgrade of the vim packages is performed. It is recommended to
" make changes after sourcing debian.vim since it alters the value of the
" 'compatible' option.
Add any command you want to have by default to your ~/.vimrc file (named _vimrc on Windows systems)

How to tell VIM to store the viminfo file somewhere else?

Is it possible to tell vim to save its viminfo file somewhere else?
Such as in the .vim folder
Try adding set viminfo+=n~/.vim/viminfo to your ~/.vimrc file. From :help 'viminfo':
n Name of the viminfo file. The name must immediately follow
the 'n'. Must be the last one! If the "-i" argument was
given when starting Vim, that file name overrides the one
given here with 'viminfo'. Environment variables are expanded
when opening the file, not when setting the option.
set viminfo='1000,nc:\\users\\abcdef\\_viminfo
This works for me in Windows 7.
I had a similar problem but had no write permission to my home folder (thus could not create the ~/.vimrc).
I solved it by modifying (through the Administrator) the _vimrc in C:\Program Files\VIM to include the line:
let $HOME = $USERPROFILE
I placed it at the beginning of the file and it worked well. Just in case someone comes across this question and this answer didn't work.
I know its quite a while now, but I am just mentioning this for future readers.
I was facing the same issue while trying to change the location for viminfo file in vimrc. At last setting the value of viminfofile option worked for me.
Added the following to my vimrc:
set viminfofile=D:\vim\viminfo
It works for me on windows with vim 8.2
TL;DR
This seems to be a bug in vim where set nocompatible is not idempotent and doesn't follow the principle of least astonishment.
As a workaround, either:
Ensure that you set nocompatible (or the equivalent set nocp) only once, and at the top of your vimrc.
Don't set it again if it's already set:
if &compatible | set nocompatible | endif " Avoid side effects if `nocp` already set
Explanation and bug illustration
From :help compatible (empahsis mine):
This is a special kind of option, because when it's set or reset,
other options are also changed as a side effect. CAREFUL: Setting or
resetting this option can have a lot of unexpected effects: Mappings
are interpreted in another way, undo behaves differently, etc. If you
set this option in your vimrc file, you should probably put it at the
very start.
Note that &viminfo is not listed in the side-effects, however the following lines clearly show the side effect upon &viminfo:
set nocompatible
set viminfo+=nWatch-my-viminfo-file-location-be-ignored
echom &viminfo
set nocompatible " do side effects even though nocomptible is already set
echom 'After 2nd "set nocompatible":'
echom &viminfo
Output:
'100,<50,s10,h,nWatch-my-viminfo-file-location-be-ignored
After 2nd "set nocompatible":
'100,<50,s10,h
vim --version | head -1
VIM - Vi IMproved 7.4 (2013 Aug 10, compiled Aug 05 2016 16:48:20)
Resolution
I have raised two GitHub issues regarding this:
Undocumented "set nocompatible" side effect upon &viminfo
set nocompatible not idempotent - setting produces side effects when already set
See also Vi StackExchange's question: Can't move viminfo file - &viminfo reverts upon loading vim which I raised before seeing this one.
The ordering of settings in the .vimrc affects the use of :set viminfo. The setting of viminfo should be after the setting of nocompatible. After reordering my .vimrc, the above solution to have the viminfo file be located in the .vim directory worked for me.
In gvim8.2 on Windows10, following option works to change path of viminfo :
set viminfofile=$VIM/.viminfo
Write this in .vimrc .
It could to change viminfo file path to under the $VIM.

Resources