~gpanders/dotfiles

ref: 12291d3163062a0a3c879f53a0b0d02541376ca4 dotfiles/vim/.vim/after/ftplugin/xxd.vim -rw-r--r-- 2.6 KiB
12291d31Greg Anders [vim] Make git commit buffers in vim always start at line 1 2 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
" xxd filetype
" Author: Greg Anders <greg@gpanders.com>
" Written: 24 Dec 2018

if &filetype !=# 'xxd'
  finish
endif

normal! 11|

" Number of bytes per line. xxd default is 16
let b:bytes_per_line = 16

setl cursorline cursorcolumn

" Remap h and l to move byte-wise
noremap <silent> <buffer> h :<C-U>call <SID>ByteLeft()<CR>
noremap <silent> <buffer> l :<C-U>call <SID>ByteRight()<CR>
noremap <silent> <buffer> e :<C-U>call <SID>ByteRight()<CR>l
noremap <silent> <buffer> w :<C-U>call <SID>WordRight()<CR>
noremap <silent> <buffer> W :<C-U>call <SID>WordRight()<CR>
noremap <silent> <buffer> b :<C-U>call <SID>WordLeft()<CR>
noremap <silent> <buffer> B :<C-U>call <SID>WordLeft()<CR>
noremap <silent> <buffer> ge :<C-U>call <SID>ByteLeft()<CR>h
noremap <silent> <buffer> 0 11<Bar>
noremap <silent> <buffer> ^ 11<Bar>

augroup xxd
  autocmd!
  autocmd InsertLeave,TextChanged <buffer> call <SID>Update()
augroup END

let b:undo_ftplugin = 'setl cul< cuc< | au! xxd'

" If &binary is set (with vim -b), use xxd to edit the file like a hex editor
function! s:Update()
  " Update the ASCII representation of the data after modifying hex values
  let view = winsaveview()
  let [_, lnum, col, _, _] = getcurpos()
  let line = getline('.')
  let linelen = strlen(line)
  let asciicolstart = linelen - b:bytes_per_line
  if col > asciicolstart
    " ASCII section
    " Place cursor at first non-whitespace character
    call cursor(lnum, asciicolstart)
    let ascii = line[asciicolstart-1:linelen-1]
    for i in range(1, b:bytes_per_line)
      call searchpos('[A-Za-z0-9]\{2\}', 'b', lnum)
      let char = ascii[b:bytes_per_line-i]
      if char !=# '.'
        let nr = printf('%.2x', char2nr(char))
        execute "normal! R" . nr . "\<Esc>h"
      endif
    endfor
  else
    silent %!xxd -r
    silent %!xxd
  endif
  call winrestview(view)
endfunction!


function! s:ByteLeft()
  let [_, lnum, col, _, _] = getcurpos()
  if col <= 11
    return
  elseif col > 11 && col <= 52
    call search('[A-Za-z0-9]\{2\}', 'b', lnum)
  else
    execute 'normal! h'
  endif
endfunction!

function! s:ByteRight()
  let [_, lnum, col, _, _] = getcurpos()
  if col >= 11 && col < 48
    call search('[A-Za-z0-9]\{2\}', '', lnum)
  elseif col == 48
    execute 'normal! w'
  else
    execute 'normal! l'
  endif
endfunction!

function! s:WordLeft()
  let [_, lnum, col, _, _] = getcurpos()
  if col <= 11
    if lnum > 1
      execute 'normal! 0B'
    else
      return
    endif
  else
    execute 'normal! B'
  endif
endfunction

function! s:WordRight()
  let [_, lnum, col, _, _] = getcurpos()
  if col >= 52
    execute 'normal! 2W'
  else
    execute 'normal! W'
  endif
endfunction