The Computer Oracle

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

Become or hire the top 3% of the developers on Toptal https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: Puzzle Game 2

--

Chapters
00:00 Question
00:37 Accepted answer (Score 75)
00:58 Answer 2 (Score 79)
01:25 Answer 3 (Score 13)
01:41 Answer 4 (Score 7)
02:06 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