-
Notifications
You must be signed in to change notification settings - Fork 0
/
imv
executable file
·101 lines (85 loc) · 2.41 KB
/
imv
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
#!/usr/bin/env bash
set -eu
function main() {
[[ $# -lt 1 ]] && echo "<filepath>" && exit 1
local filepath="$1"
[[ ! -e "$filepath" ]] && echo "$0: $filepath: No such file or directory" && exit 1
local target_basename dirpath
target_basename="$(basename "$filepath")"
dirpath="$(dirname "$filepath")"
local tmpfile
tmpfile=$(mktemp)
rm "$tmpfile"
trap "[[ -e $tmpfile ]] && rm -f $tmpfile; exit 1" 0 1 2 3 15
shopt -s expand_aliases
type >/dev/null 2>&1 'nvim' && alias vim='nvim'
local tmp_vimrc
tmp_vimrc="$(setup_tmp_vimrc)"
trap "[[ -e $tmp_vimrc ]] && rm -f $tmp_vimrc; exit 1" 0 1 2 3 15
vim -i NONE -u "$tmp_vimrc" -c "e! $tmpfile" -c "call setline(1, \"$target_basename\")" -c "start"
local exit_code=$?
[[ ! -e $tmpfile ]] || [[ $exit_code != 0 ]] && exit 1
local new_basename new_filepath
new_basename="$(cat "$tmpfile")"
new_filepath="$dirpath/$new_basename"
if [[ "$target_basename" == "$new_basename" ]]; then
echo 1>&2 "[log] imv canceled becase of same filepath"
return
fi
echo 1>&2 mv -i "$filepath" "$new_filepath"
mv -i "$filepath" "$new_filepath"
}
function setup_tmp_vimrc() {
local tmp_vimrc
tmp_vimrc="$(mktemp)"
cat >"$tmp_vimrc" <<'EOF'
set nocompatible
set autoindent
set expandtab
set incsearch
set number
set shiftwidth=4
set showmatch
set smartcase
set smartindent
set smarttab
set tabstop=4
set whichwrap=b,s,h,l,<,>,[,]
set backspace=indent,eol,start
set t_Co=256
set list
set ruler
set showcmd
set shortmess+=I
set laststatus=2
set encoding=utf-8
set fileencodings=utf-8,iso-2022-jp,cp932,sjis,euc-jp
set listchars=tab:»-,trail:-,extends:»,precedes:«,nbsp:%,eol:↲
set wildmenu wildmode=list:full
set ambiwidth=double
colorscheme desert
if ! has('nvim')
cnoremap <Tab> <Up>
cnoremap <S-Tab> <Down>
endif
au InsertEnter,VimEnter * hi StatusLine term=reverse ctermbg=5 gui=undercurl guisp=Magenta | execute "set statusline=INSERT"
au InsertLeave,VimEnter * hi StatusLine term=reverse ctermfg=0 ctermbg=2 gui=bold,reverse | execute "set statusline=NORMAL"
syntax on
nnoremap qq :q<CR>
nnoremap q! :q!<CR>
nnoremap ww :w<CR>
nnoremap wq :wq<CR>
inoremap <C-d> <Delete>
inoremap <C-f> <BS>
vnoremap <C-d> <Delete>
vnoremap <C-f> <BS>
cnoremap <C-d> <Delete>
cnoremap <C-f> <BS>
inoremap <C-h> <Left>
inoremap <C-j> <ESC>:call <SID>Down()<CR>i
inoremap <C-k> <ESC>:call <SID>Up()<CR>i
inoremap <C-l> <Right>
EOF
printf '%s' "$tmp_vimrc"
}
main "$@"