Cygwin & vim Yu Hsiang Zheng (Slighten)
Outline Shell Cygwin vim 1/21
What is a computer 2/21
What is a computer 拿 地 球 來 做 比 喻 的 話 kernel: 地 心 shell: 地 殼 application: 房 子 各 種 建 築 物 shell = command interpreter = user interface shell 大 抵 上 分 兩 種 GUI (graphical user interface, 圖 形 化 使 用 者 介 面 ): Windows UI CUI (character user interface, 文 字 型 使 用 者 介 面 ): sh, bash, csh, tcsh, ksh, zsh a.k.a. CLI (command-line interface, 命 令 行 介 面 ) 3/21
Shell 的 功 能 User 透 過 在 terminal ( 像 cmd, Cygwin terminal) 上 輸 入 指 令 (commands),shell 直 譯 它, 並 傳 給 kernel,kernel 再 驅 動 hardware 4/21
Shell on different Systems 5/21
What can I do in a shell? 列 出 檔 案 (file listing): ls 重 新 命 名 (renaming, move A to B): mv A B 進 入 不 同 資 料 夾 (change directory): cd 創 造 資 料 夾 (make directory): mkdir 刪 除 資 料 夾 (remove directory): rmdir 編 輯 (edit files with a text editor): vi, vim, emacs, nano 編 譯 組 譯 (compile, assemble files): cc, g++, as, lcs 跑 程 式 (run programs):./a.exe,./a.out 登 入 其 他 server (login to another machine, secure shell): ssh 查 詢 某 個 指 令 的 輔 助 說 明 (manual):man 6/21
What is a command 其 實 是 個 執 行 檔 ( 在 /bin 裡 ) 格 式 : 動 詞 (+ 受 詞 ) (+ 附 加 屬 性 ) (+ 附 加 屬 性 受 詞 ) (+ 附 加 屬 性 ) (+ 附 加 屬 性 受 詞 ) ls ls a ls abc/ ls a abc/ vim abc.cpp vim b a.exe g++ abc.cpp g++ abc.cpp o abc.exe 7/21
Basic shell commands % is a prompt % ls # list files % cd dirname # change directory % cd.. # go up one level % cd # go to home directory % mkdir dirname # make a directory % pwd # print working directory % more filename # look at a file % cat filename # dump file content 8/21
More shell commands % mv oldname newname # rename a file as newname, # or "moving" it to another location % cp file1 file2 # copy file1, call new one file2, or # copy file of same name to new loc % rm filename # remove a file % rmdir dir # remove directory 9/21
What is Cygwin Linux-like (Unix-like) environment for Windows making it possible to port software running on POSIX systems (such as Linux, BSD, and Unix systems) to Windows. 簡 單 來 說 就 是 在 Windows 上 的 Linux 模 擬 器 10/21
Demo: Install Cygwin Minimally, it is recommended that you install g++ (C++ 編 譯 器 ) (Gnu Compiler Collection). Specifically, make - Develop > clang: C/C++/ObjC Compiler frontend based on LLVM Develop > gcc-g++: GNU Compiler Collection (C++) Develop > gdb: The GNU Debugger Develop > mingw-gcc-g++: GNU Compiler Collection (C++) Develop > make: The GNU version of the make utility vim (vi-improved text editor). it is at Editors > vim: Vi IMproved - enhanced vi editor 11/21
Text editors in Unix VI: (command-line) 純 文 字 編 輯 器 (plain text editor), available in most CLI http://docs.freebsd.org/44doc/usd/12.vi/paper.pdf VIM: does 語 法 高 亮 (syntax highlighting) EMACS: (command-line; also window) powerful programmable text editor 12/21
VI: modal editor modal 就 是 有 模 式 (mode) 的, 擁 有 幾 種 不 同 mode 做 切 換 像 Caps Lock ( 大 寫 vs 小 寫 ), Insert vim 有 2 種 mode 1. 命 令 模 式 (Command mode) # 操 作 模 式 一 開 啟 檔 案 default 的 模 式 keys interpreted as commands move cursor, delete, copy/paste, i (insert), a (append), or o (open to) go into Insert mode 2. 插 入 模 式 (Insert mode) # 打 字 模 式 keys interpreted as data in document Esc to go back to Command mode 13/21
Basic vi commands in Command mode: h # left j # down k # up l # right w # skip a word forward b # skip a word backward i # insert (go to insert mode) Esc # back to command mode (with "escape" key, not typing E s c) G # 跑 到 頁 尾 gg # 跑 到 頁 首 14/21
More vi commands in Command mode: dd # delete one line d2d # delete 2 lines dw # delete word p # paste (deleted or yanked item) yy # "yank" a line (copy onto "clipboard") { # back paragraph } # forward paragraph. # repeat last insert / # find next pattern? # find previous pattern 15/21
vi line commands in Command mode: :w # save :q # quit :q! # quit without saving :w filename # save to another file name :e # filename edit another file :set nu # show line numbers :set nonu # hide line numbers :set ic # case-insensitive search :set noic # case-sensitive 16/21
Question: How do I save & quit? 1. :w then :q 2. or just :wq 3. or ZZ 17/21
vim: vi-improved New feature, including syntax highlighting more flexible cursor movements Syntax highlighting, do :syntax on or :syntax Multiple-undo and redo u undo, ^R redo 18/21
Custom file bash:.bashrc (shell script) vim:.vimrc 在 執 行 bash 前.bashrc 會 先 被 載 入 想 在 執 行 bash 時, 重 新 載 入.bashrc 可 使 用 指 令 : source.bashrc 在 執 行 vim 前. vimrc 會 先 被 載 入 所 以 基 本 上.xxxrc 都 是 使 用 者 自 定 的 設 定 檔 (rc == resource) 在 Linux/Unix 上,. 開 頭 的 檔 案 都 是 隱 藏 檔 需 ls a 才 看 得 到 19/21
Some problems with vim 在 Insert mode 下, 可 能 不 能 backspace 刪 字 需 要 在 command mode 打 上 這 行 解 決 :set backspace=start,eol,indent 你 可 以 在 根 目 錄 下 創 建.vimrc 寫 在 裡 面 這 樣 每 次 開 起 vim 都 會 OK 一 樣, 若 不 喜 歡 每 次 開 vim 都 要 :syntax on 或 :set nu 可 以 都 把 它 們 都 寫 在.vimrc 裡 面 20/21
More materials 請 參 考 周 百 祥 教 授 寫 的 Unix Guide with Vi Tutorial.pdf 21/21