Micro editor in Unix / Linux
There are many ways to edit files in Unix and for me, one of the best ways is to use the Editor to edit the micro screen orientation. This editor lets you edit the lines of content with other lines in the file.
Today you will find an upgraded version of the Micro Editor which is called VIM. VIM stands for Vi Improved.
Micro editors are often considered to be the actual standards in the Unix Editor because:
It is usually available on all versions of Unix systems;
Its implementation is similar to the table:
It requires very few resources for it;
It is more friendly than other editors like ed or ex.
You can use the Micro Editor to edit an existing file or create a new file from scratch. You can also use this Editor only to read a text file.
Start running Micro Editor in Unix / Linux
There are the following ways that you can use to start the Micro Editor:
Command Description filename Create a new file if it does not exist, otherwise open an existing file. vi -R filename Open an existing file in read-only mode. filename view Open an existing file in read-only mode.Below is an example to create a new file testfile if it did not exist in the current working directory:
$ vi testfile
You can observe the following result on the screen:
| ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ "testfile" [ New File ]
You will notice a tilde (~) on each cursor line. This tilde represents an unused line. If a line does not start with a tilde and a space appears, it may be a space, a tab, a new line or a few characters that are not visible.
So now you have opened a file to start it. Before implementing further processes, we explore some important concepts explained below.
Operating modes in Unix / Linux:
While working with the micro editor, you will casually follow the following two modes:
- Command mode : This mode allows you to perform administrative tasks such as saving files, running commands, moving the cursor, cutting and pasting lines or words, and searching and changing locations. In this mode, whatever you type into the system compiles (understands) as a command.
- Insert mode (Insert mode) : This mode allows you to insert text into the file. Everything that is typed in this mode is understood by the system as an input and eventually placed in the file.
Micro editors usually start with command mode. To enter text, the version must be in insert mode. To enter insert mode, simply type i. To exit insert mode, press the Esc key, which will set you back to command mode.
Hint : If you are unsure of the mode you are in, press the Esc key twice, and then you will be in command mode. You open a file using the Micro Editor and start typing a few characters, then enter the command mode to see the difference.
Exit vi in Unix / Linux
The command to exit the Micro Editor is : q . Once in command mode, type the colon and q and the sign! follow. If your file has been modified in some way, the Editor will warn you about this and not let you get rid of it. To ignore this message, the command to exit vi without saving the edits is : q !. This command helps you get rid of vi without saving any changes you have made to the file.
The command to save the contents of the Micro Editor is : w . You can match the above command with the quit command, either: wq and back!.
The easiest way to save changes and exit vi is the ZZ command. When you are in command mode, type ZZ and it will work similarly to the command : wq .
You can specify different file names to save by specifying the following file name: w. For example, if you want to save the file you are working into another file named filename2, you will type: wfilename2 and!! Try it yourself.
Move in a file in Unix / Linux
To move around in a file without affecting the text, you must be in command mode (press the Esc key twice). There are a few commands that you can use to move around a character at the same time.
Command Description k Move the cursor over a line. j Move the cursor down one line. h Move the cursor to the left of a character. l Move the cursor to the right of a character.There are two important points you need to keep in mind:
The vi editor is case sensitive and so you need to be careful when writing capital letters while using the command.
Most of the commands in vi can be opened with the number of times you want the action to take place. For example, 2j is moving the cursor down 2 lines.
There are many other ways to move in a file in vi. Remember that you must be in command mode (press Esc twice). Below is a table listing other commands that you can use to navigate around the file:
Command Description 0 or | Position the cursor at the beginning of the line. $ Set the back position at the end of the line. w Set the cursor position to the next word. b Set the cursor position first. ( Set the cursor position at the beginning of the current sentence. ) Set the cursor position at the beginning of the next sentence. E Move to the end of the word, which is limited to a space. { Move to the following paragraph. } Move to the previous paragraph. [[ Move to the following area. ]] Move to the previous area. n | Move to column n in the current row. 1G Move to the first line of the file. G Move to the last line of the file. n G Move to the nth line of the file. : n Move to the nth line of the file. f c Move forward to c F c Move back to c H Move to the top of the screen. n H Move to the nth line from the beginning of the screen. M Move to the center of the screen. L Move to the end of the screen. n L Move to the nth line from the end of the screen. : x A colon followed by a number will position the cursor on the line number xControl commands in Unix / Linux
The following commands are useful that you can use in parallel with the control key:
Command Description CTRL + d Move to half the screen. CTRL + d Move to half the screen. CTRL + f Move forward to a full screen. CTRL + u Move backward 1/2 of the screen. CTRL + b Move backwards one screen. CTRL + e Move the screen up one line. CTRL + y Move the screen down one line. CTRL + u Move the screen to 1/2 page. CTRL + d Move the screen to half the page. CTRL + b Move the screen to a page. CTRL + f Move the screen down to one page. CTRL + I Pull the screen back.Edit files in Unix / Linux
To edit the file, you need to insert it. There are many ways to enter insertion mode from command mode.
Command Description i Insert text before the current cursor position. I Insert text at the beginning of the current line. a Insert text after the current cursor position. A Insert the text at the end of the current line. o Create a new line to enter text under the current cursor position. O Create a new line to enter text on the current cursor position.Delete characters in Unix / Linux
The following lists important commands that can be used to delete characters and lines in an opened file:
Command Description x Delete a character under the current cursor position. X Delete a character before the current cursor position. dw Delete from the current cursor position to the next word. d ^ Delete from the current cursor position to the beginning of the line. d $ Delete from the current cursor position to the end of the line. D Delete from the current cursor position to the end of the current line. dd Delete the line that the cursor is currently on.As mentioned above, most of the commands in vi can be preceded by the number of times you want the action to take place. For example, 2x will delete 2 characters under the current cursor position and 2dd will delete the 2 lines that the cursor is currently on.
We recommend that you execute the above commands multiple times before performing more complex processes.
Change commands in Unix / Linux
You also have the ability to change characters, words, or lines in vi without deleting them. Here are the related commands:
Command Description cc Removing the contents of the line, makes you leave the insert mode. cw Change the word that the cursor is at, from the cursor position to the last w position of the word. r Change the position of the character under the cursor position. Vi returned to command mode after this position change was done. R Overrides multiple characters starting with the current character below the cursor. You must use the Esc key to stop this overwriting. s Change the position of the current character with the character you type. After that, you leave the insert mode. S Delete the line that the cursor is on and change it with a new paragraph. After the new text is entered, it still keeps the insert mode.Copy and paste commands in Unix / Linux
You can copy lines or words from a location and then paste them in another location using the following commands:
Yy Description Copy the current line. yw Copy the current word from the lowercase character w pointer above to the end of the word. p Paste the copy after the cursor position. P Paste a copy before the cursor position.Advanced commands in Unix / Linux
The following lists some of the advanced commands that simplify day-to-day editing and make it more effective at using vi:
Command Description J Enter the current line with the next line. << Move the current line to the left of a shift width. >> Move the current line to the right of a shift width. ~ Change the type of the character under the cursor position (Eg uppercase to lowercase). ^ G Press Ctrl + G at the same time to indicate the current status and file name. U Recovering the current line returns to the state before the cursor entered that line. u Undo the changes you made to the file. Enter u again to redirect the change. J Enter the current line with the next line. : f Display the current location of the file in% and file name, total file number. : f filename Reset the current name of the file. : w filename Write the file name : e filename Open a file other than its name. : cd dirname Change the current working directory to the dirname directory. : e # Use to switch between two opened files. : n In case you open multiple files using vi, you use: n to go to the next file in the series of files. : p In case you open multiple files using vi, you use: p to go to the previous file in that file series. : N In case you open multiple files using vi, you use: N to go to the previous file in that series of files. : r file Read the file and insert it after the current line. : nr file Read the file and insert it after the nth line.Search for words and characters in Unix / Linux
Micro editor has two types of search: string and character. To search for strings, commands / and? used. When you start these commands, the newly entered command will be pointed at the end of the line, where you enter a specific string you want to search for.
These two commands differ only in terms of where it takes place:
The command / search is purely forward in a file.
Order? Search backwards in a file.
The commands n and N respectively repeat the previous search in the same way or in the opposite direction. Some characters are important when using search commands and are preceded by a sign to become part of the search expression.
Character Description ^ Find at the beginning of the line. (Use at the start of a search expression.) . Matches with a single character. * Matches with 0 or more characters before. $ End of line. (Used at the end of the search expression.) [ Start a setting of matching or non-matching expressions. < Set the search expression away from the backslash to find the end or the beginning of the word. > Set the search expression away from the backslash to find the end or the beginning of the word.The search operation searches for characters in a line to find a character entered after the command. The command f and F only search for one character on the current line. The command f searches forward and F searches backwards and the cursor moves to the position of the character found.
The t and T commands search only one character on the current line, but with t, the cursor moves to the position before the character, and T searches the line in the opposite direction after the character.
Commands set in Unix / Linux
You can make changes to the display of the micro using the command : set . To use these commands, you must enter command mode then type : set followed by one of the following functions:
Command Description : set ic Ignore typeface (uppercase, lowercase) when searching. : set ai Set indentation mode automatically. : set no To set the indentation mode automatically. : set nu Display lines with lines on the left. : set sw Set the width of tab stops of a software. For example, you can set a shift width of 4 with this command : set sw = 4 : set ws If wrapscan is set, if the word is not found at the end of the file, it will try to search at the start. : set wm If this function has a value greater than 0, the editor will automatically enclose the character (word wrap). For example, to set the margin to two characters, you would type this : set wm = 2 : set ro Change the file type to "read only (read only)" : set term In terminal type. : set bf Ignore character control from input.Run the commands in Unix / Linux
Micro editor has the ability to run commands from within the Editor. To run a command, simply enter the command mode and enter the command:!.
For example, if you want to check if a file exists before you try to save the file with a given name, you can enter:! ls and you will see the result of the ls command on the screen.
When you press any key, you are returned to your micro area.
Replace text in Unix / Linux
The alternative command ( : s / ) allows you to quickly change words or groups from within your file. Here is a simple syntax:
: s / search / replace / g
The g character represents the global (globally). The result of this command is all events that take place on the line where the cursor is changed.
Important points in Unix / Linux:
Here are important points to help you succeed when using vi:
You must be in command mode to use commands. (Press the Esc key twice at any time to ensure you are in command mode).
You must be careful to use typefaces (flowers and normal) for all commands.
You must be in insert mode to enter text.
According to Tutorialspoint
Previous article: Network communication utilities in Unix / Linux
Next article: What is Shell?
You should read it
- Basic file system in Unix / Linux
- Useful commands in Unix / Linux
- System log in Unix / Linux
- File Management in Unix / Linux
- Network communication utilities in Unix / Linux
- What is Unix / Linux?
- Regular Expression in Unix / Linux
- Basic utilities: print, email in Unix
- Navigation IO in Unix / Linux
- Signal and Trap in Unix / Linux
- User management in Unix / Linux
- Use variables in Shell