I always have problems selecting visual areas with the normal! command in vim. Against all I expected, the following doesn't work:

function! s:foo()
    normal! v/\S\<cr>

nnoremap <silent> a :<c-u>call <sid>foo()<cr>

It just selects nothing. normal! doesn't seem to work with /. Have I reached the limit of vim's capabilities or am I just doing something wrong?


Rather than using vim key-descriptions (such as <CR> or <C-v>) you'll have to enter the literal key you want to press. \<CR> looks like it means "Press the enter key" it really means "Search for \< start of word, followed by the text CR>

To do a literal newline character instead of <cr>, try pressing <C-v><cr>. This will appear as ^M. That works for me.

  • I don't search for a backslash character, the ` \ ` was supposed to escape the < of <esc>. All these different kinds of newlines still trip me up... – hgiesel Jun 18 '16 at 23:59
  • @hgiesel oh. So what exactly are you searching for? You should describe that. – James Jun 19 '16 at 0:01
  • Just what you wrote: Select from current position to first non-whitestace character (inclusive). But with ^M it still doesn't work for me, weird. – hgiesel Jun 19 '16 at 0:06
  • 1
    @hgiesel That's because you've got the extra backlash in there. Does it work with :normal! v/\S^M? (With ^M entered <C-v><cr>) – James Jun 19 '16 at 0:10
  • @hgiesel Glad I could help! let me update my answer real quick so it more accurately represents the right solution. – James Jun 19 '16 at 0:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.