- vim的documentation很爛。看他的documentation並沒有幫助。好像還是要在網路上找解決方法學比較實際。
- 游標的位置是在游標方塊的左邊,按i insert插入的話,會在游標方塊的左邊插入。如果有兩個游標方塊(比如說游標在{或}上時),游標的位置是在比較深色的那個。在Mac上是這樣子。
- p是貼在游標的右方。P是貼在游標的左方。
- A register is a named storage place. You can think of them as multiple clipboards.
- select/copy/paste/delete
- select
- 在我的mac上,我可以在vim裡直接用滑鼠選取文字。
- 按shift + 左或右 不能選取文字(重要,跟MS Word不同)
- 按v進visual mode,然後按左右選取。
- 按ctrl + v,按左右加上下選取。
- vi) or vi} or vi" or vi' 選取(), {}, " ", ' ' 內的內容,只要游標在裡面就可以。
- viw can select inner word, cursor anyhwere in the word. And if you continue pressing w, visual mode can continue selecting more words.
- (常發生)after v select, you can p paste the info in your register to replace the text v selected. Or you can ctrl v to paste the info in your system clipboard. This works on my Mac. 如果要貼上同一個內容好幾次,最好用"ay,指定register,因為當你選取要被替代的文字,並按p貼上後,貼上的內容OK,但unname register就會變成被替代的文字,這時候要再次貼上p原本的內容到別的地方,就會變成被替代的文字。重要。
- To paste the content you copy from Vim to other programs, in Vim, add "+ before yank. For example, press "+y$, this yanks the content to the end of line (y$) into system clipboard, paste into other programs with the usual ctrl v. (在Mac上是cmd v).
- v - select text, delete d, into register, paste elsewhere p.
- (常發生) 有時候按錯鍵會不小心修改到檔案,還蠻令人緊張的,因為你不知道改了什麼,比如說,要按:結果按錯變成輸入P,結果就貼入了上一次在register的內容,常常嚇一跳。還好,可以用u來復原。還蠻常發生的。再舉個例子,比方說本來要按34j,往下跳34行,結果按錯按到p,34p,結果貼上register34次,把整個版面都覆蓋滿,重點是,一開始還會覺得怎麼會這樣,因為你不曉得按錯,你只會覺得怎麼亂跳出來亂七八糟的東西。一開始學的時候,遇到這種狀況還蠻崩潰的。這種錯誤連用了十年的我現在還是會犯。
- ciw - change inner word, word includes underscores but not hyphens
- ci' - change inner content in ' ', without ' '.只要指標在' '裡面就可以。
- ca' - change outer content in ' ', with ' '.
- ci} - change content in { }, without }. 只要指標在{ }裡面就可以。
- ca} - change content in { }, with }
- for the above 5 changes, cursor can be outside and before the word, '', {}, []. Vim will jump into the brackets. Doesn't work when cursor is after the brackets.
- daw, cursor anywhere in a word, including the space in front of or after the word. Generally after the word unless punctuation is encountered.
- cit, change content in between like <div> and </div>
- same principle applies to cat.
- yaw, cursor anywhere, including the space before or after the word. Same as above.
- yiw, exclude the space
- vim 跟中文輸入切換有點麻煩。常常是esc切換回normal mode以後按指令,但是在中文輸入狀態,因此指令無效。雖然網路上有一些補包可以安裝,但效果好像不夠好。因為他是設定insert後自動進入中文輸入模式,esc後自動解除中文模式。但有時候insert還是需要英文輸入。目前好像無解。好像是要另一個鍵盤專門輸入中文?
- v virtual mode, $ select whole content to the end of line, this will include the change line character. You can see this by the slight color change of the last cursor. So when you p paste the paste content will include the change line character. So you will change line. To not include the change line character you don't want to include the last empty selection. So when you press $ you hit h to go left one letter to deselect the change line character.
- If cursor is at the left parenthesis, % will go to the matching right parenthesis.
- if cursor is at the left parenthesis, d% will delete till the matching right parenthesis. But if cursor is within the parenthesis, d% will delete from cursor point to the front til matching left bracket backward.
- delete inner bracket - di}, cursor anywhere in the {}.
- redo undo - ctrl +r
- to repeat the search /, press n
- relative line number
" turn relative line numbers on :set relativenumber :set rnu " turn relative line numbers off :set norelativenumber :set nornu " toggle relative line numbers :set relativenumber! :set rnu!
- 但是當在rnu on時,如果用滑鼠中鍵上下移動視窗,或者用ctrl -e,y上下移動視窗,當視窗超出滑鼠所在的行的時候,滑鼠會移動行,這樣relative line number會跟著一起移動!
- yank till the end is, y$, not yt$, although it is easier to remember as yt$
- W, w
- let's say you have \draw[step=0.5in, gray, very thin] and you want to get \draw[very thin]. Put the cursor in step=0.5in and press d2W. Doesn't work.
- m{a-z} mark, ’{a-z} jump to mark. ma - mark position into buffer a. 'a - jump to position a
- lower case letters a-z are for local jump, within a file
- upper case letters A-Z can cross files. If the file is closed, vi will open that file. And file must be saved before jumping, vi will ask you to save. The current file will be closed, and the file to be jumped to will be opened in the current window.
- (不對) 這個很好用,有時候關了檔案再開啟後,也可以用'a jump到想要編輯的地方。不行,關檔後,mark就消失了。怎麼又可以了?好像同一個檔案,並且同一個terminal session就可以?
'"是跳回 最後離開檔案的位置(last exit position)。好像還蠻常用的。- mark 用的abc...等register跟copy的abc...register不一樣,不像record跟copy共用abc...等的register。
- record register 跟copy register用的是相同的register. qa 跟 "ayy,後面的指令會覆蓋先前的指令。
- jump to last position, '', 按單引號兩次。比如說本來要按v按錯鍵按到n,游標跳到下一個search,想要跳回來,就按兩次單引號。
- dt> - delete till > but without >.
- dT> does not work, to delete including > use df>.
- d/pattern, delete lines from the current position up to a line matching a pattern/string
- d/END, This deletes from the cursor position through the line containing
END. - d/, delete till the next space
- delete until certain line “d{motion}” d/]
Let's say that I have a code like this (
|represents the cursor position):func1(x|, func2(), y);I would like to get:
func1(x|);d])which means delete (
d) to the next unmatched ')' (])).- lets say we have <b s d> <b s d> <b s d> <b s d> and we are at the beginning of line, and we want to jump to the third <, we can do 2/<. 或者2f<也可以。
- add things to the ends of several lines of different length. For example, ctrl v, 4j, $, A, </b>, <esc> append </b> to the ends of all four lines of different length. Only ctrl v can do this. V can't.
- * - find the words matching the word under cursor, n - next match
- ctrl + e, y - scroll down one line or up
- how to move cursor upward? ctrl-u doesn’t work if windows mapping is flipped on. make ctrl-U scroll up by mapping
- :w <filename> - save to filename.
- :wq - write and quit
- :q! - force quit without saving.
- 0 - beginning of line
- $ - end of the line
- /<word> - find <word> over the whole document
- vim, recording register are the same as delete register 0~9, to avoid overlapping to record use register a~z.
- "a5yy, yank 5 lines into register a
- "byy, yank one line into register b
- "cyy, yank another line into register c
- "ap, paste 5 lines from register a
- "bp, paste one line from register b
- "cp, paste another line from register c
- I get this is how it works with delete but curious if this is an issue I'm causing by the order I do things. Is it better to delete the line first before copy/pasting from the browser
- The solution, use register a~z. So "aya) yank a ( ) into register a, cursor anywhere inside (). paster it using "ap.
- https://www.baeldung.com/linux/vim-registers
- If you want to use registers 0~9, check behavior before use in the above webpage. It is complicated.
- :reg see all registers. :reg a - see register a.
- (常用) ciw""<ESC>P put a pair of double quotes around text. Cursor can be anywhere in the word. 前面的ciw可以換成其他指令,也可以用d delete,但就要額外按I insert,所以用c change是有原因的,不過道理其實一樣。或者是v select特定內容後c""<ESC>P。
- (常發生)如果要貼上同一個內容好幾次,最好用"ay,指定register,因為當你選取要被替代的文字,並按p貼上後,貼上的內容OK,但unname register就變成被替代的文字,這時候要再次貼上p原本的內容到別的地方,就會變成被替代的文字。
- On my Mac, "* and "+ don't work. I have to use insert mode, and select Edit > paste.
Like most of the capitalized movement pairs, b moves by word, but B moves by WORD. The difference is that vim considers a "word" to be letters, numbers, and underscores (and you can configure this with the
iskeywordsetting), but a "WORD" is always anything that isn't whitespace.So given this:
foo-bar-bazIf your cursor is on the
zand you press b, the cursor will move back to the start ofbaz, then to the hyphen, then back to the start ofbar, and so on. Each of these is a different "word" to vim:foo,-,bar,-,baz.But if you press B, the cursor will move all the way left to the
f, becausefoo-bar-bazis all non-whitespace and thus a single WORD.:help wordinside vim explains this too.- vib and viB and ciB and cib don't work for me on my Mac.
- split window with two files, :split <filename> when in one file.
- jump, ctrl-O (capt O,shift-o) go back, ctrl-I (capt I) go forward
- /upper/+23 or ?upper?+7 ->match upper and then go down 23 lines
- Fill in a space between all characters in multiple lines.
- .,/}/s/\(\p\)\p\@=/\1 /g
- :%s/\(.\)/\1 /g
- The following recording only partially work. Still not as neat as the first solution. If recording like "q0a Esclq" then then "q115q0j0" then "@1" meet the end of line, it will not move to next line??
- q1, a Escl
- q2, 20@1
- q3, @2
- @2,@3 repeat
- q4, @2@3 -> doesn’t work, because @2 will error and stop
- to repeate same pattern from start, there is a trick, yy, p, k, J, that is paste this line below and then join line.
- what commands available in edit mode?
- select text and replace with ctrl-v
ctrl-q {motion} ctrl-vctrl-q {motion} d “0p - python script auto run
:lcd %:p:h :!python %
- VIM new _vimrc
nnoremap <silent> <F5> :!python %<CR> nnoremap <F6> :!pdflatex %<CR> nnoremap <F7> :!pdflatex -shell-escape %<CR> nnoremap <F8> :!xelatex %<CR> set autochdir
- 去除所有行後面的空白%: all lines, you can also do .,$ to set current line to last line
:%s/\s\+$//e
s: substitute
/:command separator
\s:a white space
\+:one or more preceding atom
$:end of line
/:command separator
/e:no error message when space not found - vim temporary file deal withhttps://stackoverflow.com/questions/743150/how-to-prevent-vim-from-creating-and-leaving-temporary-files
- 在windows install vim要手動把包含vim.exe的directory加入環境變數path中,包含使用者以及global的環境變數,然後要重新開機。
在 macOS Terminal(包括 zsh / bash)裡很常用的是:
Ctrl + A→ 跳到 command 開頭Ctrl + E→ 跳到 command 尾端
常用快捷鍵小抄(bash / macOS Terminal)
按鍵 功能 Ctrl + A到行首 Ctrl + E到行尾 Ctrl + U刪除游標左邊 Ctrl + K刪除游標右邊 Ctrl + W刪除前一個單字 Ctrl + C取消目前指令 Ctrl + R搜尋歷史指令 如果你常在 Terminal 打 LilyPond、編譯或跑長指令,
Ctrl + R加上Ctrl + U、Ctrl + W這幾個快捷鍵會非常省時間。
No comments:
Post a Comment