How to Create a New File with Vim and Write Contents ?

Vim (Vi Improved) is a clone of Bill Joy’s vi text editor program for Unix. It was written by Bram Moolenaar based on source for a port of the Stevie editor to the Amiga[4] and first released publicly in 1991. Credit – Wikipedia

$ vim filename.c

It will create a new file and show like “”filename.c” [New File]” at bottom of screen.
Now to edit and add something into the file we have to change to “Insert” mode, to do this hit “Insert” button from the keyboard, and file will be ready to edit with message like — INSERT — at the bottom of screen.

Now, using can use below keys to actually write some code.

  • Enter – If you want to add new line
  • Right, Left Arrow Key – To move cursor from right to left
  • Up, Down Arrow Key – To move cursor from one line to another
  • Space – To add space

Lets say, we will write the code as,

#include <stdio.h>

int main(void) {
        printf("Hello World");
        return 0;
}

Once you have written above code, and now you are sure that you want to
save this file, type below key sequence.

ESCAPE
:wq!

The meaning of above key sequence is,

  • ESCAPE – removes “Insert” Mode
  • :w – is for writing the file
  • q – is for quitting the file
  • ! – is for force quit.
  • “:wq!” becomes save and quit.
  • “:w” becomes only save
  • “q!” becomes quit without saving.

Now, after saving & closing file, you will see the newly created “filename.c”
as

$ ls -alh
-rw-rw-r--  1 myuser myuser   76 Aug 26 21:47 filename.c
Mixed Media Landscape Drawing x
Mixed Media Landscape Drawing

Leave a Comment

Sign up to our newsletter!

ADB Commands Android Android Applications Android Build system Application Libraries Application Stack / User Interface Bash / Shell Scripts Bluetooth driver Cloud Technologies Commands and Packages Compilation Content Management System Core Kernel C Programs Development, Debugging and Performance Tools Development and Build Development Environment Setup Django & REST Api Errors & Failures Git Hardware Platforms HTML JAVA Programs Linux, OS Concepts and Networking Linux Device Drivers Linux Host, Ubuntu, SysAdmin Linux Kernel Linux Networking Middleware Libraries, HAL Multimedia NDK / Middleware / HAL Operating System Concepts PHP Programming Languages Scripting and Automation Search Engine Optimisation ( SEO ) Social Media Source Code Management ( SCM ) System Administration, Security Testing and Debugging Uncategorized Web Design and Development Website Hosting Wordpress Yocto Embedded Linux