The Computer Oracle

Move cursor to beginning of non-whitespace characters in a line in Vim

--------------------------------------------------
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
and get $2,000 discount on your first invoice
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Darkness Approaches Looping

--

Chapters
00:00 Move Cursor To Beginning Of Non-Whitespace Characters In A Line In Vim
00:28 Accepted Answer Score 78
00:44 Answer 2 Score 7
01:04 Answer 3 Score 14
01:16 Answer 4 Score 83
01:35 Thank you

--

Full question
https://superuser.com/questions/301109/m...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#vim #gvim #macvim

#avk47



ANSWER 1

Score 83


Instead of pressing ^ you can press _(underscore) to jump to the first non-whitespace character on the same line the cursor is on.

+ and - jump to the first non-whitespace character on the next / previous line.

(These commands only work in normal mode, not in insert mode.)




ACCEPTED ANSWER

Score 78


If I understand correctly - from :h ^:

^ To the first non-blank character of the line.
  |exclusive| motion.

(in contrast to 0, which gets you to the beginning, regardless of whitespace or not)




ANSWER 3

Score 14


Also possibly useful: + and - will move the cursor up or down, respectively, to the first non-blank character.




ANSWER 4

Score 7


below is a snippet from by .vimrc
^[[1~ is created by pressing ctrl+v and Home

"jump to first non-whitespace on line, jump to begining of line if already at first non-whitespace
map <Home> :call LineHome()<CR>:echo<CR>
imap <Home> <C-R>=LineHome()<CR>
map ^[[1~ :call LineHome()<CR>:echo<CR>
imap ^[[1~ <C-R>=LineHome()<CR>
function! LineHome()
  let x = col('.')
  execute "normal ^"
  if x == col('.')
    execute "normal 0"
  endif
  return ""
endfunction