5

In vim, how can I move the cursor down (or up) to the first line containing a non-blank character in the same column ?

For example, in the following text:

item1
    item2
    item3
item4
    item5
    item6

If the cursor is on the e in item1, move the cursor to the e in item4.

If on the m in item3, move to the m in item5.

FYI, I'm looking for a quick and efficient way to navigate formatted text.

(P.S. Those are spaces not tabs.)

3 Answers 3

3

You could use the following mappings to do this. (There might be some edge cases I didn't think of)

nnoremap <leader>d m':exec '/\%' . col(".") . 'c\S'<CR>``n
nnoremap <leader>u m':exec '?\%' . col(".") . 'c\S'<CR>``n

The important part is :exec '/\%' . col(".") . 'c' This matches the current column. This is taken directly from :h %c. Then I added a \S to match non whitespace. m' and `` is used to store the current position and restore it around the execute statement. This is necessary since using the execute places us at the start of the line which could lead to erratic behavior (skipping too many lines in some cases). After executing this I go to the first match with n. The only difference between the up and down version is which direction we search / for down and ? for up.

If you instead wanted to do this with virtual columns (i.e. tabs) replace \%c with \%v.

2
  • 1
    That's superb; it's exactly what I was looking for! I think I will map these to <leader>j and <leader>k respectively. The only thing I will add is :noh to the end of each to clear highlights. I also note this would clobber any previous search or highlights that may have been in place, but this essentially does what I wanted. Thanks !
    – Lqueryvg
    Commented May 16, 2014 at 17:59
  • 1
    @Lqueryvg while it does clobber the search register you can still use n and N to move up and down the column.
    – FDinoff
    Commented May 16, 2014 at 18:00
2

My JumpToVerticalOccurrence plugin provides ]| and [| mappings that provide just that, supporting [count] and without clobbering the current search pattern.

1
  • Thanks for this. I will explore the plugin as well as the "one-liner" solution.
    – Lqueryvg
    Commented May 20, 2014 at 8:43
0

If you want this for code or configuration files with a defined indentation system (assuming that falls under "formatted text"), jeetsukumaran/vim-indentwise works well for relative, absolute, or block-scope movements across indented blocks.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .