Stop learning alone!

Learn faster and stay on-track by joining this free class with other self-learners.

Register for Learning Vim from the inside now.

Learning Vim from the inside

Class length: 8 weeks. Start anytime.

Creator: pbr

Status: Established

Join this class!

Lesson 3: Assignment 4

Vim reads startup files... ~/.vimrc for example.

How does that work?

How did you find the answer?

What are the various startup files Vim can/will read if they exist?

Homework Submissions

5 total

live_dont_exist (Self-grade: Outstanding)
Submitted 10 months ago | Permalink | Time spent: 1 minute
  • lid .vimrc works. Many matches. Has to be main.c though as that's where C code starts from.

  • Open main.c. I have set hlsearch in .vimrc to highlight matches as I find them. Searched for .vimrc inside main.c. Commented block between line 2926 and line 2934 lists all options for vim.

/
* Try to read initialization commands from the following places: * - environment variable VIMINIT * - user vimrc file (s:.vimrc for Amiga, ~/.vimrc otherwise) * - second user vimrc file ($VIM/.vimrc for Dos) * - environment variable EXINIT * - user exrc file (s:.exrc for Amiga, ~/.exrc otherwise) * - second user exrc file ($VIM/.exrc for Dos) * The first that exists is used, the rest is ignored.
/

Comments:

pbr
10 months ago

Good work!

Pop quiz: What happens unexpectedly when you run:

VIMINIT=':!date' vim

...and... why? :-)

lsqshr
1 month ago

will it print the current system date time in the terminal before the user type 'enter', then it goes in the editor window because on start up, VIMINIT variable is the first thing vim looks at (same quote with the previous answers). It runs the script in VIMINIT before it actually start?

Sign up or log in to comment

shobhitjain (Self-grade: Outstanding)
Submitted 1 year ago | Permalink | Time spent: 1 minute

Comments:

pbr
10 months ago

...OK, I guess!

Sign up or log in to comment

webframp (Self-grade: Outstanding)
Submitted 3 years ago | Permalink

gf on options.c in README.txt took me to

  • options.c:vimrc_found() called when vimrc or VIMIINIT environment variable is found.

So in theory, vim can read whatever the value of VIMINIT is set to. But typically this is one of .vimrc, .gvimrc, _vimrc or _gvimrc depending on the platform.

  • lid -Sn VIMINIT lists: main.c, options.txt,starting.txt,versoin5.txt
  • vim main.c
  • main.c:command_line_scan(paramp) can affect this since case 'u' or case 'U' can specify an init file.
  • from examining main.c: .exrc or _exrc, or value of EXINIT can also be used.

One neat feature is that if either vimrc or exrc are not owned by the calling user, 'secure' mode is set. Using :h secure in vim tells us:

"When on, ":autocmd", shell and write commands are not allowed in ".vimrc" and ".exrc" in the current directory and map commands are displayed."

Comments:

pbr
3 years ago

Good description of secure mode and why it gets turned on. Well done.

Sign up or log in to comment

andrewferk (Self-grade: Outstanding)
Submitted 3 years ago | Permalink

by running grep -R '~/\.vimrc' . I was able to locate a starting point of main.c.

The main() function in main.c executes source_startup_scripts() in main.c. source_startup_scripts() will try to execute a startup file in this order (once a file is found, the rest are ignored):

  • environment variable VIMINIT
  • user vimrc file (s:.vimrc for Amiga, ~/.vimrc otherwise)
  • second user vimrc file ($VIM/.vimrc for Dos)
  • environment variable EXINIT
  • user exrc file (s:.exrc for Amiga, ~/.exrc otherwise)
  • second user exrc file ($VIM/.exrc for Dos)

Comments:

pbr
3 years ago

So why not use 'lid .vimrc' instead of grep?

Awesome work identifying the order Vim looks for the files, and that it only loads and processes the first one it finds.

Sign up or log in to comment

danboykis (Self-grade: Could be better)
Submitted 3 years ago | Permalink

It seems like "do_source" function in "ex_cmds2.c" will read ".vimrc" file passed from "source_startup_scripts" in "main.c"

I found this out by inspecting the output of: $ lid -S 'newline' ".vimrc"

Poking around "source_startup_scripts" I found EVIM_FILE constant, running:

$ lid -S 'newline' "EVIM_FILE"

I navigated to "os_unix.h", since I run linux which lists a bunch of start up files.

In "source_startup_scripts", I found, variables whose values can be looked up in os_unix.h.

What are the various startup files Vim can/will read if they exist?

  • EVIM_FILE
  • SYS_VIMRC_FILE
  • macvim.vim #if on a mac
  • USR_VIMRC_FILE
  • USR_VIMRC_FILE2
  • USR_VIMRC_FILE3
  • USR_EXRC_FILE
  • USR_EXRC_FILE2
  • VIMRC_FILE
  • EXRC_FILE

Comments:

pbr
3 years ago

Rockin! Awesome work.

Question for you: Did you have any idea there were that many startup files Vim will read if they exist? (I found that a bit disconcerting)

Also: For me, -Sn works just as well as providing 'newline' as a standalone followup arg to -S - and it's a lot less typing. I found it by accident; your mileage may vary.

danboykis
3 years ago

I had no idea there was potentially this manny. I have been using vim for a while and I knew about .vimrc. I just knew there existed a system wide vimrc but I never cared about it. I was surprised there could be 3 global ones!

pbr
3 years ago

Yeah. Actually this is an achilles heel for Vim. A local user might be able to create one or another of these files with some scripting to do something neat the next time root run Vim. Not good.

Sign up or log in to comment