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

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)

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 make vimrc changes permanent on Neovim

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

Make .vimrc changes being picked up by Vim only and for me only

I have a $HOME/.vimrc file which I have configured for many options like below but not limited to.
set foldmethod=indent
set foldnestmax=10
set mouse=a
set number
These all work fine if I open a file with vim however if I open with vi or view the file none of the config work, which is fine but I get errors for some.
line 104:
E538: No mouse support: mouse=a
line 205:
E518: Unknown option: foldmethod=indent
Press ENTER or type command to continue
After searching quite a while I was able to remove some of these.
if has('mouse')
set mouse=a
endif
silent! set foldmethod=indent
However this is just suppressing them. And the linenumbers are still being displayed with vi or view which look pretty bad and many people login to the box will be suddenly mystified by the yellow line numbers.
How to make vi not take up these config.
I powerbroker to the linux box. Can I make .vimrc setting just for me.
vi and vim are the same executable.
I'd suggest you change the name of your vimrc:
$ mv ~/.vimrc ~/.myvimrc
and start Vim with:
$ vim -Nu ~/.myvimrc
You could add an alias to ~/.bashrc (or whatever works in that system) to ease your workflow:
alias myvim='/usr/bin/vim -Nu ~/.myvimrc'
As vi does not support all options of vim, What I do is to set alias for vi to vim in my rc file
alias vim='vim -p'
alias vi='vim -p'
The -p is not really required. but I kinda like tabbing enabled by default on vim. So every time, if you hit vi or vim, it behaves the same.
You can make this permanent, if you like, by adding the lines to ~/.bashrc, assuming you are using bash. You need to source ~/.bashrc to take immediate effect without logging off.

VIM Undofile displays at Terminal login

Upon opening a new terminal window, the most recent VIM undofile to be written displays as the first line, generally with some form of error. Why is this displaying, and is there any way to prevent it? Adding .hushlogin to my home directory didn't work.
Context: My .dotfiles are versioned in a Git repo, containing a symlinked .Vim directory. I'm using homebrew installations of ZSH, iTerm2, and Git.
Message: /Users/joshuaberk/.dotfiles/vim/vim.symlink/undo/%Users%joshuaberk%.dotfiles%system%env.zsh:1: unmatched
The (potentially relevant) portion of my .Vimrc is included below (Undofile settings are in "Backup").
" Search/Substitution/Completion
set ignorecase " removes case sensitivity by default
set smartcase " adding ≥1 uppercase = case sensitivity
set incsearch " do incremental searching
set gdefault " substitute all matches in line (not first)
set showmatch " quickly jump to matching bracket
set completeopt=menu,longest " disable preview window on completion
set wildmenu
set wildmode=list:longest,full
" Backup
set undofile
set undodir=~/.vim/undo
set backupdir=~/.vim/backups
set directory=~/.vim/swaps
set history=50
set autoread " updates VIM file if changed elsewhere
set hidden " hide buffers instead of closing them
set clipboard+=unnamed " system clipboard by default
In the comments, you say you're using Zach Holman's dotfiles. Within the zsh dotfiles, there's a section that looks for all *.zsh files and loads them. It's looking for everything under $ZSH:
config_files=($ZSH/**/*.zsh)
Which, by default, is the whole dotfiles directory:
export ZSH=$HOME/.dotfiles
And your .vimrc is setting your undofiles to be within .vim (which is a symlink into that dotfiles directory).
You can patch around this by removing all those undo files:
cd ~/.vim/undo
rm *.zsh
But how do you fix it permanently? I couldn't find any especially satisfying way. You can't customise the name of the undofile Vim uses, but you can customise its location. Maybe you're going to tell Vim to save its undos outside of your .dotfiles directory:
set undodir=/tmp/vim/undo
if !isdirectory(expand(&undodir))
call mkdir(expand(&undodir), "p")
endif
You could tell Vim to not save undofiles for .zsh files:
autocmd FileType zsh set noundofile
Or, you could tell zsh to STFU about errors in your config files by changing your .zshrc to print error messages to /dev/null:
for file in ${(M)config_files:#*/path.zsh}
do
source $file 2> /dev/null
done
This one seems like the worst option; it's going to make your life Hell if you introduce a typo in one of your other .zsh files. I'd go for option 1 or 2, depending on how much you want to keep your undo history across reboots.

Change linux user default login directory

I have used the command usermod -d /home/matt /home/matt/Documents/docs/ to change my linux default login directory to /home/matt/Documents/docs/, but when i use vim to edit my code, my own configuration about vim didn't work, for example, my tab will equal to 8 spaces not 4 spaces(defined in my .vimrc). The configuration file is .vimrc and is under /home/matt/.vimrc.
The following is the content of my .vimrc:
set hlsearch
set backspace=2
set autoindent
set ruler
set nu
set bg=dark
syntax on
set shiftwidth=4
set softtabstop=4
if i copy my .vimrc from /home/matt/ to my new home directory(/home/matt/Documents/docs/), things work well. But when i change my home and i have to copy my .vimrc at the same time, it terrible. Anyone has a better solution?
The usermod command changes your home directory, where .vimrc is expected to be.
I suggest you to put the file in one directory, and then create a link (through ln -s) from the other directory.

Resources