I wanted to use my H and L keys in vim to navigate vertically faster (than h/l). This is what I came up with: I consider four areas per line important:The start of the lineThe first non-whitespace characterThe last non-whitespace characterThe end of the line (after trailing whitespace)My key bindings should do the following:Skip leading whitespace (move cursor from line start to first non-whitespace character/vice versa)Skip trailing whitespace (move cursor from last non-whitespace character to line end/vice versa)If on start of line H should move the cursor to the end of the same line. If on end of line L should move the cursor to the start of the same lineIn the text between first/last non-whitespace character the cursor should move in steps of stepSize (in my Example = 5) characters at once, but should not exceed the first/last non-whitespace character (if stepSize = 5 and the cursor is two characters after the first non-whitespace character H should snap to the first non-whitespace character and should not exceed it)This code is currently in my .vimrc (created with the help of statox over at vi and vim):function! VerticalMotion(isLeft, stepSize)   Original cursor position  let originalPosition=getcurpos()  let originalColumn=originalPosition[2]   Leftmost non-whitespace column  normal! ^  let leftmostNWColumn=getcurpos()[2]   Rightmost non-whitespace column  normal! g_  let rightmostNWColumn=getcurpos()[2]   Rightmost column  normal! $  let rightmostColumn=getcurpos()[2]  if (a:isLeft)    if (originalColumn > rightmostNWColumn)      normal g_    elseif (originalColumn > leftmostNWColumn + a:stepSize)      call setpos('.', originalPosition)      execute normal! a:stepSize . h    elseif (originalColumn > leftmostNWColumn)      normal! ^    elseif (originalColumn > 1)      normal! 0    else      normal! $    endif  else     if (originalColumn < leftmostNWColumn)      normal! ^    elseif (originalColumn < rightmostNWColumn - a:stepSize)      call setpos('.', originalPosition)      execute normal! a:stepSize . l    elseif (originalColumn < rightmostNWColumn)      normal! g_    elseif (originalColumn < rightmostColumn)      normal! $    else      normal! 0    endif  endifendfunctionThen I assign the function to H/L:noremap H :call VerticalMotion(1, 5)<CR>noremap L :call VerticalMotion(0, 5)<CR>How can I improve my code? Is there some way to shorten it (seems kind of repetitive to me)? This is also my first vimscript-function so any tips of good practices are welcome as well :)
Vertical Movement for Vim
beginner;vimscript