A file editor that allows you to insert, edit, and replace text in a file.

This article focuses on the simple use of the Vim editor and the Nano editor, both of which are currently supported on macOS.

vimThe editor

The VI editor was the original editor for Unix and was later improved by the GNU project, called Vi Improved, or Vim for short. Linux has ported the Vim editor.

On macOS, you can use vi, View, or vim:

# View vim file path
qwldeMacBook-Pro:~ qwl$ which vim
/usr/bin/vim
qwldeMacBook-Pro:~ qwl$ ls -l /usr/bin/vim
-rwxr-xr-x  1 root  wheel  2129184  7 10 06:26 /usr/bin/vim
qwldeMacBook-Pro:~ qwl$ which vi
/usr/bin/vi
# Soft link points to Vim
qwllideMacBook-Pro:~ qwl$ ls -l /usr/bin/vi
lrwxr-xr-x  1 root  wheel  3 10 11  2019 /usr/bin/vi -> vim
qwldeMacBook-Pro:desktop qwl$ which view
/usr/bin/view
# Soft link points to Vim
qwldeMacBook-Pro:desktop qwl$ ls -l /usr/bin/view
lrwxr-xr-x  1 root  wheel  3 10 11  2019 /usr/bin/view -> vim
Copy the code

Through the above terminal view, you can find the connection between vi, view and Vim: vi and view are link files, which finally point to Vim.

vimbasis

Vim editor start command format: vim filename or vi filename. If filename is empty, or if filename does not exist, vim will open a new buffer for editing. If filename is not empty, vim reads the entire contents of the file into a buffer ready for editing. If the contents of the file are not large enough to display a screen, vim places a wavy line ~ on the non-file content line.

The Vim editor has two modes of operation:

  • Normal mode: When you have just opened a file to edit (or when you create a new file),vimThe editor will go into normal mode. In normal mode,vimThe editor interprets the keyboard keys as commands.
  • Insert mode: In insert mode,vimEach key entered at the current cursor position is inserted into the buffer. Press theiKey to go into insert mode. To exit insert mode and return to normal mode, press the exit key on the keyboardesc.

Normal mode, use the keyboard up, down, left and right, can control the cursor movement, there are also commands to control the cursor movement instead of the keyboard:

H: move one character left J: move one line down K: move one line up L: Move one character right CTRL + F: move one screen down CTRL + B: move one screen up G: move to the last line of the buffer num G: move to the num line of the buffer gg: move to the first line of the buffer

Normal mode has a special feature called command line mode. The command line mode provides an interactive command line where additional commands can be entered to control vim’s behavior. Enter the command line mode of the Vim editor by entering the colon: : in normal mode. There are several commands in command line mode to save the buffer data to a file and exit vim:

Q: If the buffer data is not modified, exit q! W filename: saves the file to another file. Wq: saves the buffer data to the file and exits

In insert mode, you can insert data into the buffer. Sometimes, however, after entering data into the buffer, you need to add or remove it. In normal mode, the Vim editor provides several commands to edit the data in the buffer:

X: deletes the characters from the current cursor position. Num x: deletes the num characters from the current cursor position. Dd: deletes the line where the current cursor is located. Dw: deletes the num words starting from the current cursor position. J: deletes the newline at the end of the line where the current cursor is located. U: Deletes the command of the previous edit Char: replaces a single character at the current cursor position with char. R text: Overwrites the data at the current cursor position with text until ESC is pressed

vimEditor cut and paste

When vim deletes data, it stores it in a separate register and can be retrieved with the P command.

Dd + P: Cut and paste the whole line of data. X + P: cut and paste the single character. Dw + P: cut and paste the single word

vimEditor copy and paste

The replication command in vim is y(for yank). You can use the same second character after y as the d command (yw means to copy a word, y$means to copy to the end of the line). After copying the text, move the cursor to where you want to place the text and type p. The copied text will appear at that location.

Yy: to copy the whole line, num yw: to copy a word, num y: to copy a single character, num y$: to copy from the current cursor position to the end of the line

vimVisual copy of the editor

With the above method of copying, we have no way of knowing what is being copied. The Vim editor provides a visual schema to solve this problem. To make a visual mode copy, we first move our cursor to the position where the text begins to copy, then press the V key, and finally control the cursor movement command or keyboard, to select the text to copy. Paste it with p.

vimEditor find and replace

Search: To search for a string in normal mode, first press the slash key /, then enter the string you want to search for, and then press Enter. By default, the editor will behave in one of the following ways:

  • If the text you are looking for appears after the current position of the cursor, the cursor jumps to the first position where the text appears.
  • If the text you are looking for does not appear after the current position of the cursor, the cursor goes around the end of the file and appears in the first place where the text is (indicated with a message).
  • Output an error message indicating that the text you are looking for was not found in the file.

To continue looking for the same string, there are two ways:

  • The input/According to theenterkey
  • According to thenKey, meaning:next

Substitution: You must enter the command mode to use the substitution. In common mode, colon is used to enter the command mode. The replacement command format is as follows: :s/old/new/ Move the cursor to the position where string replacement begins. Enter the command line mode and enter s/old/new. The Vim editor jumps to old at the cursor and replaces it with new.

You can make some changes to the substitution command to replace multiple pieces of text:

  • :s/old/new/g: Replaces all that appear in a rowoldfornew.
# text: Hello Hello Hello Hello Word! # Move the cursor to the place where the replacement started (at h of the first hello) and type :s/hello/hi/g # Output hi hi hi hi hi hi hi word!Copy the code
  • :num s/old/new/g: Replaces all that appear in line NUMoldfornew.
# The desert smoke straight
#hello word!
#hello hello hello hello hello word!
#hello word!
# Changhe falls yen

# enter
:2 s/hello/hi/g
# output:

# The desert smoke straight
#hi word!
#hello hello hello hello hello word!
#hello word!
# Changhe falls yen
Copy the code
  • :n,m s/old/new/g: Replaces all lines from n to moldfornew.
# The desert smoke straight
#hello word!
#hello hello hello hello hello word!
#hello word!
# Changhe falls yen

# enter
:2, 4 s/hello/hi/g
# output:

# The desert smoke straight
#hi word!
#hi hi hi hi hi word!
#hi word!
# Changhe falls yen
Copy the code
  • :% s/old/new/g: Replaces all in the entire fileold.
  • :%s/old/new/gc: Replaces all in the entire fileold, but in each occurrence prompt:replace with hi (y/n/a/q/l/^E/^Y)?

nanoThe editor

nanoThe text editor is also fromUnixMany commands are displayed at the bottom of the editor window. You can easily use it by following these command prompts:About to startnanoEditor:nano filenameAbout thefilenamewithvimEditor processing is the same.

The resources

Linux command line and shell script programming