linux command

vi

Linux Command – vi ใช้ในการสร้างหรือแก้ไข file ข้อมูล text

 

คำสั่ง

1. สร้าง file

vi <filename>

$ vi test

หลังจากเข้าสู่หน้า vi prompt แล้วจะจะพบว่าเราไม่สามาถพิมพ์ข้อมูลลงไปได้โดยตรง จะต้องเลือกคำสั่งก่อนว่าจะทำอะไรกับ file ข้อมูล เมื่อเลือกคำสั่งแล้วถ้าจะกลับสู่ vi prompt ใหม่ก็ให้กด ESC

 

2. คำสั่งจัดการ file

:w   save file
:wq   save file และออกจาก vi
:q   ออกจาก vi ถ้าไม่มีการแก้ไขข้อมูล
:q!   ออกจาก vi โดยไม่สนว่าข้อมูลมีการแก้ไขหรือไม่
:e name   เปลี่ยนชื่อ file
:e!   เอา save file version ล่าสุดกลับมา
:e + name   โหลด file เพื่อแก้ไข และเลือน cursor ไปบรรทัดสุดท้าย
:e +n name   โหลด file เพื่อแก้ไข และเลือน cursor ไปบรรทัด n
:e #   แก้ไข file อันใหม่
^^   เหมือน  :e #
:w name   save ข้อมูลลง file ใหม่
:w! name   save ข้อมูลทับลง file เดิม

 

3. เขียนหรือแทรกข้อมูลลง file

i   แทรกข้อมูลก่อน cursor
a   เพิ่มข้อมูลหลัง cursor
A   เพิ่มข้อมูลท้ายบรรทัด
I   แทรกข้อมูลหน้าบรรทัด
o   แทรกบรรทัดถัดไปของ cursor
O   แทรกบรรถัดก้อน cursor
rx   เปลี่ยนตัวอักษรทับ 1 ตัว
R   เปลี่ยนตัวอักษรทับหลายๆตัว

 

4. ลบข้อมูล หรือ บรรทัดใน file

x   ลบตัวอักษรด้านขวา cursor 1 ตัว
nx   ลบตัวอักษรด้านขวา cursor n ตัว
X   ลบตัวอักษรด้านซ้าย cursor 1 ตัว
nX   ลบตัวอักษรด้านซ้าย cursor n ตัว
D   ลบบรรทัด 1 บรรทัดสุดท้าย
d$   ลบบรรทัด สุดท้ายจนถึง บรรทัด cursor
dd or :d   ลบบรรทัด cursor
ndw   ลบคำถัดไปจาก cursor n คำ
ndb   ลบคำก่อนหน้า cursor n คำ
ndd   ลบบรรทัดแรกจนถึง บรรทัด cursor
:n,md   ลบบรรทัดที่ n จนถึง m

 

5. การย้าย cursor

h   Move left
j   Move down
k   Move up
l   Move right
Arrow Keys   These do work, but they may be too slow on big files. Also may have unpredictable results when arrow keys are not mapped correctly in client.
w   Move to next word
W   Move to next blank delimited word
b   Move to the beginning of the word
B   Move to the beginning of blank delimted word
^   Moves to the first non-blank character in the current line
+ or   Moves to the first character in the next line
  Moves to the first non-blank character in the previous line
e   Move to the end of the word
E   Move to the end of Blank delimited word
(   Move a sentence back
)   Move a sentence forward
{   Move a paragraph back
}   Move a paragraph forward
[[   Move a section back
]]   Move a section forward
0 or |   Move to the begining of the line
n|   Moves to the column n in the current line
$   Move to the end of the line
1G   Move to the first line of the file
G   Move to the last line of the file
nG   Move to nth line of the file
:n   Move to nth line of the file
fc   Move forward to c
Fc   Move back to c
H   Move to top of screen
nH   Moves to nth line from the top of the screen
M   Move to middle of screen
L   Move to botton of screen
nL   Moves to nth line from the bottom of the screen
Control-d   Move forward ½ screen
Control-f   Move forward one full screen
Control-u   Move backward ½ screen
Control-b   Move backward one full screen
CTRL-e   Moves screen up one line
CTRL-y   Moves screen down one line
CTRL-u   Moves screen up ½ page
CTRL-d   Moves screen down ½ page
CTRL-b   Moves screen up one page
CTRL-f   Moves screen down one page
CTRL-I   Redraws screen
z   z-carriage return makes the current line the top line on the page
nz   Makes the line n the top line on the page
z.   Makes the current line the middle line on the page
nz.   Makes the line n the middle line on the page
z-   Makes the current line the bottom line on the page
nz-   Makes the line n the bottom line on the page
%   Move to associated ( ), { }, [ ]

 

 

โครงสร้างคำสั่ง

 vim [options] [file ..]
 vim [options] -
 vim [options] -t tag
 vim [options] -q [errorfile]

 

รายละเอียด

เป็นคำสั่งที่ใช้ในการสร้างหรือแก้ไข file ข้อมูล text จัดอยู่ในกลุ่ม editor พื้นฐานบนระบบ Unix และ Linux

 

Option

 The options may be given in any order, before or after filenames. Options without an argument can be combined after a single dash.

 +[num] For the first file the cursor will be positioned on line "num". If "num" is missing, the cursor will be positioned on the last line.

 +/{pat} For the first file the cursor will be positioned in the line with the first occurrence of {pat}. See ":help search-pattern" for the available search patterns.

 +{command}

 -c {command}
 {command} will be executed after the first file has been read. {command} is interpreted as an Ex command. If the {command} contains spaces it must be enclosed in dou‐
 ble quotes (this depends on the shell that is used). Example: Vim "+set si" main.c
 Note: You can use up to 10 "+" or "-c" commands.

 -S {file} {file} will be sourced after the first file has been read. This is equivalent to -c "source {file}". {file} cannot start with '-'. If {file} is omitted "Session.vim"
 is used (only works when -S is the last argument).

 --cmd {command}
 Like using "-c", but the command is executed just before processing any vimrc file. You can use up to 10 of these commands, independently from "-c" commands.

 -A If Vim has been compiled with ARABIC support for editing right-to-left oriented files and Arabic keyboard mapping, this option starts Vim in Arabic mode, i.e. 'arabic'
 is set. Otherwise an error message is given and Vim aborts.

 -b Binary mode. A few options will be set that makes it possible to edit a binary or executable file.

 -C Compatible. Set the 'compatible' option. This will make Vim behave mostly like Vi, even though a .vimrc file exists.

 -d Start in diff mode. There should be two, three or four file name arguments. Vim will open all the files and show differences between them. Works like vimdiff(1).

 -d {device} Open {device} for use as a terminal. Only on the Amiga. Example: "-d con:20/30/600/150".

 -D Debugging. Go to debugging mode when executing the first command from a script.

 -e Start Vim in Ex mode, just like the executable was called "ex".

 -E Start Vim in improved Ex mode, just like the executable was called "exim".

 -f Foreground. For the GUI version, Vim will not fork and detach from the shell it was started in. On the Amiga, Vim is not restarted to open a new window. This option
 should be used when Vim is executed by a program that will wait for the edit session to finish (e.g. mail). On the Amiga the ":sh" and ":!" commands will not work.

 --nofork Foreground. For the GUI version, Vim will not fork and detach from the shell it was started in.

 -F If Vim has been compiled with FKMAP support for editing right-to-left oriented files and Farsi keyboard mapping, this option starts Vim in Farsi mode, i.e. 'fkmap' and
 'rightleft' are set. Otherwise an error message is given and Vim aborts.

 -g If Vim has been compiled with GUI support, this option enables the GUI. If no GUI support was compiled in, an error message is given and Vim aborts.

 -h Give a bit of help about the command line arguments and options. After this Vim exits.

 -H If Vim has been compiled with RIGHTLEFT support for editing right-to-left oriented files and Hebrew keyboard mapping, this option starts Vim in Hebrew mode, i.e.
 'hkmap' and 'rightleft' are set. Otherwise an error message is given and Vim aborts.

 -i {viminfo}
 When using the viminfo file is enabled, this option sets the filename to use, instead of the default "~/.viminfo". This can also be used to skip the use of the .vim‐
 info file, by giving the name "NONE".

 -L Same as -r.

 -l Lisp mode. Sets the 'lisp' and 'showmatch' options on.

 -m Modifying files is disabled. Resets the 'write' option. You can still modify the buffer, but writing a file is not possible.

 -M Modifications not allowed. The 'modifiable' and 'write' options will be unset, so that changes are not allowed and files can not be written. Note that these options
 can be set to enable making modifications.

 -N No-compatible mode. Reset the 'compatible' option. This will make Vim behave a bit better, but less Vi compatible, even though a .vimrc file does not exist.

 -n No swap file will be used. Recovery after a crash will be impossible. Handy if you want to edit a file on a very slow medium (e.g. floppy). Can also be done with
 ":set uc=0". Can be undone with ":set uc=200".

 -nb Become an editor server for NetBeans. See the docs for details.

 -o[N] Open N windows stacked. When N is omitted, open one window for each file.

 -O[N] Open N windows side by side. When N is omitted, open one window for each file.

 -p[N] Open N tab pages. When N is omitted, open one tab page for each file.

 -R Read-only mode. The 'readonly' option will be set. You can still edit the buffer, but will be prevented from accidently overwriting a file. If you do want to over‐
 write a file, add an exclamation mark to the Ex command, as in ":w!". The -R option also implies the -n option (see below). The 'readonly' option can be reset with
 ":set noro". See ":help 'readonly'".

 -r List swap files, with information about using them for recovery.

 -r {file} Recovery mode. The swap file is used to recover a crashed editing session. The swap file is a file with the same filename as the text file with ".swp" appended. See
 ":help recovery".

 -s Silent mode. Only when started as "Ex" or when the "-e" option was given before the "-s" option.

 -s {scriptin}
 The script file {scriptin} is read. The characters in the file are interpreted as if you had typed them. The same can be done with the command ":source! {scriptin}".
 If the end of the file is reached before the editor exits, further characters are read from the keyboard.

 -T {terminal}
 Tells Vim the name of the terminal you are using. Only required when the automatic way doesn't work. Should be a terminal known to Vim (builtin) or defined in the
 termcap or terminfo file.

 -u {vimrc} Use the commands in the file {vimrc} for initializations. All the other initializations are skipped. Use this to edit a special kind of files. It can also be used to
 skip all initializations by giving the name "NONE". See ":help initialization" within vim for more details.

 -U {gvimrc} Use the commands in the file {gvimrc} for GUI initializations. All the other GUI initializations are skipped. It can also be used to skip all GUI initializations by
 giving the name "NONE". See ":help gui-init" within vim for more details.

 -V[N] Verbose. Give messages about which files are sourced and for reading and writing a viminfo file. The optional number N is the value for 'verbose'. Default is 10.

 -v Start Vim in Vi mode, just like the executable was called "vi". This only has effect when the executable is called "ex".

 -w {scriptout}
 All the characters that you type are recorded in the file {scriptout}, until you exit Vim. This is useful if you want to create a script file to be used with "vim -s"
 or ":source!". If the {scriptout} file exists, characters are appended.

 -W {scriptout}
 Like -w, but an existing file is overwritten.

 -x Use encryption when writing files. Will prompt for a crypt key.

 -X Don't connect to the X server. Shortens startup time in a terminal, but the window title and clipboard will not be used.

 -y Start Vim in easy mode, just like the executable was called "evim" or "eview". Makes Vim behave like a click-and-type editor.

 -Z Restricted mode. Works like the executable starts with "r".

 -- Denotes the end of the options. Arguments after this will be handled as a file name. This can be used to edit a filename that starts with a '-'.

 --echo-wid GTK GUI only: Echo the Window ID on stdout.

 --help Give a help message and exit, just like "-h".

 --literal Take file name arguments literally, do not expand wildcards. This has no effect on Unix where the shell expands wildcards.

 --noplugin Skip loading plugins. Implied by -u NONE.

 --remote Connect to a Vim server and make it edit the files given in the rest of the arguments. If no server is found a warning is given and the files are edited in the current
 Vim.

 --remote-expr {expr}
 Connect to a Vim server, evaluate {expr} in it and print the result on stdout.

 --remote-send {keys}
 Connect to a Vim server and send {keys} to it.

 --remote-silent
 As --remote, but without the warning when no server is found.

 --remote-wait
 As --remote, but Vim does not exit until the files have been edited.

 --remote-wait-silent
 As --remote-wait, but without the warning when no server is found.

 --serverlist
 List the names of all Vim servers that can be found.

 --servername {name}
 Use {name} as the server name. Used for the current Vim, unless used with a --remote argument, then it's the name of the server to connect to.

 --socketid {id}
 GTK GUI only: Use the GtkPlug mechanism to run gvim in another window.

 --version Print version information and exit.

 

กลุ่มคำสั่ง

ed, emacs, vim, gedit, nano, vimtutor(1)

 

Reference:

คำสั่ง Unix – Linux Command

Linux, Unix

 

Author: Suphakit Annoppornchai

Credit: https://saixiii.com

2 Thoughts to “vi – Linux Command คำสั่งในการสร้างหรือแก้ไข file ข้อมูล text”

Leave a Reply