Real Vim ninjas count every keystroke - do you?

Pick a challenge, fire up Vim, and show us what you got.

Changelog, Rules & FAQ, updates: @vimgolf, RSS.

Your VimGolf key: please sign in

$ gem install vimgolf
$ vimgolf setup
$ vimgolf put 5f1310bd3cc9f3000bef87ed

Levenshtein distance

Compute distance for each pair. Notice that this recursive implementation is very inefficient. Wagner-Fischer algorithm is iterative and much faster. #vimscript

Start file
" Levenshtein distance between two strings
function! LD(s1, s2)
  let l1 = strlen(a:s1)
  let l2 = strlen(a:s2)
  if l1 <= 0
    return l2
  elseif l2 <= 0
    return l1
  elseif a:s1[0] ==# a:s2[0]
    return LD(a:s1[1:], a:s2[1:])
  else
    return 1 + min([
    \ LD(a:s1, a:s2[1:]), LD(a:s1[1:], a:s2), LD(a:s1[1:], a:s2[1:])
    \ ])
  endif
endfunction
"
Humm... Is this useful?
"nmap M I<C-R>=LD("<C-R>a","<C-R>b")<CR><Esc>

"
VIM VimGolf
"keystroke matt
"
vimscript way
"reformat refactor
"
challenge chatter
"colons semicolons
"
golfer golfers
"order sorting
"
modules models
"shift+ret ctrl+ret
"
ninja manic
"destination distance
"
pattern lines
"linewrapping linewrapping
"
swap switch
"vice versa
"
split multiples
"block blackened
"
-a-b-c c-a-b
"hello_world hello world!
End file
6
8
9
5
5
4
1
5
2
5
4
7
6
0
4
4
7
5
3
2

View Diff

1,39c1,20
< " Levenshtein distance between two strings
< function! LD(s1, s2)
<   let l1 = strlen(a:s1)
<   let l2 = strlen(a:s2)
<   if l1 <= 0
<     return l2
<   elseif l2 <= 0
<     return l1
<   elseif a:s1[0] ==# a:s2[0]
<     return LD(a:s1[1:], a:s2[1:])
<   else
<     return 1 + min([
<     \ LD(a:s1, a:s2[1:]), LD(a:s1[1:], a:s2), LD(a:s1[1:], a:s2[1:])
<     \ ])
<   endif
< endfunction
< "
Humm... Is this useful?
< "nmap M I<C-R>=LD("<C-R>a","<C-R>b")<CR><Esc>
<
< "
VIM VimGolf
< "keystroke matt
< "
vimscript way
< "reformat refactor
< "
challenge chatter
< "colons semicolons
< "
golfer golfers
< "order sorting
< "
modules models
< "shift+ret ctrl+ret
< "
ninja manic
< "destination distance
< "
pattern lines
< "linewrapping linewrapping
< "
swap switch
< "vice versa
< "
split multiples
< "block blackened
< "
-a-b-c c-a-b
< "hello_world hello world!
---
> 6
> 8
> 9
> 5
> 5
> 4
> 1
> 5
> 2
> 5
> 4
> 7
> 6
> 0
> 4
> 4
> 7
> 5
> 3
> 2

Solutions

The best way to learn is to practice. Below, you will find some of the solutions other golfers have entered. To unlock higher ranked solutions, submit your own entry which does as well or better than the solutions you can currently see - climb the ladder!

Check out these helpful resources to improve your Vim skills... Game on.

Unlock 36 remaining solutions by signing in and submitting your own entry
#37 Rennnda / @rennnda2020 - Score: 48 - 04/27/21 @ 14:36
cG6<CR>8<CR>9<CR>5<CR>5<CR>4<CR>1<CR>5<CR>2<CR>5<CR>4<CR>7<CR>6<CR><CR><BS>0<CR>4<CR>4<CR>7<CR>5<CR>3<CR>2<CR><BS><Esc>ZZ

0 comments


Created by: @mcr05

37 active golfers, 105 entries

Leaderboard (lowest score wins):
44
#31 - Jason / @jason_eveleth

07/23/2020 at 06:58AM

44
#32 - Borna Sadeghi / @borna_sadeghi

03/19/2021 at 07:28PM

44
#33 - chriskuck / @chriskuck

03/25/2021 at 03:34PM

44
#34 - Marco Meinardi / @Kee__no

12/30/2022 at 08:53PM

44
#35 - Jakob Föger / @drvolcano86

02/17/2023 at 06:01PM

44
#36 - masatosis / @masatosis

12/20/2023 at 04:44AM

48
#37 - Rennnda / @rennnda2020

04/27/2021 at 02:36PM