devFancy BE Developer

vi Editor의 다양한 명령어 정리

2021-09-07
devfancy

Prologue

  • VI 편집기에 대해 명령어들을 제대로 복습하고 정리하기 위해 해당 글을 작성하게 되었다.

About vi ( file editing work )

  • vi(pronounced “vee-eye”)

    • A screen-oriented text editor

    • The standard Unix editor

    • It is short for “visual”

    • There are lots of clones(vim, nvi, elvis, macvim)

vi Modes

  • Command Mode ( default mode )

    • Command mode is the mode you are in when you start

    • Move cursor, Move page, Delete, Copy, Paste,,,

  • Insert(or Text) Mode ( Edit )

    • The mode in which text is created

    • Literally typed into the document (bottom - <–INSERT–> )

    • Insert Mode -> Command Mode : Just press ESC

  • (Additional) Ex Mode

    • Special mode of Command mode

    • Search, Save, Replace, Move line, Quit

    • Ex Mode -> Command Mode : Just press ESC

Starting vi

  • $vi

    • Start with new file

    • When you save this file, you should set the file name

  • $vi filename

    • Open a file with vi

    • Ex) Type: vi myfile.txt

      • If myfile.txt does not exist, myfile.txt created

      • If myfile.txt does exist, the first few line of the file will appear.

From Command Mode to Insert Mode

Key Actions
i Insert text ‘before’ current character
a Insert text ‘after’ current character
I Begin text insertion at the “beginning of a line”
A Begin text at “end of a line”
o Open a new line ‘above’ current line
O Open a new line ‘above’ current line
s Substitute one character under cursor continue to insert

Ex mode( Writing & Exiting in Command )

Writing (Saving)

Key Actions
:w Save current file
:w fileName Save current file to fileName

Exiting vi

Key Actions
:q Quit(will only work if file has not been changed
:q! Forcely quit
:wq Save, then quit
:wq fileName Save to fileName, then quit

Command mode

Basic Cursor Movement ( one line )

  • h(left), j(down), k(up), l(right)

  • w : Right one word(->, left to right)

  • b : Left one word(<-, right to left)

  • e : Start at the end of a word, left to right

Advanced vi_1 (in command mode) - Deleting, and Changing Text

Key Actions
x Delete a character
X Backspace delete
dd Delete a line, 10 + dd : delete 10 rows
D Delete a line, But keep deleted rows intact
r Replace a character
R Edit mode (—REPLACE—)
yy Copy row, 10 + yy : Copy 10 rows
p Paste below by my cursor
P Paste above by my cursor

Advanced vi_2 (in command mode)

Key Actions
set nu Show line Numbers
:set nonu Hide line numbers
gg, [[ Go to the beginning of the document
G, ]] Go to the end of the document
H Start of screen
M Middle of screen
L End of screen
ctrl + b Go ‘up’ one page
ctrl + f Go ‘down’ one page ‘forward’

Advanced vi_3(replacement :치환)

Form: :[number]s / [old content] / [new content]

  • (kor): :줄번호 / 기존내용 / 바꿀 내용

    • :5s / passwd / security : Replace 5th ‘passwd’ with ‘security’.

    • :20,23s / PASS / passport : Replace 20 ~ 23th ‘PASS’ with ‘passport’.

    • :%s / MIN / MAX : Replace full document

    • :numbers / old / new / g : replace at once => Mainly used.

shell command

  • :!ifconfig : Stop vi and ifconfig ip address

  • :! : Stop vi and print the working environment

  • :!command : Stop vi and run the command

  • :1,3d : delete 1 ~ 3rows

etc

  • ctrl + w + n : Divide the screen horizontally.

  • ctrl + w + o : Close all windows except the one where I am.

  • ctrl + w + w : Move the screen window.

  • :w : save

  • :e : open

  • :e [ file ] : open the file

  • :enew : make new file

  • :w » [ file ] : Adds content to the specified file and saves it

  • :f : indicates information.

    • ex) “file route” line 1 of 158 –0%– col 1

vim install & setup

  • $ sudo apt-get install vim

  • $ vi ~/.vimrc

    • set nu

    • set smartindent

    • set shftwidth=4

    • set laststatus=2

    • syntax on


Comments

Index