如何在导航时将前导空格视为Vim中的选项卡?

时间:2015-01-20 20:17:11

标签: vim

基本上我对PSR-2代码合规性有以下设置:

set tabstop=4
set shiftwidth=4
set softtabstop=4
set expandtab
set smarttab

PSR-2要求缩进为4个空格。这很好,但我习惯使用真正的标签而不是空格,所以如果我在一行的开头,我向右移动,而不是移动到第一个缩进字符,它一次移动一个空间。

有没有办法让vim以同样的方式对待这些领先的空间,也就是说,跳过"跳跃"在正常模式和插入模式下导航到第一个非空格字符?

我知道我可以使用^将光标放在第一个非空白字符上,但我并不习惯这样,而且它不像简单导航那么方便。< / p>

2 个答案:

答案 0 :(得分:1)

将以下内容放入vimrc

function! s:super_left()
  return getline('.')[:col('.')] =~ '^\s\+$' ? 'w' : 'l'
endfunction
augroup SuperLeft
  au!
  autocmd FileType php nnoremap <expr> l <sid>super_left()
augroup END

答案 1 :(得分:1)

我认为你可以更好地习惯使用vims更强大的移动命令,例如^

话虽如此,这是你可以达到你想要的一种方式。

nnoremap <right> :silent call SkipSpace()<cr>
function! SkipSpace()
  let curcol = col('.')
  execute "normal ^"
  let hatcol = col('.')

  if curcol >= hatcol
    " move one space right
    let newcol = curcol + 1
  elseif curcol + 4 > hatcol
    " move to the start of the next word if it is less than 4 spaces away
    let newcol = hatcol
  else 
    " move 4 spaces right
    let newcol = curcol + 4
  endif

  execute "normal " . newcol . "|"

endfunction

P.S。如需一点乐趣,请查看:help |

相关问题