How to open a file using vi
The worst nightmare of a developer who doesn't know how to work with the command line and/or work with files on the command line (opening, editing, saving, exiting the f*cking... ehm awesome editor): Needing to log in to a server via SSH aka having only the command line available. What will you do if you have to edit a file when there is no text editor with a UI available?
I know, I know, you can ask another developer but why not solve the issue on your own? So let's start with preventing you from looking like this Junior Developer when you are in such a situation by learning how to open a file using vi first.
How to open vi
To open vi, just type the command vi
in your terminal. In case that vim is available on your system, the vi command will open vim. The vi command will open vi/vim displaying some hints:
~
~
~ VIM - Vi IMproved
~ version 9.0.270
~ by Bram Moolenaar et al.
~ Vim is open source and freely distributable
~ Sponsor Vim development!
~ type :help sponsor<Enter> for information
~ type :q<Enter> to exit
~ type :help<Enter> or <F1> for on-line help
~ type :help version9<Enter> for version info
~
~
As you can see, it displays how to get help (typing :help
). That's very nice of vi 😁
Note that there are several tildes ("~") which mean "no content" (i.e. no text and no blank lines).
Opening vi like this can be used to first write down whatever you want to write down and save it as a new file when you are done and want to exit vi.
How to open an existing file
To open an existing file, you need to type the vi command followed by the file name: vi [filename]
, e.g. vi my-file
or vi path/to/directory/my-file
to open the file "my-file" which is located in another directory. As you can see the brackets should not be typed. Vi will then look something like this:
this is some content
~
~
~
~
"my-file" [noeol] 1L, 20B
The line at the bottom is called prompt line or status line.
We can extract the following information from the status line:
- the name of the current file is "my-file"
- the file does not contain an end-of-line on the last line, i.e. no newline (\n) at the end ("[noeol]")
- the number of lines of the file is 1 ("1L")
- the size of the file is 20 Bytes ("20B")
How to open a new file
Let's say we wanted to open a non-existing file in vi, we could do so by specifying the file name after the vi command like beforehand: vi my-new-file
. It is even possible to specify that the new file should be located in another directory, e.g. vi path/to/directory/my-new-file
. Vi will then look something like this:
~
~
~
"my-new-file" [New]
We can extract the following information from the status line:
- the name of the current file is "my-new-file"
- "my-new-file" is a new file ("[New]")
Recap
In this article we learned
- how to open vi by using the
vi
command - how to open an existing and a new file by using the vi command, e.g.
vi my-file
orvi path/to/directory/my-file
- that tildes mean that there is no content
- that the line at the bottom is called "prompt line" or "status line"
- what the content in the status line means
Watch it on YouTube
You can watch me explaining how to open a file using vi and vim in this video: