369

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

Michael Durrant
  • 89,394
  • 88
  • 305
  • 468

9 Answers9

528

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.

Kenan
  • 11,783
  • 8
  • 39
  • 49
Tim Pote
  • 25,751
  • 6
  • 60
  • 64
91

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 :)

Kasun Siyambalapitiya
  • 3,407
  • 7
  • 33
  • 55
abe312
  • 4,485
  • 32
  • 21
  • 1
    by sudo it will also work if folder is restricted, which is often the case. – abe312 Jan 09 '16 at 19:25
  • 9
    Yes, 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
  • 7
    also 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
24

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)

enter image description here

enter image description here

The filename will also be displayed.

Giriraj Pawar
  • 361
  • 3
  • 13
  • 1
    Welcome to [so]! I fail to see, though, what this answer adds that is not present in the accepted answe – D. Ben Knoble Oct 10 '19 at 03:29
  • Thank you for sharing this. Even though this answer duplicates the accepted answer, there are so many things neatly written down for us, newbies. – tpk May 03 '21 at 08:41
15

Terminal > su > password > vim /etc/vimrc

Click here and edit as in line number (13):

set nu

click here and Edit as "Line number (13)"

muru
  • 4,489
  • 1
  • 32
  • 73
sagar mahajan
  • 339
  • 3
  • 2
7

I did not have a .vimrc file in my home directory. I created one, added this line:

set number

and that solved the problem.

F. Hauri
  • 58,205
  • 15
  • 105
  • 122
Jean
  • 71
  • 1
  • 1
6

If you don't want to add/edit .vimrc, you can start with

vi "+set number" /path/to/file
Chris H.
  • 61
  • 1
  • 4
2

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.

2

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.
Kyle s
  • 484
  • 5
  • 14
0

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

Miguel Ruiz
  • 148
  • 1
  • 1
  • 10