11

I recently found out about Vim hard mode and I've decided to try and use it. However, I'm running into some problems with fine grain movements.

For example, let's say I really just want to move up or down one line. Since j and k are disabled, I'm not sure what better alternatives there are for this.

I've run into the same problem when I want to move one or two characters to right or left. Does anyone know how one would replace these types of movements?

This is the plugin I am using: https://github.com/wikitopian/hardmode.

  • What a stupid plugin! Use whatever works. – romainl Apr 16 '16 at 19:59
  • I really agree with romainl: it is good to learn how to navigate efficiently in a file but imposing to yourself this kind of plugin is a not necessary pain. Still you could be interested in my answer here (more precisely the part about / and ?. – statox Apr 16 '16 at 22:04
  • 1
    I would respectfully disagree with statox and romainl here. Navigating directly to the targets you want to change can greatly improve your overall efficiency. While yes this may slow you down at first, In the long run you will make up for the lost time using better navigations. – Archangel33 Apr 17 '16 at 6:39
  • Actually @Archangel33 I think we share the same opinion about the fact that navigating more precisely with other methods than hjkl is a good thing. What I was disapproving was the plugin itself: In my opinion it is better to learn new ways to navigate once at the time (you can't learn all of them at the same time) and during the learning using plugins which frustrates you is not necessary. That being said it is only my opinion: one has right to disagree :-) – statox Apr 17 '16 at 19:00
  • 1
    @Alex Indeed my comment was only my personal opinion, I would never consider that I have the only and universal solution that's why my the last sentence was That being said it is only my opinion: one has right to disagree. Now, the debate is potentially endless: of course some people will benefit of this kind of plugin whereas it will only frustrate some other and harm their learning. If you'd like to talk about it I'll gladly discuss with you in a chat but we shouldn't spam the comments of the question with this kind of debate :-) – statox Apr 29 '16 at 12:45
17

These are just a few of the movements that vim is capable of, these should get you going for now.

TL;DR

  1. Use Vim's built in help feature on navigation or better still usr_03.txt
  2. Search google for vim advanced movements
  3. Navigate with/to words
  4. go here All the right moves

Movements

There are many ways of getting around in vim I have listed some that I found by searching for vim advanced movements, and a few that I use all the time. Im sure you know of most of these but the ones I think you will want to pay particular attention too are the word navigation's.

Here are a few that allow for navigation inside a line.

  • 0 – go to the starting of the current line.
  • ^ – go to the first non blank character of the line.
  • $ – go to the end of the current line.
  • g_ – go to the last non blank character of the line.

Navigation with words can be very helpful. To be more granular, you should navigate in relation to words, using:

  • e – go to the end of the current word.
  • E – go to the end of the current WORD.
  • b – go to the previous (before) word.
  • B – go to the previous (before) WORD.
  • w – go to the next word.
  • W – go to the next WORD.

A word consists of a sequence of letters, digits and underscores. A WORD consists of a sequence of non-blank characters, separated with white space. words|WORDS may also change depending on your vim settings.

Example to show the difference between WORD and word

192.168.1.1 is single WORD.

192.168.1.1 is seven words.

The search movements can be extremely useful too.

  • /pattern – Search for a pattern which will you take you to the next occurrence of pattern
    • n will take you to the next match of pattern below the cursor.
  • ?pattern – Search for a pattern which will you take you to the previous occurrence of pattern.
    • N will take you to the next match of pattern above the the cursor.
  • * – Go to the next occurrence of the current word under the cursor.
  • # – Go to the previous occurrence of the current word under the cursor.
  • % – Go to the matching braces, or parenthesis inside code.

Here are a few you can do even before you open vim, from the command line Vim +N filename allows you to go to the Nth line of the file after opening it.

vim +10 /etc/passwd

Vim +/pattern filename opens the file to the first line with the occurrence of the pattern. In the following example, vim will open the README file and jump to the first occurrence of the word “install”.

vim +/install README

Vim +?patten filename opens the file to the last occurrence of pattern. In the following example, it will open the README file and jump to the last occurrence of the word “bug”.

vim +?bug README

8

As an alternative to hardmode, the following snippet will allow you to use h, j, k, and l as normal but produce an audible beep to warn you when they are used in rapid succession.

function! s:BeepOnRepeat(key)
    let count1 = v:count1

    if !exists('s:last_time')
        let s:last_time    = reltime()
        let s:repeat_count = 1
    else
        " increment the repeat count if `h`, `j`, `k`, or `l` was pressed
        " within the last second, otherwise reset it
        let now = reltime()
        if str2float(reltimestr(reltime(s:last_time, now))) < 1
            let s:repeat_count += 1
        else
            let s:repeat_count = 1
        endif
        let s:last_time = now

        " produce an audible beep if a sequence of `h`, `j`, `k`, and/or `l`,
        " greater than three, was pressed (within the repeat count time limit)
        if s:repeat_count > 3
            let savebelloff    = &belloff
            let savevisualbell = &visualbell
            let &belloff    = ''
            let &visualbell = 0
            execute "normal! \<Esc>"
            let &belloff    = savebelloff
            let &visualbell = savevisualbell
        endif
    endif

    execute 'normal! ' . count1 . a:key
endfunction

nnoremap <silent> h :<C-u>call <SID>BeepOnRepeat('h')<CR>
nnoremap <silent> j :<C-u>call <SID>BeepOnRepeat('j')<CR>
nnoremap <silent> k :<C-u>call <SID>BeepOnRepeat('k')<CR>
nnoremap <silent> l :<C-u>call <SID>BeepOnRepeat('l')<CR>

For another approach to limiting hjkl usage, see vim-molasses.

3

Alternatively use Medium Mode which still tries to learn you "right moves", but is less strict in doing that:

Medium mode is a Vim plugin that limits character-wise motions, but does not disable them completely. By default, medium mode will only allow two consecutive character-wise motions.

1

I had this same issue then I typed :help hardmodeLo and behold I found

'g:HardMode_level' If set to 'wannabe' 
arrow keys disabled, but not   hjkl etc.                                                                                              

By the way I am one of the vim beginners that found this plugin extremely helpful since it really pushed me away from the mouse.

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.