I can :set number
from within a file I'm editing but how can I have them always be on by default?

- 93,410
- 97
- 333
- 497
-
4Can you put the set command in your .vimrc? – Tim Apr 23 '12 at 02:32
-
3where is that located ? Or do I just create one ? – Michael Durrant Apr 23 '12 at 02:33
-
4You can make one. It has to be in your home directory. See my answer for details. – Tim Pote Apr 23 '12 at 02:35
-
2I created it, it worked. – Michael Durrant Apr 23 '12 at 02:35
-
It doesn't work when opening a directory using Vim, i have to use set nu again to turn numbering on, any workaround for that ? – user3218088 Jun 16 '14 at 10:38
-
12I never understood why Vim and half the IDEs out there disable line numbers by default. Trying to save a few pixels? – sudo Feb 12 '17 at 10:42
9 Answers
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.

- 27,191
- 6
- 63
- 65
-
4Yeah. Any time I want to set some defaults for a command line program I search their man page for `rc`. It's a pretty common convention. – Tim Pote Apr 23 '12 at 02:41
-
2Create the file .vimrc if it does not exist in the home directory. – Rajeev Ranjan Sep 04 '18 at 09:00
-
3
-
2Simply run this command anywhere in your terminal: `echo "\nset nu" >> ~/.vimrc`. Now exit and reopen terminal session. – Kapil Jituri Jan 31 '20 at 06:02
-
1if you cant find .vimrc in your home directory, you can check it inside /etc/vim/vimrc – unknown programmer guy Dec 01 '20 at 19:54
-
This works but maybe VIM's default profile no longer working. One example: After creating .vimrc file java's default documentation comment's formatting not working correctly :( – maruf Sep 09 '21 at 16:52
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 :)

- 3,956
- 8
- 38
- 58

- 2,547
- 25
- 16
-
1by sudo it will also work if folder is restricted, which is often the case. – abe312 Jan 09 '16 at 19:25
-
9Yes, and you break access for the normal user which owns his home directory. This has been the cause of several bug reports and is just the wrong solution – Christian Brabandt Jan 09 '16 at 19:39
-
7also why use gedit? what if it's a server? or gedit is just not installed? make more sense to use vim :) – yonatan Jun 14 '16 at 14:05
set nu
set ai
set tabstop=4
set ls=2
set shiftwidth=4
Add the above code to your .vimrc file. if the .vimrc file is not present please create it in your home directory (/home/name of the user)
set nu -> Displays line numbers
set ai -> Enables auto-indentation
set ls=2 -> Shows a status line
set tabstop=4 -> Sets tab of length 4 spaces (it is 8 by default)
set shiftwidth=4 -> Number of spaces to use for each step of (auto)indent. Used for |'cindent'|
, |>>|
, |<<|
, etc. Please refer to Vim documentation here.

- 411
- 5
- 14
Terminal > su
> password
> vim /etc/vimrc
Click here and edit as in line number (13):
set nu

- 4,723
- 1
- 34
- 78

- 349
- 3
- 2
-
1Note that this would change default vim behavior for ALL users on a system, unless overridden by a users personal .vimrc in their home directory – crobicha May 01 '17 at 16:54
-
2
-
If you don't want to add/edit .vimrc, you can start with
vi "+set number" /path/to/file

- 99
- 1
- 5
I did not have a .vimrc file in my home directory. I created one, added this line:
set number
and that solved the problem.

- 64,122
- 17
- 116
- 137

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

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

- 484
- 5
- 14
Add any command you want to have by default to your ~/.vimrc
file (named _vimrc
on Windows systems)

- 408
- 1
- 6
- 13