Creating, Viewing and Editing Text File

vi text editor:

vi stand for Visual Interface is a text-editor in Linux. vi is display-oriented: the screen of terminal acts as a window into the file your are editing. Changes you make to the file are reflected in what you see. vi editor is a full screen editor and has three modes of operation:

Command Mode:

command which cause action to be taken on the file. This mode is used for the file navigation, cut and paste, and simple commands such as undo and redo.

Insert Mode:

This mode is used for normal text editing means entered text is inserted into the file. Accessed by typing the letter “i” or “I”, we simple enter text. Most keystrokes result in text appearing on the screen. To get out of insert mode, hit the “esc” Escape key. Once you press “Esc” Escape key it will turns off the insert mode.

Last-Line Mode:

This mode is used to save, quit and open files, as well as search & replace and other operaion. Press “:” and vi will switch to Last-Line Mode. Enter a command like “:w” to write the file or “:q” to exit the editor.

Starting vi:

You may use vi to open an already existing file by typing (#vi filename) where “filename” is the name of the existing file. If the file is not in your current directory, you must use the full pathname. Or you may create a new file by typing (#vi newname) where “newname” is the name to give the new file. Once open new file on-screen, you will see blank lines, each with a tidle (~) at the left, and a line at the bottom giving the name and status of the new file:

The Instructor will demonstrate a typical file editing session using only basic Vim keystrokes.

  1. Open a file with vim filename
  2. Repeat this text entry cycle as many times as the task require.

Press I to enter insert mode

Enter text

Press esc to return to command mode.

If necessary, press u to undo mistaken edits on the current line.

  • Repeat this text deletion cycle, as many times as the task required:

Use arrow key to position the cursor

Press x to delete a selection of text

If necessary, use u to undo mistaken edits on the current line.

  • To save or exit, choose one of the following to write or discard file edits:

Enter :w to save the file and remain in command mode for more editing.

Enter :wq to save the file and quim vim.

Enter :q! to quit vim, but discard all file changes since the last write.

Rearranging existing text

In vim, copy and paste is known as yank and put, using command characters y and p. Begin by positioning the cursor on the first character to be selected, then enter visual mode. Use arrow keys to expand the visual selection. When ready, press y to yank the selection into memory. Position the cursor at the new location, then press p to put the selection at the cursor.

The instructor will demonstrate “yank and put” using visual mode.

  1. Open file with Vim filename.
  2. Repeat this text entry cycle as many times as the task require.

Use arrow key to position the cursor to the first character

Press v to enter visual mode.

Use arrow keys to position the cursor to the last character.

Press y to yank the selection

Use arrow key to position the cursor at the insert location

Press p to put the selection

  • To save or exit, choose one of the following to write or discard file edits:

Enter :w to save the file and remain in command mode for more editing.

Enter :wq to save the file and quim vim.

Enter :q! to quit vim, but discard all file changes since the last write.

EXAMPLES:

Creating a vim file
[cms@asim ~]$ touch /home/cms/Desktop/f1
[cms@asim ~]$ touch /home/cms/Desktop/f1

[cms@asim Desktop]$ touch f2
[cms@asim Desktop]$ vim f2
Creating a new file with vim.
[cms@asim Desktop]$ vim f3

Constructing pipelines

Redirection controls channel output to or from files while piping sends channel output to another process

Examples for processing piping redirection

Paginate a commands long output
[cms@asim ~]$ ls -l /usr/bin | less
total 146420
-rwxr-xr-x. 1 root root      41544 Oct 30  2018 [
-rwxr-xr-x. 1 root root     107904 Oct 30  2018 a2p
-rwxr-xr-x. 1 root root      52720 Apr 24 09:46 ab
-rwxr-xr-x. 1 root root      11336 Nov 13  2018 abrt-action-analyze-backtrace
-rwxr-xr-x. 1 root root      11320 Nov 13  2018 abrt-action-analyze-c
-rwxr-xr-x. 1 root root       1345 Nov 13  2018 abrt-action-analyze-ccpp-local
-rwxr-xr-x. 1 root root       6821 Nov 13  2018 abrt-action-analyze-core
-rwxr-xr-x. 1 root root      11304 Nov 13  2018 abrt-action-analyze-oops
-rwxr-xr-x. 1 root root      11312 Nov 13  2018 abrt-action-analyze-python
-rwxr-xr-x. 1 root root       2814 Nov 13  2018 abrt-action-analyze-vmcore
-rwxr-xr-x. 1 root root       1348 Nov 13  2018 abrt-action-analyze-vulnerability
-rwxr-xr-x. 1 root root      11360 Nov 13  2018 abrt-action-analyze-xorg
-rwxr-xr-x. 1 root root       5002 Nov 13  2018 abrt-action-check-oops-for-hw-error
-rwxr-xr-x. 1 root root      11344 Nov 13  2018 abrt-action-generate-backtrace
-rwxr-xr-x. 1 root root      11328 Nov 13  2018 abrt-action-generate-core-backtrace
-rwxr-xr-x. 1 root root       8341 Nov 13  2018 abrt-action-install-debuginfo
-rwxr-xr-x. 1 root root       3207 Nov 13  2018 abrt-action-list-dsos
-rwxr-xr-x. 1 root root       8958 Nov 13  2018 abrt-action-notify
-rwxr-xr-x. 1 root root       3535 Nov 13  2018 abrt-action-perform-ccpp-analysis
-rwxr-xr-x. 1 root root       1292 Nov 13  2018 abrt-action-save-kernel-data
-rwxr-xr-x. 1 root root      23800 Nov 13  2018 abrt-action-save-package-data
-rwxr-xr-x. 1 root root      15480 Nov 13  2018 abrt-action-trim-files
-rwxr-xr-x. 1 root root      48720 Nov 13  2018 abrt-applet
-rwxr-xr-x.
Count the number of lines in an output or listing.
[cms@asim ~]$ ls | wc -l > /home/cms/Desktop/f1
Grab the first line, last line, or selected lies of command output.
[cms@asim ~]$ ls -t | head -n 10 > /tmp/ten-last-changed-files

Determine the terminal device for the current window. Send the results as mail and view the same results in this window.

[cms@asim ~]$ tty
/dev/pts/0

Leave a Reply

Your email address will not be published.