2018-1-2linux Basic Knowledge VI Editor and bash arithmetic

Source: Internet
Author: User
Tags arithmetic arithmetic operators


In the previous chapter we talked about the basics of vim, such as how we open and close files, how to edit, and so on, as well as the transformations between the many modes of vim and the common editing commands, then we introduce the last-line mode of vim and the use of the built-in command-line interface.

first, Vim last line mode

Vim's last-line mode makes the VIM Editor's function perfect, making its editor a more powerful tool with many commands in the last-line mode, so here's a more common usage:

1.1 Address Delimitation

Refers to the text within the range of the corresponding editing operations, separated by commas to indicate the starting position and end position, the command format is as follows:

: Start_pos[,end_pos]

   from the above format can be seen, the end of the subsequent position can be omitted, then its editing in the following ways:

   #: A particular line of #, for example, 5 is line 5th;    : Current line;    $: last row;    #,# : Specifies the row range, the left is the starting line, the right side is the end line    #,+#: Specifies the row range, the left is the starting line absolute number, the right side is the offset of the relative left row number; for example:3,+7      .,$-1     1,$: Full text;      %: full text;     / Pattern/: The line from where the cursor is actually to the end of the file for the first time that the pattern is matched;        /first/, $     /pat1/,/pat2/: Starts at the point where the cursor is located, the first line that is matched by the PAT1, and all rows between the end of the first line matched by PAT2; 

Then the address delimitation is the same as the line range we selected earlier, and can be used together with the edit command for editing:

For example: D y C W/path/to/somefile: Saves the text in the range to the specified file;

We can specify a row range for its file merge, using the R command, a space to follow the path of a file can be said to merge the contents of the file into the current file.

R/path/to/somefile: Reads and inserts the text from the specified file into the specified position;

1.2 Find

Find this feature we are not unfamiliar, when you open a Word document in Windows, you want to find a field, or use a text editor, using the Ctrl+f shortcut, it will show the search bar used to find it, We can also find a field in Linux using the less command or the man manual, but in vim, we can use not only keywords, but also patterns to find.

/pattern: Finds all strings that can be currently matched to the end of the file from the current cursor position;? PATTERN: Finds all strings that can be currently matched to the file header from the current cursor location, N: Next, same as command direction, N: Previous, opposite of command direction;

1.3 Find and replace

After saying how to do the search, then the replacement is nothing more than a search to make a processing action, say find the character or field to replace the string you want to fruit, use special command for S.

   s: Command of the last-line mode, using format:      s/What to find/Replace with content/modifiers           What to look for: you can replace with a regular expression;        : You cannot use regular expressions, but you can reference ;             if the "What to find" section uses grouping symbols in a pattern; in the Replace with content Use the & symbol to refer to all the text of the lookup pattern matching directly by referencing;            .                       modifier:             i: Ignore Case;             g: Global substitution means that a row is replaced if it matches multiple times;                  can replace delimiters with other non-commonly used characters:         [email protected]@@        s# ##

So instead of using regular expression metacharacters, you can use a back-to-reference mechanism, or you can use the direct reference mechanism to match all of the content, because it is not intended to work with the contents of the lookup.

1.4 Vim Multi-file function

Vim can open more than one text file at a time for processing, but because it is a full-screen editor, you can only display one when opening a file, so in full-screen mode we can do the operation and conversion of the file. The usage is:

Vim FILE1 FILE2 ...

Since Vim is a full-screen editor, we can switch between files in the following ways:

: Next Next:p Rev Prev: first one: Last

Exit All Files:

: Wqall:wall:qall

There is also a multi-window mode, full-screen editor because the default can only display one, we can use some of the options to multi-window to speak multiple files together display.

-O: Horizontal split window;-O: Vertical split window; toggle between windows: Ctrl+w, ARROW Note: A single file can also be split into multiple windows for viewing: ctrl+w,s: Horizontal Split window; ctrl+w,v: Vertical Split window;

1.5 customizing VIM working characteristics

The so-called job feature is nothing more than a function display, such as adding a line number or automatic indentation. So what we need to note is that the settings in the last-line mode are only valid for the current VIM process and need to be written into the configuration file to make it permanent.

Permanent: Global:/ETC/VIMRC User personal: ~/.VIMRC Note: This file may not be available, you need to create a new one;

   custom features are as follows:

   1, line number       display: Set number, abbreviated to set nu       Suppress: set nonumber, set nonu   2, brackets match highlight       match: Set  showmatch, set sm      Cancel: set nosm   3, start indent        Enable:set ai      Cancel: set noai   4, highlight Search       Enable:set hlsearch      Cancel:set nohlsearch        5, syntax highlighting       enabling:syntax on       Ignore: syntax off       6, ignore character case        Enable:set ic      disable: Set noic       7, Tab four grid      set ts=4        get help:      :help&nbsP;    :help subject 

Two, the arithmetic operation of bash script

Arithmetic operations are not unfamiliar to us, in elementary or middle school teachers teach us how to use subtraction and the second side or the remainder to calculate mathematical problems to get the correct answer to the problem, in programming they are also common arithmetic operators, but slightly different than in the book.

+, -, *, /, **, %

In bash, because bash is a weakly typed language, the default is all as a character type, so if the arithmetic is, the example is as follows:

# echo "$num 1+ $num 2" 1+2

However, if the variable is declared as an integer, it will also be treated as a string as a result operation, as a variable substitution:

# declare-i Num3=3 # declare-i num4=4 # echo "$num $num 4" 3+4

For bash, to do arithmetic, you have to use a special arithmetic format.

Arithmetic operation format: (1) Let var= arithmetic expression formula (2) var=$[arithmetic expression] (3) var=$ ((Arithmetic expression)) (4) var=$ (expr $ARG 1 $OP $ARG 2) Note: The multiplication notation requires the use of escape characters in some scenarios;


2018-1-2linux Basic Knowledge VI Editor and bash arithmetic

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.