forked from mutewinter/dot_vim
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfunctions.vim
143 lines (129 loc) · 2.97 KB
/
functions.vim
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
" ----------------------------------------
" Functions
" ----------------------------------------
" ---------------
" Strip Trailing White Space
" ---------------
" From http://vimbits.com/bits/377
" Preserves/Saves the state, executes a command, and returns to the saved state
function! Preserve(command)
" Preparation: save last search, and cursor position.
let _s=@/
let l = line(".")
let c = col(".")
" Do the business:
execute a:command
" Clean up: restore previous search history, and cursor position
let @/=_s
call cursor(l, c)
endfunction
function! StripTrailingWhiteSpaceAndSave()
:call Preserve("%s/\\s\\+$//e")<CR>
:write
endfunction
command! StripTrailingWhiteSpaceAndSave :call StripTrailingWhiteSpaceAndSave()<CR>
nnoremap <silent> <leader>stw :silent! StripTrailingWhiteSpaceAndSave<CR>
" ---------------
" Write Buffer if Necessary
"
" Writes the current buffer if it's needed, unless we're the in QuickFix mode.
" ---------------
function! WriteBufferIfNecessary()
if &modified && !&readonly
:write
endif
endfunction
command! WriteBufferIfNecessary call WriteBufferIfNecessary()
function! CRWriteIfNecessary()
if &filetype == "qf"
" Execute a normal enter when in Quickfix list (for opening files)
execute "normal! \<enter>"
else
WriteBufferIfNecessary
endif
endfunction
" Idea for MapCR from http://git.io/pt8kjA
function! MapCR()
if exists('g:vscode')
nnoremap <silent> <enter> :Write<CR>
else
nnoremap <silent> <enter> :call CRWriteIfNecessary()<CR>
endif
endfunction
call MapCR()
" ---------------
" Make a scratch buffer with all of the leader keybindings.
"
" Adapted from http://ctoomey.com/posts/an-incremental-approach-to-vim/
" ---------------
function! ListLeaders()
silent! redir @b
silent! nmap <LEADER>
silent! redir END
silent! new
silent! set buftype=nofile
silent! set bufhidden=hide
silent! setlocal noswapfile
silent! put! b
silent! g/^s*$/d
silent! %s/^.*,//
silent! normal ggVg
silent! sort
silent! let lines = getline(1,"$")
silent! normal <esc>
endfunction
command! ListLeaders :call ListLeaders()
" ---------------
" Sort attributes inside <> in html.
" E.g.
" <div
" b="1"
" a="1"
" c="1"
" />
"
" becomes
"
" <div
" a="1"
" b="1"
" c="1"
" />
" ---------------
function! SortAttributes()
normal vi>kojV
:'<,'>sort
endfunction
command! SortAttributes call SortAttributes()
nnoremap <silent> <leader>sa :SortAttributes<CR>
" ---------------
" Sort values inside a curl block
" E.g.
" {
" c: 1,
" a: 1,
" b: 1,
" }
"
" becomes
"
" {
" a: 1,
" b: 1,
" c: 1,
" }
" ---------------
function! SortBlock()
normal vi}jV
:'<,'>sort
endfunction
command! SortBlock call SortBlock()
nnoremap <silent> <leader>sb :SortBlock<CR>
" Show syntax highlighting group for word under cursor
nnoremap <leader>st :call <SID>SynStack()<CR>
function! <SID>SynStack()
if !exists("*synstack")
return
endif
echo map(synstack(line('.'), col('.')), 'synIDattr(v:val, "name")')
endfunc