How to create a new file with vim

How do I create and edit a file via SSH using a text editor such as vim on a Linux or Unix-like system?

There are many ways to create a new file in Linux or Unix-like system. One can use a text editor such as vi or vim to create and edit a file. This page explains how to use vim text editor to create a file via SSH.

VIM text editor commands

Vim is a text editor to create or edit a text file, config file and programs on a Linux, macOS, and Unix-like system. There are two modes in vim:

  1. Command mode : In this mode you can move around the file, delete text, copy/paste/undo/redo and more.
  2. Insert mode : In this mode you can insert text or edit text.

How do I change mode from one to another when using vim?

To switch from insert mode to command mode press or type escape key (Esc). If you are ever unsure about something you typed, just press ESC to place you in Normal mode. Then retype the command you wanted.

To switch from command mode to insert mode type any one of the following characters:

  • a : Append text following the current cursor position
  • A : Append text to the end of the current line
  • i : Insert text at the current cursor position
  • I : Insert text at the beginning of the cursor line
  • o : Begin a new line below the cursor to insert text
  • O : Begin a new line above the cursor to add text

How do I move the cursor around when working with vim?

To move the cursor, press the h,j,k,l keys as indicated:

       < h       l >              
  1. The h key is at the left and moves left.
  2. The l key is at the right and moves right.
  3. The j key looks like a down arrow and moved down.
  4. The k key looks like a up arrow and moved up.
  5. You can use up, down, left and right arrow keys too. But, with hjkl keys you will be able to move around much faster.

Step to create and edit a file using vim

  1. Log into your server using SSH command: ssh [email protected]
  2. Type vim command to create a file named demo.txt: vim demo.txt
    Vim started with a new blank file
  3. To enter insert mode and to append text type the letter i (usually you do this by press Esc followed by i)
    Enter INSERT mode in vim
  4. Start entering text
  5. When done editing press the Esc key to go back to command mode
  6. Once in command mode to save and exit the file type a colon (:) followed by x. Press the Enter key.
    To save the file and exit the vim

I suggest you read vim command tutorial by simply typing the following command at the CLI:
$ vimtutor

Posted by: SXI ADMIN

The author is the creator of SXI LLC and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

Previous Post

How to Make Website WCAG Compliant?

Next Post

Link download Kali Linux 2020.1 (ISO + Torrent)

Related Posts