python - 相当于 Python 文件的移动百分比

标签 python vim

对于使用 {} 表示 block 的语言,vim 具有全能的 % 键。 python代码的等效移动是什么?或者至少移动到具有相同缩进的下一行/上一行。

最佳答案

vim wiki建议绑定(bind),例如:

nnoremap <M-,> k:call search('^'. matchstr(getline(line('.')+1), '\(\s*\)') .'\S', 'b')<CR>^
nnoremap <M-.> :call search('^'. matchstr(getline(line('.')), '\(\s*\)') .'\S')<CR>^

以及提供更全面的解决方案:

" Jump to the next or previous line that has the same level or a lower
" level of indentation than the current line.
"
" exclusive (bool): true: Motion is exclusive
" false: Motion is inclusive
" fwd (bool): true: Go to next line
" false: Go to previous line
" lowerlevel (bool): true: Go to line with lower indentation level
" false: Go to line with the same indentation level
" skipblanks (bool): true: Skip blank lines
" false: Don't skip blank lines
function! NextIndent(exclusive, fwd, lowerlevel, skipblanks)
  let line = line('.')
  let column = col('.')
  let lastline = line('$')
  let indent = indent(line)
  let stepvalue = a:fwd ? 1 : -1
  while (line > 0 && line <= lastline)
    let line = line + stepvalue
    if ( ! a:lowerlevel && indent(line) == indent ||
          \ a:lowerlevel && indent(line) < indent)
      if (! a:skipblanks || strlen(getline(line)) > 0)
        if (a:exclusive)
          let line = line - stepvalue
        endif
        exe line
        exe "normal " column . "|"
        return
      endif
    endif
  endwhile
endfunction

" Moving back and forth between lines of same or lower indentation.
nnoremap <silent> [l :call NextIndent(0, 0, 0, 1)<CR>
nnoremap <silent> ]l :call NextIndent(0, 1, 0, 1)<CR>
nnoremap <silent> [L :call NextIndent(0, 0, 1, 1)<CR>
nnoremap <silent> ]L :call NextIndent(0, 1, 1, 1)<CR>
vnoremap <silent> [l <Esc>:call NextIndent(0, 0, 0, 1)<CR>m'gv''
vnoremap <silent> ]l <Esc>:call NextIndent(0, 1, 0, 1)<CR>m'gv''
vnoremap <silent> [L <Esc>:call NextIndent(0, 0, 1, 1)<CR>m'gv''
vnoremap <silent> ]L <Esc>:call NextIndent(0, 1, 1, 1)<CR>m'gv''
onoremap <silent> [l :call NextIndent(0, 0, 0, 1)<CR>
onoremap <silent> ]l :call NextIndent(0, 1, 0, 1)<CR>
onoremap <silent> [L :call NextIndent(1, 0, 1, 1)<CR>
onoremap <silent> ]L :call NextIndent(1, 1, 1, 1)<CR>

使用:

  • [l and ]l 跳转到与当前行相同缩进级别的上一行或下一行。
  • [L and ]L 跳转到上一行或下一行,缩进级别低于当前行。

关于python - 相当于 Python 文件的移动百分比,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16883360/

相关文章:

vim - 如何清除 VIM 中函数调用的输出?

linux - 如何将多个帐户(计算机、登录名、密码)添加到我的 VIM .netrc 文件中?

python - PANDAS 中类似 SQL 的窗口函数 : Row Numbering in Python Pandas Dataframe

python - 使用 patches.Rectangle 绘制不规则光栅图时避免缓慢循环

python - 为什么在这种情况下插入父类(super class)的 __init__() ?

vim - 在 vi/vim 编辑器中从文件中删除几个选定的字符

vim - 我怎么做;像 : in vimpulse? 一样工作

c - 如何在vim中仅用一个制表符自动缩进分割线?

python - httplib.BadStatusLine : '' on Linux but not Mac

python - nbconvert jupyter笔记本到pdf(带颜色)