-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvimrc
718 lines (639 loc) · 25 KB
/
vimrc
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
"Learn scripting
" http://learnvimscriptthehardway.stevelosh.com/
" NOTE: The username is explicitly set in ycm_global_ycm_extra_conf, and may
" need to be changed
" Vundle (Manages the plugins [needs to be first])
" https://github.com/gmarik/Vundle.vim
" ==============================================================================
set nocompatible " be iMproved, required
filetype off " required
" set the runtime path to include Vundle and initialize
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()
" alternatively, pass a path where Vundle should install plugins
"call vundle#begin('~/some/path/here')
Plugin 'gmarik/Vundle.vim'
" NERD-tree plugin
Plugin 'The-NERD-tree'
" YouCompleteMe plugin
" Plugin 'valloric/YouCompleteMe'
" Plugin which generates the .ycm_extra_conf.py file
" Plugin 'rdnetto/YCM-Generator', { 'branch': 'stable'}
" linediff plugin
" https://github.com/AndrewRadev/linediff.vim
" Plugin 'AndrewRadev/linediff.vim'
" Rainbow paranthesis
" Plugin 'kien/rainbow_parentheses.vim'
" LaTeX-Box
" NOTE: Comment this when using the LaTeX clean-up procedure
" Plugin 'LaTeX-Box-Team/LaTeX-Box'
" Fugitive (for git)
Plugin 'tpope/vim-fugitive'
" Dracula theme
Plugin 'dracula/vim'
" LanguageTool
" Requires java8
" sudo add-apt-repository ppa:webupd8team/java
" sudo apt update; sudo apt install oracle-java8-installer
" sudo apt install oracle-java8-set-default
" Plugin 'dpelle/vim-LanguageTool'
" Plugin 'python-mode/python-mode'
" All of your Plugins must be added before the following line
call vundle#end() " required
filetype plugin indent on " required
" To ignore plugin indent changes, instead use:
"filetype plugin on
"
" Brief help
" :PluginList - lists configured plugins
" :PluginInstall - installs plugins; append `!` to update or just :PluginUpdate
" :PluginSearch foo - searches for foo; append `!` to refresh local cache
" :PluginClean - confirms removal of unused plugins; append `!` to auto-approve removal
"
" see :h vundle for more details or wiki for FAQ
" Put your non-Plugin stuff after this line
" ==============================================================================
" Use the Dracula theme
set termguicolors
colorscheme dracula
" Rainbow options
" ==============================================================================
let g:rbpt_colorpairs = [
\ ['brown', 'RoyalBlue3'],
\ ['Darkblue', 'SeaGreen3'],
\ ['darkgray', 'DarkOrchid3'],
\ ['darkgreen', 'firebrick3'],
\ ['darkcyan', 'RoyalBlue3'],
\ ['darkred', 'SeaGreen3'],
\ ['darkmagenta', 'DarkOrchid3'],
\ ['brown', 'firebrick3'],
\ ['gray', 'RoyalBlue3'],
\ ['black', 'SeaGreen3'],
\ ['darkmagenta', 'DarkOrchid3'],
\ ['Darkblue', 'firebrick3'],
\ ['darkgreen', 'RoyalBlue3'],
\ ['darkcyan', 'SeaGreen3'],
\ ['darkred', 'DarkOrchid3'],
\ ['red', 'firebrick3'],
\ ]
let g:rbpt_max = 16
" ==============================================================================
" LaTeXBoX option
" ==============================================================================
let g:LatexBox_Folding=1 " Enable folding
" ==============================================================================
" LanguageTool
" ==============================================================================
let g:languagetool_jar='$HOME/LanguageTool-3.8/languagetool-commandline.jar'
" let g:languagetool_lang='en-US'
let g:languagetool_win_height='25'
" Set the languagetool excetions
let g:languagetool_disable_rules='COMMA_PARENTHESIS_WHITESPACE,CURRENCY,WHITESPACE_RULE,EN_UNPAIRED_BRACKETS,EN_QUOTES,MORFOLOGIK_RULE_EN_US'
" ==============================================================================
" Set the global leader
let mapleader = "-"
" Automatically cd into the directory that the file is in
autocmd BufEnter * :call TryChdir()
function! TryChdir()
try
execute "chdir ".escape(expand("%:p:h"), ' ')
catch \E472\
echom "Could not chdir"
endtry
endfunction
" Own mappings
" ==============================================================================
" Makes it possible to open vimrc and execute it as a script
" Changes will be effective immediately
" Open the ~/.vimrc with ev, add mapping, then use sv
nnoremap <leader>ev :tabe $MYVIMRC<CR>
nnoremap <leader>sv :w<CR> :source $MYVIMRC<CR>
nnoremap <leader>eb :tabe $HOME/.bashrc<CR>
nnoremap <leader>sb :w<CR> :!source $HOME/.bashrc<CR>
" Mark a word and put quotation marks around it
nnoremap <leader>" viw<esc>a"<esc>hbi"<esc>lel
" Open a new line without entering insert mode
nnoremap <leader>o o<ESC>
nnoremap <leader>O O<ESC>
" Delete without copying to the register
vnoremap <leader>d "_d
" Close current buffer
nnoremap <leader>c :q<CR>
" Open current tab
nnoremap <leader>tt :tabe<CR>
" Close current tab
nnoremap <leader>tc :tabclose<CR>
" Diff this and diff off
nnoremap <leader>dt :difft<CR>
nnoremap <leader>do :diffoff<CR>
vnoremap <leader>ld :Linediff<CR>
" Spell repeat
nnoremap <leader>sr :spellr<CR>
" LanguageTool
nnoremap <leader>lt :LanguageToolCheck<CR>
" ==============================================================================
" Learn vim mappings
" Touch typing tutorial
" http://www.typingclub.com/typing-qwerty-en.html
" ==============================================================================
" NOP means no operation. Nothing will happen if you try to use these
" http://vimcasts.org/blog/2013/02/habit-breaking-habit-making/
" Use hjkl
nnoremap <Up> <NOP>
inoremap <Up> <NOP>
nnoremap <Down> <NOP>
inoremap <Down> <NOP>
nnoremap <Left> <NOP>
inoremap <Left> <NOP>
nnoremap <Right> <NOP>
inoremap <Right> <NOP>
" Use Ctrl-h to delete one character, Ctrl-w one word, Ctrl-u the whole line
inoremap <Backspace> <NOP>
" Use your mappings
inoremap <ESC> <NOP>
" ==============================================================================
" These are the set commands used
" ==============================================================================
set nocompatible " Necessary for lots of cool vim things
set nowrap " Vim does not wrap the text
set autoindent " Vim indents
set expandtab " Spaces instead of tab character
set smarttab " Vim understands where the next tabs begins
set number " Line Numbers
set ignorecase " Ignoring case
set smartcase " Ignore case only for first letter
set hlsearch " Highlight things that we find with the search
set is " Highlights all
set splitbelow " Sets the default split
set cmdheight=1 " Sets the height of the command line to one
set shiftwidth=4 " 4 character tab
set softtabstop=4 " Display of real tab
set backspace=2 " Backspace works as expected
"set whichwrap+=<,>,h,l " Let the arrow keys (and h and l) change line
" " < and > are the arrow keys in n and v
" " [ and ] are the arrow keys in i
set textwidth=79 " Set to wrap at 80 characthers
set laststatus=2 " Always show the status bar
set statusline=%f%m%r%h\ [%L]\ [%{&ff}]\ %y%=[%p%%]\ [%l,%v]
"Set the status line
"http://stackoverflow.com/questions/4390011/how-do-you-write-text-on-the-status-line-with-the-filename-row-and-col-number-w
" ==============================================================================
" Color the syntax
" ==============================================================================
syntax on
" ==============================================================================
" Inline mode abbrivations
" ==============================================================================
iabbrev adn and
iabbrev ahve have
iabbrev implimentation implementation
iabbrev lenght length
iabbrev liek like
iabbrev rigth right
iabbrev taht that
iabbrev tehn then
iabbrev teh the
iabbrev ot to
iabbrev pbd pdb
iabbrev woudl would
iabbrev waht what
iabbrev wtha what
iabbrev wiht with
iabbrev cartesian Cartesian
iabbrev fourier Fourier
" ==============================================================================
" General mappings
" ==============================================================================
" Typing nt toggles NERDTree
nnoremap nt :NERDTreeToggle <CR>
" Map ctrl-movement keys to window switching
nnoremap <C-k> <C-w><Up>
nnoremap <C-j> <C-w><Down>
nnoremap <C-l> <C-w><Right>
nnoremap <C-h> <C-w><Left>
" remap jk to escape in insert mode
inoremap jk <Esc>
vnoremap <leader>jk <Esc>
"Makes F3 a toggle of numbering
nnoremap <F3> :set nonumber!<CR>
"Makes F8 a toggle of numbering
nnoremap <F8> :RainbowParenthesesToggle<CR>
" Toggles the paste
set pastetoggle=<F2>
" Select visual word
nnoremap <SPACE> viw
vnoremap <SPACE> iw
" Go to start of line and end of line
nnoremap <leader>H ^
nnoremap H 0
nnoremap L $
vnoremap <leader>H ^
vnoremap H 0
vnoremap L $h
" Mark search and replace visually selected
vnoremap <C-r> "hy:%s/\<<C-r>h\>//gc<left><left><left>
" Latex remap
nnoremap <leader>lr9 a\L(\R)<esc>hhi
vnoremap <leader>lr9 xa\L(\R)<esc>hhhp
" ==============================================================================
" YouCompleteMe
" ==============================================================================
" Path (for global ycm_extra_conf)
let g:ycm_global_ycm_extra_conf = '$HOME/.vim/bundle/YouCompleteMe/third_party/ycmd/cpp/ycm/.ycm_extra_conf.py'
" Do not ask to confirm if ycm find a .ycm_extra_conf.py
let g:ycm_confirm_extra_conf = 0
" Let the location list be populated (:lopen opens the location list)
let g:ycm_always_populate_location_list = 1
" YCM Diagnose
nnoremap <leader>di :YcmDiags<CR>
" Apply YCM FixIt
nnoremap <leader>fi :YcmCompleter FixIt<CR>
" Apply YCM ForceCompile
nnoremap <leader>fc :YcmForceCompileAndDiagnostics <CR>
" Go to definition (open in a new tab)
" <C-w>s splits the tab horizontally
" <C-k><C-o> goes to the previous buffer (above)
" <C-j><C-w>T opens the buffer below in a new tab
nnoremap <leader>jd :YcmCompleter GoTo<CR> <C-w>s <C-w><Up> <C-o> <C-w><Down> <C-w>T
" ==============================================================================
" File specific settings
" For all files: Remove trailing whitespaces on save
autocmd BufWritePre * :call <SID>StripTrailingWhitespaces()
" For all files except makefile: Replace tabs with spaces
autocmd BufWritePre *.py,*.cxx,*.cpp,*.c,*.h,*.hxx,*.tex retab! 4
" If the filetype is found, a function specifying the settings is called
autocmd BufNewFile,BufRead,BufEnter *.py call Set_Python_Options()
autocmd BufNewFile,BufRead,BufEnter *.cxx,*.cpp,*.c,*.h,*.hxx call Set_C_Cpp_Options()
autocmd BufNewFile,BufRead,BufEnter Make,Makefile,make,make call Set_Makefile_Options()
autocmd BufNewFile,BufRead,BufEnter *.tex call Set_tex_Options()
" Toggle colorcolumn
" ==============================================================================
function! g:ToggleColorColumn()
if &colorcolumn != ''
setlocal colorcolumn&
else
setlocal colorcolumn=81
endif
endfunction
nnoremap <silent> <leader>cc :call g:ToggleColorColumn()<CR>
" ==============================================================================
" Remove trailing whitespaces on save
" ==============================================================================
function! <SID>StripTrailingWhitespaces()
let l = line(".")
let c = col(".")
let w = winsaveview()
" Removes whitespace
%s/\s\+$//e
" Removes the end-lines
%s/\($\n\s*\)\+\%$//e
execute "normal! Go"
execute "normal! D"
call cursor(l, c)
call winrestview(w)
endfunction
" ==============================================================================
" Generic comment function
" ==============================================================================
function! ToggleComment(symbol)
" Get the character length of the input
let str_len = strlen(a:symbol)
" Get current cursor
let l = line(".")
let c = col(".")
" Go to first non-white character
execute "normal! ^"
" Get character under cursor
" Initialize first_chars with the first character
let first_chars = matchstr(getline('.'), '\%' . col('.') . 'c.')
" We start at 2: If str_len is one and we start at one => we enter the for
" loop
for cur_char in range(2,str_len)
" Go one character forward
execute "normal! l"
" Append the character
let first_chars = first_chars.matchstr(getline('.'), '\%' . col('.') . 'c.')
endfor
" Question mark case-insensitive no matter what the user has set
if first_chars !=? a:symbol
" Do the commenting (. is the concatination operation)
execute "normal! I".a:symbol." "
" Call back the cursor
call cursor(l, c+str_len+1)
else
" Go to first non-white character
execute "normal! ^"
" Remove the comment
execute "normal! ".string(str_len+1)."x"
" Call back the cursor
call cursor(l, c-str_len-1)
endif
endfunction
" ==============================================================================
" Python
" ==============================================================================
function! Set_Python_Options()
nnoremap <F5> :w<CR> :PymodeRun<CR>
inoremap <F5> <ESC> :PymodeRun<CR>
" Add comment
nnoremap <leader>a :call ToggleComment("#")<CR>
" autocmd BufWritePre * :PymodeLintAuto
endfunction
" ==============================================================================
" C and C++
" ==============================================================================
" Let global variable...
" Run pyclewn in async, set windows to bottom and spawn terminal at /dev/pts/4
let g:pyclewn_args = "--gdb=async --window=bottom --tty=/dev/pts/4"
" Allow pyclewn run in async mode and spawn a terminal at /dev/pts/4
function! Set_C_Cpp_Options()
" Sets the folding to syntax in c++
set foldmethod=syntax
" Make mapping
nnoremap <F4> :w<CR> :make<CR>
inoremap <F4> <ESC> :w<CR> :make<CR>
" %< is the filename without extention
nnoremap <F5> :w<CR> :!./%< <CR>
inoremap <F5> <ESC> :w<CR> :!./%< <CR>
" Pyclewn mappings
nnoremap <F6> :w <CR> :make clean <CR> <CR> :make <CR> <CR>
\:tabe % <CR> :Pyclewn gdb --args %<<CR>
\:Cbreak main <CR> :Cmapkeys <CR> :Cinferiortty <CR> :Crun <CR>
nnoremap <F7> :Cunmapkeys<CR> :Cexitclewn<CR>
nnoremap <leader>r :Crun<CR>
nnoremap <leader>p :Cprint<SPACE>
nnoremap <leader>w :Cwatch<SPACE>
" Add comment
nnoremap <leader>a :call ToggleComment("//")<CR>
endfunction
" ==============================================================================
" Makefiles
" ==============================================================================
function! Set_Makefile_Options()
" Turn of space for tab
set noexpandtab
set nosmarttab
set nosmartindent
" Map keys
nnoremap <F5> :w<CR> :make<CR>
inoremap <F5> <ESC> :w<CR> :make<CR>
nnoremap <F6> :w<CR> :make clean<CR>
inoremap <F6> <ESC> :w<CR> :make clean<CR>
" Add comment
nnoremap <leader>a :call ToggleComment("#")<CR>
endfunction
" ==============================================================================
" tex-files
" ==============================================================================
function! Set_tex_Options()
" Let vim wrap the text softly, and will not force linebreaks
setlocal wrap " Softwrap
setlocal textwidth=0 wrapmargin=0 " Vim will not make newlines
setlocal linebreak " --"--
setlocal nolist " list disables linebreak
" Formatting when writing gq and writing text
set formatexpr=MyFormatExpr(v:lnum,v:lnum+v:count-1)
" Map keys
nnoremap <leader>to :LatexTOC<CR>
nnoremap <F5> :w<CR> :!pdflatex -interaction=nonstopmode %<CR>
\:!bibtex %:r<CR>
\:!pdflatex -interaction=nonstopmode %<CR>
inoremap <F5> <ESC> :w<CR> :!pdflatex -interaction=nonstopmode %<CR>
\:!bibtex %<CR>
\:!pdflatex -interaction=nonstopmode %<CR>
nnoremap <F6> :w<CR> :!rm -f *.log *.aux *.out<CR>
inoremap <F6> <ESC> :w<CR> :!rm -f *.log *.aux *.out<CR>
nnoremap <F7> :w<CR> :!okular %:r.pdf &<CR>
inoremap .<SPACE> .<CR>
" Add comment
nnoremap <leader>a :call ToggleComment("%")<CR>
nnoremap <leader>cl :call Clean_TeX()<CR>
\<CR>:call Clean_TeX()<CR>
\<CR>:call Clean_TeX()<CR>
\<CR>:call Format_TeX()<CR>
endfunction
" Formatting text
"----------------
function! MyFormatExpr(start, end)
silent execute a:start.','.a:end.'s/[.!?]\zs /\r/g'
endfunction
"----------------
" Function for cleaning up equations
"-----------------------------------
function! Clean_TeX()
echom "Cleaning up equations"
try
" \r denotes a newline
%s/\\\[/\\begin{align}/g
%s/\\\]/\\end{align}/g
%s/\\begin{equation}/\\begin{align}/g
%s/\\end{equation}/\\end{align}/g
%s/\\begin{equation\*}/\\begin{align\*}/g
%s/\\end{equation\*}/\\end{align\*}/g
%s/\\begin{eqnarray}/\\begin{align}/g
%s/\\end{eqnarray}/\\end{align}/g
%s/\\begin{eqnarray\*}/\\begin{align\*}/g
%s/\\end{eqnarray\*}/\\end{align\*}/g
" If the strings were not found
catch /E486/
endtry
echom "Cleaning up aligns"
try
%s/&=&/=\&/g
%s/&=/=\&/g
%s/&+&/\&+/g
%s/+&/\&+/g
%s/&-&/\&-/g
%s/-&/\&-/g
%s/&\*&/\&\*/g
%s/\*&/\&\*/g
%s/&\/&/\&\//g
%s/\/&/\&\//g
" If the strings were not found
catch /E486/
endtry
echom "Putting important things on own line"
try
%s/\\index{\(.*\)}/\r\%\r\\index{\1}\r\%\r/g
%s/\\label{\(.*\)}/\r\%\r\\label{\1}\r\%\r/g
%s/\\lstinline!\(.*\)!/\r\%\r\\lstinline!\1!\r\%\r/g
" If they now have newlines before: Remove them
%s/\(\s*\n*\)*\n%\n\\index{/\r\%\r\\index{/g
%s/\(\s*\n*\)*\n%\n\\label{/\r\%\r\\label{/g
%s/\(\s*\n*\)*\n%\n\\lstinline!/\r\%\r\\lstinline!/g
" If the strings were not found
catch /E486/
endtry
echom "Adding percents"
try
" Let begins start with a precent mark
%s/\\begin{/\%\r\\begin{/g
%s/\\footnote{/\%\r\\footnote{/g
" The parantheses makes a group \1 is a backreference to that group
". matches character and * is any number of them
%s/\\end{\(.*\)}/\\end{\1}\r\%\r/g
" If the strings were not found
catch /E486/
endtry
echom "Cleaning up percents (comments)"
try
" The parantheses makes a group, * repeats that group
" Make repeated newlines with precents to only one new line of
" precent
%s/%\(\n%\)*/\%/g
" Make comment + a lot of new lines to just a comment and newline
%s/%\n\n\(\n\)*/\%\r/g
" Repeat the first in case the previous brougth percentages together,
" and allow whitespaces
%s/%\n\s*%\(\n\(\s*%\)*\)*/%\r/g
" If the strings were not found
catch /E486/
endtry
echom "Making space for sections"
try
" 2 spaces for new sub sub section
%s/\n\(\n\)*\\subsubsection/\r\r\r\\subsubsection/g
" 3 spaces for new sub section
%s/\n\(\n\)*\\subsection/\r\r\r\r\\subsection/g
" 5 spaces for new section
%s/\n\(\n\)*\\section/\r\r\r\r\r\r\\section/g
" Puts a comment between the section{} and the paragraph below
" /1 is the backreference
%s/section{\(.*\)}\(\n\)*/\section{\1}\r\%\r/g
" Make repeated newlines with precents to only one new line of
" precent
%s/%\(\n%\)*/\%/g
" If the strings were not found
catch /E486/
endtry
echom "Last touch"
try
" Deal with nested begins (they will have some whitespace before the
" percentage
%s/\(\s*\)%\n\\begin/\1%\r\1\\begin/g
" Do the same for the end percentage sign
" \1 refers to the argument and \2 refers to the spaces
%s/\\end{\(.*\)}\n\(\s*\)%/\2\\end{\1}\r\2%/g
catch /E486/
endtry
echom "Clean-up complete"
endfunction
"-----------------------------------
" Function to format the Tex
"---------------------------
function! Format_TeX()
" TODO:
" Make it possible to endline after a sentence which ends with a
" punctuation. For now, this is appearing as a character, followed by a
" punctuation and at least two spaces
" %s/\(.\)\.\s\s\(\s*\)/\1\.\rMYSUPERBANANAMARKER\r/gc
" Problems with footnotes
" Save the current position
let cursor_start = getpos(".")
" Go to the beginning of the document
normal gg
try
" Search for the beginnig of the document, and go to the first line
" under begin{document}
execute "/begin{document}/+"
try
execute "/section{/+"
catch /E486/
echom "No section found. Will start from line 1"
endtry
catch /E486/
echom "This document has no /begin{document}. Will start from line 1"
endtry
" Store the current position
let cur_pos = getpos(".")
" Stores the line number of the end of document
let end_file = line("$")
" Store the regex for the start and end of the paragraph
let common = '^\(%D\)\=\s*'.
\'\('.
\'$\|'.
\'\\centering\|'.
\'\\caption\|'.
\'\\label\|'.
\'\\begin\|'.
\'\\end\|'.
\'\\\[\|'.
\'\\\]\|'.
\'\\\(sub\)*section\>'.
\'\|\\item\>\|'.
\'\\NC\>\|'.
\'\\blank\>\|'
let par_begin = common.
\'\\noindent\>\)'
let par_end = common.
\'\\place\)'
" cur_pos is the line number of the current position
while cur_pos[1] < end_file
" Set the cursor to the current position
call setpos(".", cur_pos)
" Checks that the current line is not empty
if (getline(".") != "")
" ? searches upwards in the document for par_begin
" The second ? stops the search command, so that we are allowed to
" the next line with + (the . is the string concatenation operator)
execute '?'.par_begin.'?'
"Check that verbatim etc. are not in the string
let temp_string = getline(".")
" Check if we are working with an empty string again (in that case
" we won't have to check the string)
if temp_string != ""
" As we ran the Clean_TeX() function above, all \begin{}
" environments should be the first thing appearing on a line,
" possibly preceeded by whitespaces. Therefore, we will split
" the temp_string (the whitespaces will not be a part of the
" split, and thence the first element shold be /begin
let temp_string = split(temp_string)[0]
" We will now check if temp_string is one of the following
" (possibly followed by environmental specifiers)
" Note that the slice includes the last index
if (temp_string[0:15] == "\\begin{verbatim}") ||
\(temp_string[0:14] == "\\begin{tabular}") ||
\(temp_string[0:12] == "\\begin{array}") ||
\(temp_string[0:17] == "\\begin{lstlisting}")
" Go to the next line after \end{
execute '/\\end{/+'
" update the position
let cur_pos = getpos(".")
else
" Go to the next line
normal j
endif
else
" Go to the next line
normal j
endif
" Starts visual mode
normal v
" Finds the end of the paragraph and goes one line back
execute '/'.par_end.'/-'
" Set the previous position in case we are unable to go forward
let prev_pos = cur_pos
" Get the new position
let cur_pos = getpos(".")
" Increment the line by one
let cur_pos[1] += 1
" Do the formatting
normal gq
" If no prgress is made: Move forward
if (prev_pos[1] == cur_pos[1])
let cur_pos[1] += 1
endif
else
" If the line is empty, just increase the line number
let cur_pos[1] += 1
endif
endwhile
" Reset the position to the original position (will probably be different
" than what it was before the start)
call setpos('.', cursor_start)
endfunction
"---------------------------
" ==============================================================================
" Fix paste bug triggered by the above inoremaps
" https://github.com/johndgiese/dotvim/issues/4#issuecomment-442151962
set t_BE=