2 Comments

Great tip!

I have a function called "Preserve" wich I can use to perform some tasks and keeping the cursor position:

<pre>

" preserve function

if !exists('*Preserve')

function! Preserve(command)

try

let l:win_view = winsaveview()

"silent! keepjumps keeppatterns execute a:command

silent! execute 'keeppatterns keepjumps ' . a:command

finally

call winrestview(l:win_view)

endtry

endfunction

endif

</pre>

So to remove the DOS end of line i use another function called Dos2Unix

<pre>

" dos2unix ^M

if !exists('*Dos2unixFunction')

fun! Dos2unixFunction() abort

"call Preserve('%s/ $//ge')

call Preserve(":%s/\x0D$//e")

set ff=unix

set bomb

set encoding=utf-8

set fileencoding=utf-8

endfun

endif

com! Dos2Unix :call Dos2unixFunction()

</pre>

And the "Preserve function" could be used to other stuff like reindenting the whole file:

<pre>

command! -nargs=0 Reindent :call Preserve('exec "normal! gg=G"')

</pre>

Expand full comment

Great info! I've been looking for something like this for a while.

Expand full comment