Apr 24, 2025

Vim notes

Notes. Useful commands.
  1. 游標的位置是在游標方塊的左邊,按i insert插入的話,會在游標方塊的左邊插入。如果有兩個游標方塊(比如說游標在{或}上時),游標的位置是在比較深色的那個。在Mac上是這樣子。
  2. p是貼在游標的右方。
  3. ciw - change inner word, word includes underscores but not hyphens
  4. ci' - change inner content in ' ', without ' '.只要指標在' '裡面就可以。
  5. ca' - change outer content in ' ', with ' '.
  6. ci} - change content until }, without }. 只要指標在{ }裡面就可以。
  7. ca} - change content until }, with }
  8. vi) or vi}

  1. 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.
  2. 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.
  3. delete inner bracket - di}
  4. redo undo - ctrl +r
  5. relative line number
    1. " 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!
  6. W, w
  7. m{a-z} mark, ’{a-z} jump to mark. ma - mark position into buffer a. 'a - jump to position a
  8. dt> - delete till > but without >.
  9. dT> does not work, to delete including > use df>.
  10. 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 ')' (])).

  11. v - select text, delete d, into register, paste elsewhere p.
  12. ctrl v, 4j, $, A, </b>, <esc>, <esc> - append </b> to all four lines.
  13. * - find the words matching the word under cursor, n - next match
  14. ctrl + e, y - scroll down one line or up
  15. :w <filename> - save to filename.
  16. :wq - write and quit
  17. :q! - force quit without saving.
  18. 0 - beginning of line
  19. $ - end of the line
  20. /<word> - find <word> over the whole document
  21. vim, recording register are the same as delete register 0~9, to avoid overlapping to record use register a~z.
  22. Really what I'm confused about is the following example:
    1. Copy code from my browser
    2. Go to a line in Nvim ad delete (dd)
    3. Paste. It pastes that line I just deleted. But what I want is the code I copied.
    4. 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
    5. The solution, use register a~z. So "aya) yank a ( ) into register a. paster it using "ap.
    6. https://www.baeldung.com/linux/vim-registers
    7. If you want to use registers 0~9, check behavior before use in the above webpage. It is complicated.
    8. :reg see all registers. :reg a - see register a.
  23. ciw""<ESC>P put a pair of double quotes around text. Cursor can be anywhere in the word.
  24. On my Mac, "* and "+ don't work. I have to use insert mode, and select Edit > paste.
  25. 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 iskeyword setting), but a "WORD" is always anything that isn't whitespace.

    So given this:

    foo-bar-baz
    

    If your cursor is on the z and you press b, the cursor will move back to the start of baz, then to the hyphen, then back to the start of bar, 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, because foo-bar-baz is all non-whitespace and thus a single WORD.

    :help word inside vim explains this too.

    1. vib and viB and ciB and cib don't work for me on my Mac.
  26. jump, ctrl-O (capt O,shift-o) go back, ctrl-I (capt I) go forward
  1. /upper/+23 or ?upper?+7 ->match upper and then go down 23 lines
  2. Fill in a space between all characters in multiple lines.
    1. .,/}/s/\(\p\)\p\@=/\1 /g
    2. :%s/\(.\)/\1 /g
    3. 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??
      1. q1, a Escl
      2. q2, 20@1
      3. q3, @2
      4. @2,@3 repeat
      5. q4, @2@3 -> doesn’t work, because @2 will error and stop
  3. to repeate same pattern from start, there is a trick, yy, p, k, J, that is paste this line below and then join line.
  4. how to move cursor upward? ctrl-u doesn’t work if windows mapping is flipped on. make ctrl-U scroll up by mapping
  5. what commands available in edit mode?
  6. select text and replace with ctrl-v
    ctrl-q {motion} ctrl-v
    
     
    ctrl-q {motion} d “0p
    
  7. delete until certain line “d{motion}”
    d/]
    
  8. python script auto run
    :lcd %:p:h
    :!python %
    
  9. VIM new _vimrc
    nnoremap <silent> <F5> :!python %<CR>
    nnoremap <F6> :!pdflatex %<CR>
    nnoremap <F7> :!pdflatex -shell-escape %<CR>
    nnoremap <F8> :!xelatex %<CR>
    set autochdir
    
  10. 去除所有行後面的空白
    :%s/\s\+$//e
    
     %: all lines, you can also do .,$ to set current line to last line
    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
  11. vim temporary file deal withhttps://stackoverflow.com/questions/743150/how-to-prevent-vim-from-creating-and-leaving-temporary-files
  12. 在windows install vim要手動把包含vim.exe的directory加入環境變數path中,包含使用者以及global的環境變數,然後要重新開機。


No comments: