How to Create A New File with Vi/Vim Text Editor on Linux

This post will guide you how to create new file with vi/vim text editor in CentOS/RHEL/Ubuntu Linux. How do I create a new file in the current directory on Linux. How to save a file in vi/vim Editor in Linux. How to insert text into a file with vi/vim editor.

Vi/Vim is a text editor in Linux, and you can use this tool to create/modify a file or multiple files.

Create A new File with Vi/Vim


vi tool is installed by default in your Linux system, and vim is not installed by default, so if you want to use the vim editor, you need to installed it firstly. type the following command:

# yum install vim (For CentOS Linux)

Or

$ sudo apt-get install vim (For Ubuntu/Debian linux)

Outputs:

devops@devops-osetc:~$ sudo apt-get install vim
sudo: unable to resolve host devops-osetc
Reading package lists... Done
Building dependency tree
Reading state information... Done
Suggested packages:
ctags vim-doc vim-scripts
The following NEW packages will be installed:
vim
0 upgraded, 1 newly installed, 0 to remove and 598 not upgraded.
Need to get 0 B/1,036 kB of archives.
After this operation, 2,458 kB of additional disk space will be used.
Selecting previously unselected package vim.
(Reading database ... 178944 files and directories currently installed.)
Preparing to unpack .../vim_2%3a7.4.1689-3ubuntu1.2_amd64.deb ...
Unpacking vim (2:7.4.1689-3ubuntu1.2) ...
Setting up vim (2:7.4.1689-3ubuntu1.2) ...
update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/vim (vim) in auto mode
update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/vimdiff (vimdiff) in auto mode
update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/rvim (rvim) in auto mode
update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/rview (rview) in auto mode
update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/vi (vi) in auto mode
update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/view (view) in auto mode
update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/ex (ex) in auto mode

If you want to create a new file called fio.txt under the current directory, just execute the following command:

# vim fio.txt

Then this file will be opened. and press “:wq” to save and close the file. the file fio.txt has been created under the current directory.

Insert Text into File with vi/Vim


If you want to insert text into a file that has been created, you need to use the vi/vim command to open this file, and press “i” key in your keyboard to enter into the INSERT mode of the Vim editor. and then you can type the text into this file.

Save and close the File via Vi/Vim


If you want to save the modification of the file that is in the INSERT mode, you need to press “ESC” key to exit the INSERT mode, and type the command “:w” to save the modification.

If you want to save and close the file, and you need to type the comand “:wq” or ‘:x’.

Video: Create file with Vi/Vim

 

You might also like:

Sidebar



back to top