Learn about Micro applications in Linux

For Linux users, Vi - text editing application is one of the indispensable support tools . Unlike Nano - a text editing application in Terminal format, Vi has a variety of useful and functional shortcut keys, with 2 main modes of operation: Insert and Command.

Micro editor in Linux

  1. Open Micro on Linux
  2. Command mode
  3. Insert mode
  4. Saving and Quiting
  5. Write a very small C program using Vi
  6. Common options for opening files in Vi
  7. Move between characters
  8. Common commands in Vi
  9. Copy and paste command in Vi (Practice!)
  10. Advanced micro command
  11. Work with two or more files (Practice!)

Open Micro on Linux

Vi is actually a Terminal application, so you will have to boot from the corresponding Terminal window. Use the vi / path / to / file syntax to open the text file available with Vi, and the command will also work if the specified text file is not available, instead Vi will create a text file with that name. .

Learn about Micro applications in Linux Picture 1

Note that we need to use the sudo command if we want to modify the file system, for example, if we want to edit the system's Fstab file, type:

sudo vi / etc / fstab

If you use a non-Ubuntu operating system version of Linux, replace sudo with su .

Command mode

Here is the image when we open the file with Vi, it looks like we can type the character here, but it's not really. Vi is actually a drafting application, and is opened in Command mode:

Learn about Micro applications in Linux Picture 2

When in Command mode, we can move the cursor by pressing the arrow key, pressing x to delete the character just below the cursor, and pressing dd to delete the entire character line. Besides, you can choose, copy, cut and save the text in this mode. Move the cursor to the left or right of the letter to copy and press the v key, press x to cut the text, then place the cursor where you need to move the text and press p to paste:

Learn about Micro applications in Linux Picture 3

Insert mode

This is Vi's operating mode that allows users to insert characters into the text. Just press the I button we have switched to Insert mode after defining the cursor position in Command mode:

Learn about Micro applications in Linux Picture 4

When entering the necessary text, press the Escape key to return to Command mode.

Saving and Quiting

You can save and exit the application in Command mode (press Escape to make sure we are in this mode). Type: wq to save the file after changing and closing Vi, or executing it separately into 2 steps: w to save the file and: q to exit the program without saving the changes:

Learn about Micro applications in Linux Picture 5

But Vi will not let the user close the application if it has changed since the last save, type: q! And press Enter to ignore this warning:

Learn about Micro applications in Linux Picture 6

Write a very small C program using Vi

Open a Terminal.

Please enter:

 vi file.c 

This will create a new file.

Learn about Micro applications in Linux Picture 7

Vi in Command mode by default. So if you want to write something, you can't do this.

Type 'i' to be able to write your code.

Learn about Micro applications in Linux Picture 8

Now, you will be in Insert mode , but if you want to delete a character, you cannot do it.

Type ESC to change to Command mode.

In Command mode, type 'x'. This will delete the character under the cursor.

Learn about Micro applications in Linux Picture 9

You can insert a character to the left of the cursor by typing 'i'.

You can insert the character to the right of the cursor by typing 'a'.

Insert a '>' in the text.

Learn about Micro applications in Linux Picture 10

Return to Command mode and type : wq, then return:

 wq 

Now you are at the command prompt. Please see the contents of your file using cat.

Learn about Micro applications in Linux Picture 11

You may be thinking that Vi is very complicated, but in reality it is not. Vi is a fairly simple editor, but it can only work in a single mode at a time.

Re-open your file ( vi file.c ) and write the following code:

 void main(void){ printf('vi is a great tool to write code faster '); } 

Save your file and if you have gcc installed, you should compile and run it.

Learn about Micro applications in Linux Picture 12

Learn about Micro applications in Linux Picture 13

Common options for opening files in Vi

  1. vi file: Create a new file if it does not exist. If not, it will open the existing file.
  2. vi -R file: Read-only mode (Read only)

Move between characters

  1. You must be in Command mode.
  2. You can use the keys: Up, down, left and right arrows.

Alternatively, you can use other keys:

k
Move the cursor up
j
Move the cursor down
hour
Move the cursor to the left
l
Move the cursor to the right

Common commands in Vi

Note : You must be in Command mode.

Order
Describe
i
Insert the text before the current cursor position.
l
Insert text at the beginning of the current line.
a
Insert text after the current cursor position.
A
Insert text at the end of the current line.
o
Create a new line for the text entry below the cursor.
O
Create a new line for the text entry above the cursor.
x
Delete the character below the cursor.
X
Delete the character before the cursor.
dd
Delete the cursor line being placed.
cc
Delete the content of the line, to the user in Insert mode.
r
Replace the character below the cursor.

Copy and paste command in Vi (Practice!)

Create a new file named "linux-distro".

Write the following list:

  1. Ubuntu
  2. Linux Mint
  3. Debian
  4. Slackware
  5. Red Hat

Learn about Micro applications in Linux Picture 14

Change to Command mode (ESC).

Move the cursor to "Ubuntu".

Type yy (this is a command to copy a line).

Type G.

Insert a new line by typing o.

Change to Command mode (ESC).

Type P (This command is for pasting lines).

Learn about Micro applications in Linux Picture 15

Type 1G

Type 4yy.

Type G.

Type P.

Learn about Micro applications in Linux Picture 16

Alternatively, you can use other copy - paste commands:

  1. yw: Copy from current.
  2. p : Set the copied text after the cursor.

Advanced micro command

<<
Join the current line and the next line. Some participate in multiple lines.
>>
Align the current line to the left with a variable width.
: nr file
Read the file and insert it after the line n.
~
Convert case / lower case of characters below the cursor.
^ G
Press CTRL and G keys at the same time to display the file name and current status.
U
Restore the current line to the state before the cursor enters the line.
u
Undo the last change to the file. Re-enter "u" again to make changes again.
J
Join the current line and the next line. Some participate in multiple lines.
: f
Displays the current location in the file with% and file name, total file.
: f filename
Rename the current file to filename.
: filename w
Write the filename of the file.
: e filename
Open a file other than filename.
: cd dirname
Change the current working directory to dirname.
: e #
Use to switch between open files.
: n
In case you open multiple files with Vi, use: n to move to the next file in the string.
: N
In case you open multiple files using vi, use: N to move to the previous file in the string.
: r file
Read the file and insert it after the current line

Work with two or more files (Practice!)

Open the linux-distros file

In Command mode, type:

 :e unix 

Write " UNIX is a good OS " and save it.

Go to the linux-distros file with the command:

 :e # 

Exit Vi by typing:

 :q 

Good luck!

See more:

  1. Basic Shell commands in Linux
  2. Certain deadly commands never run on Linux
  3. How to use the Vim editor
  4. 5 best free code editors
4 ★ | 1 Vote

May be interested

  • How to run Windows applications on Linux with WinePhoto of How to run Windows applications on Linux with Wine
    there are a number of applications from windows that are needed but there are no alternatives on linux. fortunately, there is a software project that allows you to run windows applications on linux. this software is called wine. it creates a compatibility layer for windows programs to interact with the linux operating system.
  • How to create USB Boot Live Kali LinuxPhoto of How to create USB Boot Live Kali Linux
    the most popular and fastest way to apply kali linux is to run it from a usb drive. this method has many advantages. to do this, we first need to create a bootable usb drive from an iso image of kali linux.
  • How to set up a Windows virtual machine in LinuxPhoto of How to set up a Windows virtual machine in Linux
    do you need to run windows software in linux? one of the typical answers is to install windows in a virtual machine. if that sounds scary to you, don't worry too much.
  • How to install Kali Linux dual boot WindowsPhoto of How to install Kali Linux dual boot Windows
    parallel installation kali linux and windows can be quite helpful. however, you need to be cautious during the setup process. first, make sure you back up any important data on your windows installation. because you will modify the hard drive, please store this backup on external devices. when you have finished backing up data, we recommend reading the article how to install kali linux on your computer - including explaining the processes for basic kali installation.
  • Things you need to know about Kali Linux ImagePhoto of Things you need to know about Kali Linux Image
    to run 'live' potassium from a usb drive on a standard windows and apple computer, you'll need a bootable linux kali iso image, in 32-bit or 64-bit format.
  • How to update Kali Linux and fix errors when updatingPhoto of How to update Kali Linux and fix errors when updating
    kali linux's excellent penetration testing system like any other system in the world needs to be updated. but most updates are not developed by the kali linux founding team but are created by programmers themselves.