Skip to main content

Working with Vi editor in Unix

Working with Vi editor in Unix

Vi is a very powerful text editor on Unix.

The below resources will help you when dealing with the vi editor in Unix. 

This article is intended to work as a basic starting point or something that you can use on a regular basis for the basic needs. In short, this is a vi survival toolkit.

Basic operation modes

1. Command mode to execute commands on the file with respect to its content.
    Pressing the Esc button will take you to the Command mode.
2. Insert mode to manipulate the text content within the file.
    Pressing the "Insert" key will take you to the insert mode.

Create or Opening a file

vi filename

It will create a file if does not exist, otherwise the file will be opened.

Adding or editing text content

As soon as you open a file in vi, you will be in command mode. To edit the content, press the "Insert" key. This will take you to the Insert mode. Add the content using the keyboard.


Exiting the vi editor

This is by far the task that makes many sweat.

1. When the file content is not changed

Make sure you are in command mode pressing Esc key

:q and press enter key (make a note of the colon)

2. when the file content is changed and you do not want it to be saved[Perhaps the one thing you always should know]

Make sure you are in command mode by pressing Esc key.

:q! and then press enter key

3. when you have changed the file content and it has to be saved

Make sure you are in command mode by pressing Esc key.

:wq and then press enter key


Additional options when dealing with the text content

For the activities like deleting a fill line, some characters, forward and backward string search, please use the below reference.

https://www.cs.colostate.edu/helpdocs/vi.html

Footnote : These are learnings from encounters in the day to day work and there is quite a lot to learn with respect to this tool.

Comments

Popular posts from this blog

Public private key based login to Unix computer

Public private key based login to Unix computer We know that we can login to a Unix system in several ways. One drawback with username password mode is that there will be requirement to update the password frequently. In such cases, we go for the public key based login.  Required software 1. Putty and puttygen Putty can be downloaded from the download page of  https://www.putty.org/ The downloaded zip contains both putty and puttygen. 2. Winscp This is an optional software which gives a window when dealing with ssh, ftp, sftp connections. This can be downloaded from their official site. https://winscp.net/eng/download.php How it works In the public key based login, we will generate a public key and corresponding private key pair. We can optionally protect the private key using a password so that if someone gets your private key, they wont be able to put it into real use [The secret would be needed for loading your private key to the ssh client]. Process Create the public - private k

PDF files : Merge pdf files into one , extract the pages into new one

 Today, we will take a look into he pdf file management. Many a times we come across situations where  1. We have to extract certain pages of a large pdf file. 2. We have merge several pdf files into a single pdf file. We are covering the basic usage below. We are using Ubuntu Linux for this exercise. Merge several files into a single file 'Pdfunite' is the utility that I will be using today. In my example, I have 3 input files (pdf1.pdf, pdf2.pdf, pdf3.pdf)   Place all the files to be merged in a folder. Use the terminal to navigate to the folder. Execute the command as below: pdfunite is the utility, then we list out all the files in the sequence it has to be added to the resulting file. The last name is the name of the resulting file. The file will be created by the utility. pdfunite pdf1.pdf pdf2.pdf pdf-result.pdf  Now you can see that there is a new large file created using the given input pdf files.  Extract certain pages from a PDF file I am using the 'pdfseparate&#

Linux - How to find all the files that contains a given string

Linux - Find all files that contains a given string. In order to find all the files that contains a given string, we can make use of several commands. One of the simplest option is to use the grep command. grep -R '/path/to/theFiles/' -e 'pattern-to-search' With this execution, grep will go through all the files in the provided location recursively(since we gave -R option) and look for the string-pattern to look for. Then, it will provide a list of files that contains our given string-pattern.