Linux has one of the most robust, extensive command line interfaces among Operating Systems. However, they are more often used to navigate the file system, copy/move files, or for simple tasks which does not utilize the full potential of the Linux Shell. I will attempt to compose a series of posts which will explain how to use simple commands built inside Linux to complete tedious tasks with ease.
In the first post, I am going to share a few tricks on using Vi.
Vi, as you may be aware, is the command line text editor predominantly used in Linux Distributions. Virtually every Linux user must have used Vi. But other than a few edits a vast majority may have not used the advanced commands of Vi.
Let's see a few tips on Vi. Use the samples attached to this post to try out the examples shown below:
1. Bookmarks inside file:
Suppose you are editing a very long file, and you would like to go back to line 23. In this case,
First , go to line 23 by entering ":23" in ex mode (Command Mode).
and hit ma (m, then a) to create a mark named “a” in line 23
Now, to go back to the Mark "a", go back to ex mode and hit
To Create multiple marks, hit "m" followed by the letter (eg: md for a mark named "d".
To view all the marks on a document, hit ":marks"
To view a particular mark, use :marks followed by the mark name. For example, to view the marks a and c, use ":marks ac"
2. Insert an existing file into the current document.
Suppose you are editing a file, and you want to add the contents of another file into the current file you are editing. In this case in ex mode(command mode), type ":r <filename>"
For example, entering ":r /etc/redhat-info" will insert the contents of the
/etc/redhat-info file into the current document.
3. Inserting the output of a command:
If you want to enter the output of a command, try ":r !<command>"
Examples:
":r !uname -a" will enter the kernel and OS details into the document.
":r !date" will insert the current date into the document.
":r !hostname" will insert the hostname into the document.
We'll see more on Part 2.
In the first post, I am going to share a few tricks on using Vi.
Vi, as you may be aware, is the command line text editor predominantly used in Linux Distributions. Virtually every Linux user must have used Vi. But other than a few edits a vast majority may have not used the advanced commands of Vi.
Let's see a few tips on Vi. Use the samples attached to this post to try out the examples shown below:
1. Bookmarks inside file:
Suppose you are editing a very long file, and you would like to go back to line 23. In this case,
First , go to line 23 by entering ":23" in ex mode (Command Mode).
and hit ma (m, then a) to create a mark named “a” in line 23
Now, to go back to the Mark "a", go back to ex mode and hit
`a
(backtick, followed by a) . The cursor will move to line 23 again.To Create multiple marks, hit "m" followed by the letter (eg: md for a mark named "d".
To view all the marks on a document, hit ":marks"
To view a particular mark, use :marks followed by the mark name. For example, to view the marks a and c, use ":marks ac"
2. Insert an existing file into the current document.
Suppose you are editing a file, and you want to add the contents of another file into the current file you are editing. In this case in ex mode(command mode), type ":r <filename>"
For example, entering ":r /etc/redhat-info" will insert the contents of the
/etc/redhat-info file into the current document.
3. Inserting the output of a command:
If you want to enter the output of a command, try ":r !<command>"
Examples:
":r !uname -a" will enter the kernel and OS details into the document.
":r !date" will insert the current date into the document.
":r !hostname" will insert the hostname into the document.
We'll see more on Part 2.