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

192,317

Solution 1

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.

Solution 2

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

Solution 3

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.

Solution 4

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

Solution 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.

Share:
192,317
Michael Durrant
Author by

Michael Durrant

rails ruby rspec rock

Updated on July 28, 2022

Comments

  • Michael Durrant
    Michael Durrant almost 2 years

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

  • Tim Pote
    Tim Pote about 12 years
    Yeah. 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.
  • abe312
    abe312 over 8 years
    by sudo it will also work if folder is restricted, which is often the case.
  • Christian Brabandt
    Christian Brabandt over 8 years
    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
  • yonatan
    yonatan almost 8 years
    also why use gedit? what if it's a server? or gedit is just not installed? make more sense to use vim :)
  • crobicha
    crobicha about 7 years
    Note that this would change default vim behavior for ALL users on a system, unless overridden by a users personal .vimrc in their home directory
  • Ryan  Hoo
    Ryan Hoo over 6 years
    I like the color scheme in this screenshot, which one are you using?
  • Rajeev Ranjan
    Rajeev Ranjan over 5 years
    Create the file .vimrc if it does not exist in the home directory.
  • Brosig
    Brosig over 5 years
    For ubuntu server the file location is /etc/vim/vimrc
  • aarona
    aarona about 5 years
    another great setting that's useful is set relativenumber
  • Kapil Jituri
    Kapil Jituri over 4 years
    Simply run this command anywhere in your terminal: echo "\nset nu" >> ~/.vimrc. Now exit and reopen terminal session.
  • unknown programmer guy
    unknown programmer guy over 3 years
    if you cant find .vimrc in your home directory, you can check it inside /etc/vim/vimrc
  • maruf
    maruf over 2 years
    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 :(