Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting to skip over punctuation when moving forwards and backwards words

Tags:

vim

When I'm using vim I generally never want to move to a punctuation mark when I press w or b to go forwards or backwards. So I'm wondering if there's a setting or something to change this functionality?

e.g. If I've got some code like

object.method(args)

and my cursor is at the [o] in "object" then I want w to move to the [m] in "method", and another w to move to the [a] in "args". I don't want it to land on the [.] or the [(]. If I've ever wanted to move to a punctuation char I've always used f or F to jump straight to it. I've never personally wanted to move to a punctuation char when I move through words and I just realized this is really bugging me.

like image 288
Tub Avatar asked Jun 29 '11 21:06

Tub


People also ask

How to skip entire words on the command line?

Now you can skip entire words on the command line by holding down the left ⌥ key and hitting ← or → To make this work for the right option key you need to set the key modifier to act as an Escape Sequence, a la the first step. Have a fresh tip?

How to find correct punctuation correctly?

A good correct punctuation website will be able to do the job in seconds and without getting tired or distracted and missing things. The ideal option, of course, is to do both, use the writing analysis tool to do the initial corrections and then to review the results manually after to see if anything has been missed.

Why do people make mistakes with their punctuation?

Many will make mistakes within their writing either through not knowing the rules or simply because they are rushing to get their writing done. The following are just a few of the most commonly made mistakes with your punctuation that our free application will be able to highlight for you:

Should punctuation be inside or outside of your speech marks?

Placing punctuation outside of your speech marks: you should place your final punctuation inside of the speech marks; unless you are writing in British English in which case it can go outside. Who Should Correct Their Writing?


2 Answers

I too find that I would like a movement that is more inclusive that w, but not as inclusive as W. In particular, I would like a movement that only considers tokens beginning with alphanumeric characters as significant.

So I came up with the following:

" <SPACE>   : forward to next word beginning with alphanumeric char
" <S-SPACE> : backward to prev word beginning with alphanumeric char
" <C-SPACE> : same as above (as <S-SPACE> not available in console Vim
" <BS>      : back to prev word ending with alphanumeric char
function! <SID>GotoPattern(pattern, dir) range
    let g:_saved_search_reg = @/
    let l:flags = "We"
    if a:dir == "b"
        let l:flags .= "b"
    endif
    for i in range(v:count1)
        call search(a:pattern, l:flags)
    endfor
    let @/ = g:_saved_search_reg
endfunction
nnoremap <silent> <SPACE> :<C-U>call <SID>GotoPattern('\(^\\|\<\)[A-Za-z0-9_]', 'f')<CR>
vnoremap <silent> <SPACE> :<C-U>let g:_saved_search_reg=@/<CR>gv/\(^\\|\<\)[A-Za-z0-9_]<CR>:<C-U>let @/=g:_saved_search_reg<CR>gv
nnoremap <silent> <S-SPACE> :<C-U>call <SID>GotoPattern('\(^\\|\<\)[A-Za-z0-9_]', 'b')<CR>
vnoremap <silent> <S-SPACE> :<C-U>let g:_saved_search_reg=@/<CR>gv?\(^\\|\<\)[A-Za-z0-9_]<CR>:<C-U>let @/=g:_saved_search_reg<CR>gv
nnoremap <silent> <BS> :call <SID>GotoPattern('[A-Za-z0-9_]\(\>\\|$\)', 'b')<CR>
vnoremap <silent> <BS> :<C-U>let g:_saved_search_reg=@/<CR>gv?[A-Za-z0-9_]\(\>\\|$\)<CR>:<C-U>let @/=g:_saved_search_reg<CR>gv

" Redundant mapping of <C-SPACE> to <S-SPACE> so that
" above mappings are available in console Vim.
"noremap <C-@> <C-B>
if has("gui_running")
    map <silent> <C-Space> <S-SPACE>
else
    if has("unix")
        map <Nul> <S-SPACE>
    else
        map <C-@> <S-SPACE>
    endif
endif

I have had this for a long time now, and I find that I use <SPACE>/<C-SPACE> movements so much more than w and W; it just seems more useful when coding. You can, of course, map the commands to whatever keys you find useful or more appropriate.

like image 137
Jeet Avatar answered Oct 01 '22 02:10

Jeet


Even running the risk of creating a script for something that's built-in (like I did last time), here is a little function that may help accomplishing this.

function! JumpToNextWord()
    normal w
    while strpart(getline('.'), col('.')-1, 1) !~ '\w'
        normal w
    endwhile
endfunction

Basically, what it does is executing the standard w and repeating it if the character under the cursor is not in a word character (feel free to change that pattern.

If you add that and a little map in your .vimrc:

nnoremap <silent> ,w :call JumpToNextWord()<CR>

It should work.

like image 25
sidyll Avatar answered Oct 01 '22 03:10

sidyll