Software engineering notes

Linux Vim

Command

新增、刪除、插入

複製、剪下、貼上

Visual模式

游標移動

畫面移動

在分割視窗時讓其他視窗一起 scrolling down

對每個視窗下 :set scb (:set scrollbind 縮寫), 然候再測試是否 work (ctrl + e)

取消的話對其中一個下 :set scb! (:set noscrollbind 縮寫) 就可以解除了

搜尋、取代

分割視窗

暫存器

buffer

tab

儲存、離開

vimdiff

其他操作

Execute outside commands

Json

Install jq command

Make one line into readable json

:'<,'>!jq

visual select on the JSON line, then execute jq

Make readable json into one line

:'<,'>!jq -c

visual select on the JSON line, then execute jq -c

範圍

加上註解(//)

Append text to multiple lines:

  1. - Enter Visual Block mode.
  2. Use j/k to select the lines.
  3. $ - Move cursor to last character.
  4. A - Enter insert mode after last character.
  5. Insert desired text.
  6. - Exit insert mode and finish block append.

範圍縮排

shift + v : 選出要縮排的列再按>>,這時候只會縮排一次,再按.(複製上一個動作)就可以要縮排幾次就按幾下

vim plugin

hello.vim

function! XX()
    echo getpos(".")
endfunction

隨便編輯其他文件

  1. :source hello.vim
  2. :call XX()

就會 print 出你目前的游標位子了, 如: [0, 3, 8, 0]

get external command return

let a = system('ls -al')
echo a

讓 vim 複製到 Clipboard

確認目前 vim 版本有支援此功能

(TODO: 未完成)

vim --version | grep clipboard

如果看到 +clipboard or +xterm_clipboard 就表示有

看到 -clipboard and -xterm_clipboard 表示沒有

:set clipboard=unnamed
:set clipboard+=unnamed

查看檔案的 binary hex

command:

vim test.txt

test.txt 進入後輸入 :%!xxd

0000000: 3031 3233 3435 3637 3839 4142 4344 4546  0123456789ABCDEF
0000010: 4748 494a 4b4c 4d4e 4f50 5152 5354 5556  GHIJKLMNOPQRSTUV
0000020: 5758 595a 0a                             WXYZ.

最後面的 0a 是換行字元

編輯中間的 hex

0000000: 3031 3233 3435 3637 3839 4142 4344 4546  0123456789ABCDEF

將 23 (hex 3233) 位置調換成 32 (hex 3332)

0000000: 3031 3332 3435 3637 3839 4142 4344 4546  0123456789ABCDEF

再執行 :%!xxd -r 將 hex 轉回來

0132456789ABCDEFGHIJKLMNOPQRSTUVWXYZ