Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

1) With the { and } commands in vim I can jump to the previous or next completely blank line (or beginning or end of file). How do I modify these commands to jump to the previous or next line being completely blank or containing only whitespace (or beginning or end of file) ?

2) How do I create commands to jump to the beginning or end of the current code block, defined by the current indent? For example, I want to easily move from line F to line D or G (or to line C or H):

share|improve this question
I'm not posting this as an answer because it's not complete enough, but for #2 you can use the indent() function to get the indent of the current line, then use that indent in a search expression to find the next line with the same indent. I did something similar in a plugin to jump between labels in a C switch statement which you can find at if some example code might help. – garyjohn May 2 '12 at 15:37

Question 1 seems relatively easy (and my answer probably a bit naive):

nnoremap } /^\s*$<CR>
nnoremap { ?^\s*$<CR>

But you should find another shortcut, I think.

Also you already have gg to jump to the first line of the buffer and G to jump to its last line. Isn't that enough?

I don't know how to answer question 2 in a simple manner. Did you try vim-indent-object?

share|improve this answer
Thanks for your reply. vim-indent-object was more or less exactly what I was looking for the second question. For the first question, I came up with a similar solution myself but then the search highlighting gets triggered and it doesn't look too nice (although I guess I could turn it off). Also, the fact that the { and } commands can jump to the beginning/end of the file is quite handy... It would be great if there is a way to get that functionality. – Matt May 2 '12 at 12:51
I don't like it so I don't activate highlighting (it's off by default). – romainl May 2 '12 at 13:39
@Matt: The contents of the search register are restored after a function call. Therefore you can put your search commands within a function, set your mapping to call that function and when the function returns, your cursor will have moved but your search highlighting will be unaffected. – garyjohn May 2 '12 at 15:26

Combining the great tips of romainl and garyjohn I got the exact functionality I was looking for for part #1:

nnoremap <silent> { :call MyPrevParagraph()<CR>
nnoremap <silent> } :call MyNextParagraph()<CR>

function MyNextParagraph()
  let myline = search('^\s*$', 'W')
  if myline <= 0
    execute 'normal! G$'
    execute 'normal! '.myline.'G0'

function MyPrevParagraph()
  let myline = search('^\s*$', 'bW')
  if myline <= 0
    execute 'normal! gg0'
    execute 'normal! '.myline.'G0'
share|improve this answer

You must log in to answer this question.

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