VIM : How to create a file and write contents using vim editor

Vim (/vɪm/;[3] a contraction of 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.
so “: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
Android Android Commands Android Java Applications Application Libraries Bash / Shell Scripts Bluetooth driver Build Frameworks Commands and Packages Core Kernel C Programs Development Environment Setup Documents / Books Errors & Failures File Systems Framebuffer / Display Driver git Go Language Programs Hardware Platforms Home Kernel & Device Drivers Kernel Booting and Porting Linux, OS Concepts and Networking Linux Device Drivers Linux Host, Ubuntu, SysAdmin Linux Kernel Linux Networking Middleware Libraries, HAL NDK / Middleware / HAL Network Driver OS Concepts PHP Procfs Filesystem Programming Languages RaspberryPi Scripting and Automation Search Engine Optimisation ( SEO ) Social Media Socurce Code Management ( SCM ) System Administration, Security Testing and Debugging Uncategorized Userspace Utilities Web design and development Wordpress Yocto / Bitbake / Openembedded
Tags: ,

Leave a Reply