Based on the Linux Master 2 exam syllabusReport an error
/pattern (downward) and ?pattern (upward); the next result is n and the opposite direction is N.:s/find/replace/.% means the whole file, and g means everything within a line. :%s/A/B/g is the classic form.~/.vimrc (personal) and /etc/vimrc (global).| Command | Direction |
|---|---|
/pattern | Search downward from the cursor |
?pattern | Search upward from the cursor |
n | Next result in the same direction |
N | Next result in the opposite direction |
/error search downward for error
?error search upward for error
n next result
N next result in the opposite direction/, an input line appears at the bottom of the screen.n and N is an exam point. N is not "upward"; it is the opposite of the direction you first searched. If you searched with ?, N goes downward.| How the search was first made | Where n goes | Where N goes |
|---|---|---|
/pattern | Down | Up |
?pattern | Up | Down |
| Search | What it finds |
|---|---|
/^root | Lines that start with root |
/bash$ | Lines that end with bash |
/a.c | Any single character between a and c — abc, a1c |
/\<the\> | The word the only (excludes there, other) |
/\/etc\/hosts | A string containing slashes — write it as \/ |
/ itself is the "start search" symbol, if the string you are looking for contains a slash, put a backslash in front.search hit BOTTOM, continuing at TOP.E486: Pattern not found: pattern appears.:set ic, and to temporarily turn off search highlighting use :noh (vim). : % s / Seoul / Busan / g
│ │ │ │ │ │
│ │ │ │ │ └── g everything on the line
│ │ │ │ └──────── replacement string
│ │ │ └───────────────── string to find
│ │ └────────────────────── s substitute
│ └───────────────────────── % the whole file
└──────────────────────────── : ex mode| Command | Range |
|---|---|
:s/A/B/ | Only the first A on the line with the cursor |
:s/A/B/g | All A's on the line with the cursor |
:%s/A/B/g | All A's in the whole file |
:1,10s/A/B/g | All A's on lines 1 through 10 |
:%s/A/B/gc | Replace throughout but confirm each one (confirm) |
Tell
%andgapart.%decides up to which lines, andgdecides how many within one line.:%s/A/B/g, with both attached, means "replace everything in the whole file."
This is the result of using each of four commands on the same two lines, with the cursor on line 1.
Original 1 banana apple 2 grape banana
:s/a/A/ 1 bAnana apple 2 grape banana only the first on the current line
:s/a/A/g 1 bAnAnA Apple 2 grape banana all on the current line
:%s/a/A/ 1 bAnana apple 2 grApe banana only the first on every line
:%s/a/A/g 1 bAnAnA Apple 2 grApe bAnAnA all on every line%, just "one per line" is changed. The banana on line 2 stayed as it was.| Range | Target |
|---|---|
| (omitted) | The line with the cursor |
% | The whole file — same as 1,$ |
1,10 | Lines 1 through 10 |
.,$ | From the current line to the last line |
10,$ | From line 10 to the last line |
.,+5 | The current line and the five lines below it |
. is the current line and $ is the last line. % is shorthand for 1,$.:1,5d deletes lines 1 through 5.| Command | What it does |
|---|---|
:%s/A//g | Delete all A (leave the replacement empty) |
:%s/^/#/ | Add # to the start of every line — comment out everything at once |
:%s/^#// | Remove the # at the start of lines |
:%s/A/B/gi | Replace ignoring case |
:%s#/usr/local#/opt#g | Use # as the delimiter — when replacing paths with many slashes |
:g/pattern/d | Delete entire lines containing that pattern |
/. The character right after s becomes the delimiter.c, it asks replace with B (y/n/a/q/l/^E/^Y)? at each match. y replace, n skip, a all the rest, q stop.u. One whole substitution is undone at once.g at the end (an option) and the :g at the start (a command) are different things. The option g means "everything in a line," and the command :g means "run the following command on every line that has the pattern."| Command | What it does |
|---|---|
:set nu | Show line numbers |
:set nonu | Hide line numbers |
:set ai | Auto-indent (autoindent) |
:set ts=4 | Set tab size (tabstop) |
:set ic | Ignore case when searching (ignorecase) |
:set hlsearch | Highlight search results |
:set sw=4 | Auto-indent width (shiftwidth) |
:set sm | Show matching brackets (showmatch) |
:set list | Show invisible characters, such as tabs (^I) and line ends ($) |
:set all | View the current values of all options |
:syntax on | Turn on syntax highlighting (vim) |
| Abbreviation | Full name | Abbreviation | Full name |
|---|---|---|---|
nu | number | ai | autoindent |
ic | ignorecase | ts | tabstop |
sw | shiftwidth | sm | showmatch |
To turn one off, put no in front — nonu, noai, noic, nolist. It is not that each option has its own separate off command.
Options that take a value are given with = — ts=4. The form differs from on/off options.
If you want to know the current value, add a question mark. :set ts? answers like tabstop=8 (the default is 8).
Settings given this way disappear when you close the editor.
To keep using them, write them in a configuration file.
| File | Scope |
|---|---|
~/.vimrc | Personal settings |
/etc/vimrc | Global settings (all users) |
~/.exrc | Personal settings file of the old vi |
" ~/.vimrc example
set nu
set ai
set ts=4.bashrc. If it starts with ~/ it is personal; if /etc/ it is global.set nu without a colon. Comments start with a double quote ("), unlike the shell's #./etc/vimrc on the Red Hat family and /etc/vim/vimrc on the Debian family.| Easily confused file | Actual role |
|---|---|
~/.vimrc | Settings you write yourself |
~/.viminfo | A record vim leaves automatically — search terms, command history, copied content. It is not a configuration file |
~/.exrc | The configuration file of the original vi and ex. vim reads this only when ~/.vimrc does not exist |
.filename.swp | A swap file created during editing. Used for recovery after an abnormal exit |
^G Get Help ^O Write Out ^X Exit
└ ^ means the Ctrl key| Shortcut | What it does |
|---|---|
Ctrl+O | Save (Write Out) |
Ctrl+X | Exit (Exit) |
Ctrl+W | Search |
Ctrl+K | Cut one line |
Ctrl+G | Help |
| Shortcut | What it does |
|---|---|
Ctrl+U | Paste the cut line |
Ctrl+R | Read in the contents of another file |
Ctrl+\ | Replace a string |
Ctrl+C | Show the current cursor position |
Ctrl+X
Save modified buffer? Y Yes N No ^C Cancel ← Y
File Name to Write: memo.txt ← EnterCtrl+X after editing, it asks whether to save. After Y, you confirm the file name and press Enter, and it saves and exits. It does not hold out with just a warning, as vi does.^ is the Ctrl key and M- is the Alt (Meta) key.| Shortcut | What it does |
|---|---|
Ctrl+X Ctrl+S | Save |
Ctrl+X Ctrl+C | Exit |
Ctrl+G | Cancel a command |
| Shortcut | What it does |
|---|---|
Ctrl+X Ctrl+F | Open a file (find file) |
Ctrl+S / Ctrl+R | Search downward / upward |
Ctrl+K | Cut from the cursor to the end of the line (kill) |
Ctrl+Y | Paste (yank) |
Ctrl+A / Ctrl+E | Start / end of the line |
Ctrl+X u | Undo |
Ctrl+X Ctrl+S means holding Ctrl while pressing x, then s. The emacs documentation writes this as C-x C-s. C- is Ctrl and M- is Meta (Alt).Ctrl+X and differ only in the following letter — with Ctrl+S it is save, with Ctrl+C it is exit.yy is copy, while emacs's Ctrl+Y is paste.Ctrl+A, Ctrl+E, Ctrl+K, and Ctrl+R on the bash command line are exactly emacs-style shortcuts. The keys we used last week come from here.| Category | vi / vim | emacs | nano |
|---|---|---|---|
| Creator | Bill Joy | Richard Stallman | GNU (pico improvement) |
| Modes | Yes (3) | None | None |
| Save | :w | Ctrl+X Ctrl+S | Ctrl+O |
| Exit | :q | Ctrl+X Ctrl+C | Ctrl+X |
| Configuration file | ~/.vimrc | ~/.emacs | ~/.nanorc |
| Character | Available everywhere | Highly extensible | Easy to learn |
The answer to "Which editor has modes?" is vi. emacs and nano have no modes.
| What you want to do | vi / vim | emacs | nano |
|---|---|---|---|
| Save and quit | :wq | Ctrl+X Ctrl+S then Ctrl+X Ctrl+C | Ctrl+X then Y, Enter |
| Quit without saving | :q! | Ctrl+X Ctrl+C then answer no to saving | Ctrl+X then N |
| Search | /pattern | Ctrl+S | Ctrl+W |
| Cut one line | dd | Ctrl+K (cursor to end of line) | Ctrl+K |
| Paste | p | Ctrl+Y | Ctrl+U |
Ctrl combination it is emacs or nano; if it is a colon or a single letter it is vi. When they are mixed in the choices, split them by this criterion first.Ctrl+X it is nano; if it takes Ctrl+X Ctrl+C twice it is emacs. The exit keys are similar, so questions swap them.Ctrl+K is cut in both editors, but pasting differs: Ctrl+Y in emacs and Ctrl+U in nano.Tomorrow we cover running programs: the concept and types of processes.
/pattern : The vi command that searches for a string downward from the cursor.n / N : Keys that move through search results in the same direction / the opposite direction.:s/A/B/.% : A range symbol that makes the whole file the target of a substitution.g : An option that makes a substitution replace every match within a line.~/.vimrc : vim's personal configuration file. The global configuration is /etc/vimrc.Click a choice to reveal the answer and explanation.
Question 1
Which of the following vi commands replaces every apple in the whole file with orange?
Question 2
Which of the following vi commands displays line numbers on the screen?
Question 3
Which of the following is the file that stores vim's personal environment settings?
Question 4
Which of the following editors has no distinct editing modes, so you can type characters immediately after launching it?
Question 5
Which of the following is the shortcut that saves what you have written in the nano editor?