Articles Comments

Oracle DBA & All IT » Programming, Unix » การใช้งาน vi editor บน unix

การใช้งาน vi editor บน unix

Vi คืออะไร (What is vi?)

Vi (visual editor) คือ editor พื้นฐานในระบบปฏิบัติการ Unix ซึ่งเป็นโปแกรม editor ใช้สำหรับ เพิ่ม ลบ แก้ไข ไฟล์ข้อมูลใดๆ โดยมีคำสั่งพื้นฐานดังนี้

UNIX vi editor มี  2 โหมด:

  1. Command mode เป็นคำสั่งสำหรับทำการใดๆกับไฟล์
  2.  Insert mode ใช้สำหรับการใส่ข้อมูลลงไฟล์
    กดปุ่ม <Esc> (Escape) สำหรับยกเลิกการใช้โหมด insert  

NOTE:

ทั้ง UNIX และ vi เป็น case-sensitive. (ตัวเล็กตัวใหญ่ต่างกัน)

 


เริ่มต้น vi (Start vi)

เริ่มต้น ใช้ vi ตามด้วยชื่อไฟล์  ถ้าไฟล์มีอยู่แล้ว จะเป็นการเปิดไฟล์นั้นๆ ขึ้นมาเพื่อทำการแก้ไข ถ้าไฟล์ยังไม่มีจะเป็นการสร้างไฟล์ใหม่ขึ้นมา
 

 

*

vi filename

edit filename starting at line 1

 

vi -r filename

recover filename that was being edited when system crashed

     

 

 

การออกจาก vi (Exit vi)

หลังจากที่เราทำการสร้าง หรือ แก้ไขไฟล์เรียบร้อยแล้ว เมื่อเราจะออกจาก vi เราสามารถที่จะออกโดย บันทึกหรือ ไม่บันทึกไฟล์ที่เราสร้างหรือแก้ไขก็ได้
Note: เมื่อเราทำการกดปุ๋ม (:)  cursor จะอยู่ที่ด้านล่างของหน้าจอ แล้วให้เราทำการพิมพ์คำสั่งที่เราต้องการแล้วจบคำสั่งด้วยการกดปุ่ม <Return> (or <Enter>)

 

 

*

:x<Return>

quit vi, writing out modified file to file named in original invocation

 

:wq<Return>

quit vi, writing out modified file to file named in original invocation

 

:q<Return>

quit (or exit) vi

*

:q!<Return>

quit vi even though latest changes have not been saved for this vi call

     

 

 

การเลื่อน cursor ใน vi (Moving the Cursor)

ใน vi ไม่สามารถจะใช้ mouse ย้าย cursor ได้เหมือนกับบน pc เราต้องย้ายโดยการใช้ คีย์สั่งด้านล่างเท่านั้น

ในตารางด้านล่าง สัญลักษณ์l ^ ที่มาก่อนตัวอักษรหมายถึงให้กดปุ่ม <Ctrl> ค้างไว้ขณะที่กดตัวอักษรที่ตามหลัง
 

 

*

j or <Return>
  [or down-arrow]

move cursor down one line

*

k [or up-arrow]

move cursor up one line

*

h or <Backspace>
  [or left-arrow]

move cursor left one character

*

l or <Space>
  [or right-arrow]

move cursor right one character

*

0 (zero)

move cursor to start of current line (the one with the cursor)

*

$

move cursor to end of current line

 

w

move cursor to beginning of next word

 

b

move cursor back to beginning of preceding word

 

:0<Return> or 1G

move cursor to first line in file

 

:n<Return> or nG

move cursor to line n

 

:$<Return> or G

move cursor to last line in file

     

 

 

การจักการหน้าจอ (Screen Manipulation)

คำสั่งต่อไปนี้สำหรับย้ายหน้าจอที่เราต้องการดู
 

 

 

^f

move forward one screen

 

^b

move backward one screen

 

^d

move down (forward) one half screen

 

^u

move up (back) one half screen

 

^l

redraws the screen

 

^r

redraws the screen, removing deleted lines

     

 

 

การเพิ่ม, เปลี่ยนแปลง และลบข้อมู (Adding, Changing, and Deleting Text)

คำสั่งด้านล่างใช้สำหรับย้อนกลับไปข้อมูลก่อนหน้าที่ทำการแก้ไข 1 step ไม่สามารถย้อนกลับไปมากกว่านี้
 

 

*

u

UNDO WHATEVER YOU JUST DID; a simple toggle

     

 

 

การใส่ข้อมูล (Inserting or Adding Text)

คำสั่งต่อไปนี้จะใช้สำหรับใส่ข้อมูลลงไฟล์ ทุกครั้งที่ใช้คำสั่งใน insert โหมด ต้องการการกดปุ่ม <Esc> เพื่อยกเลิกโหมด insert ทุกครั้ง
 

 

*

i

insert text before cursor, until <Esc> hit

 

I

insert text at beginning of current line, until <Esc> hit

*

a

append text after cursor, until <Esc> hit

 

A

append text to end of current line, until <Esc> hit

*

o

open and put text in a new line below current line, until <Esc> hit

*

O

open and put text in a new line above current line, until <Esc> hit

     

 

 

การเปลี่ยนแปลงข้อมูล (Changing Text)

คำสั่งสำหรับการเปลี่ยนแปลงข้อมูล
 

 

*

r

replace single character under cursor (no <Esc> needed)

 

R

replace characters, starting with current cursor position, until <Esc> hit

 

cw

change the current word with new text,
starting with the character under cursor, until <Esc> hit

 

cNw

change N words beginning with character under cursor, until <Esc> hit;
  e.g., c5w changes 5 words

 

C

change (replace) the characters in the current line, until <Esc> hit

 

cc

change (replace) the entire current line, stopping when <Esc> is hit

 

Ncc or cNc

change (replace) the next N lines, starting with the current line,
stopping when <Esc> is hit

     

 

 

การลบข้อมูล (Deleting Text)

คำสั่งสำหรับการลบข้อมูล
 

 

*

x

delete single character under cursor

 

Nx

delete N characters, starting with character under cursor

 

dw

delete the single word beginning with character under cursor

 

dNw

delete N words beginning with character under cursor;
  e.g., d5w deletes 5 words

 

D

delete the remainder of the line, starting with current cursor position

*

dd

delete entire current line

 

Ndd or dNd

delete N lines, beginning with the current line;
  e.g., 5dd deletes 5 lines

     

 

 

การคัดลอกและแปะข้อมูล (Cutting and Pasting Text)

คำสั่งสำหรับการคัดลอกและแปะข้อมูล
 

 

 

yy

copy (yank, cut) the current line into the buffer

 

Nyy or yNy

copy (yank, cut) the next N lines, including the current line, into the buffer

 

p

put (paste) the line(s) in the buffer into the text after the current line

     

 

 

คำสั่งอื่น (Other Commands)

การค้นหาข้อมูล (Searching Text)

คำสั่งสำหรับค้นหาคำในไฟล์
 

 

 

/string

search forward for occurrence of string in text

 

?string

search backward for occurrence of string in text

 

n

move to next occurrence of search string

 

N

move to next occurrence of search string in opposite direction

     

 

 

แสดงเลขบรรทัด (Determining Line Numbers)

คำสั่งตารางด้านล่างนี้ สามารถที่จะโชว์บรรทัดปัจจุบัน, จำนวนรวมของบรรทัด เป็นต้น
 

 

 

:.=

returns line number of current line at bottom of screen

 

:=

returns the total number of lines at bottom of screen

 

^g

provides the current line number, along with the total number of lines,
in the file at the bottom of the screen

     

 

 

การบันทึกและอ่านไฟล์ (Saving and Reading Files)

 

 

:r filename<Return>

read file named filename and insert after current line
(the line with cursor)

 

:w<Return>

write current contents to file named in original vi call

 

:w newfile<Return>

write current contents to a new file named newfile

 

:12,35w smallfile<Return>

write the contents of the lines numbered 12 through 35 to a new file named smallfile

 

:w! prevfile<Return>

write current contents over a pre-existing file named prevfile

     

 

 

 

.. Preview : 15124

Related Search:

  • vi editor คือ
  • วิธีการใช้ vi
  • vi mode
  • การใช้งาน vi
  • vi unix
  • vi editor

Written by

บอกเล่าสิ่งที่พบเจอมาในการทำงาน ประสบการณ์การทำงานด้าน DBA ถ่ายทอดกันด้วยภาษาง่ายๆ บ้านๆ " ทุกอย่างไม่อยาก แต่... แค่ตั้งใจไม่พอ ต้องลงมือทำ และทำ GoodLuck " Fanpage: www.facebook.com/DBAor .. "Oracle Database Consultant " ..

Filed under: Programming, Unix · Tags: , , , , , ,

  • Added, I enjoy your site! 🙂

    • Never seen a beettr post! ICOCBW

    • Times are changing for the better if I can get this onilne!

  • Super information! I have been previously seeking something like this for a long time now. Thank you!

    • Just cause it’s silmpe doesn’t mean it’s not super helpful.

    • Wow! That’s a really neat anwser!

  • Bookmarked, I enjoy your blog! 🙂

    • Thank God! Someone with brains spkeas!

  • You relaly found a way to make this whole process easier.

  • Your article was excellent and eruitde.