Habit breaking, habit making
2015-09-09 16:00
351 查看
Habit breaking, habit making
http://vimcasts.org/blog/2013/02/habit-breaking-habit-making/Feb 6, 2013
Moving your Vim cursor around using the arrow keys is a bad habit, and like many bad habits it’s a difficult one to break!
Putting these lines into your vimrc can help:
noremap <Up> <NOP> noremap <Down> <NOP> noremap <Left> <NOP> noremap <Right> <NOP>
This snippet causes each of the arrow keys to execute no operation, or in other words: it disables them. Next time you move your hand to the arrow keys you’ll find that nothing happens when you press them. That should remind you to move your hand
back where it belongs: on the home row, where
h,
j,
k, and
lkeys are waiting for you. Alternatively, you could use the
konami code version of this snippet.
Learning to operate Vim without leaving the home row is the first rite of passage. If you’re still in the habit of moving around using the arrow keys, then you should disable them today.
Stop using the h
, j
, k
, l
keys!
There’s nothing slower than moving one line or column at a time. Vim provides dozens of motions for moving around quickly, so it’s no exageration to say thatholding down j is a Vim anti-pattern.
Learning Vim is not unlike studying a foreign language, where adding a word to your vocabulary increases the number of things that you can say. It takes time and practice to pick up Vim’s motions, but every time you add a motion to your repertoire you’ll
discover scenarios where it can save you time and keystrokes.
For the sake of learning, let’s say that it’s a bad habit to use the
h,
j,
k, and
lmotions. In that case, you should disable them by putting this snippet in your vimrc:
noremap h <NOP> noremap j <NOP> noremap k <NOP> noremap l <NOP>
Not being able to move one line or column at a time will force you to use other motions to get around. If that sounds scary to you, let me introduce a couple of the most useful motions. Learning these won’t take a lot of effort, and the payback will be huge.
Wordwise motions are 5x faster than h
and l
The w,
b,
e, and
gecommands allow us to move forward or backward to the start or end of a word. The
W,
B,
E, and
gEcommands do the same for a WORD (see
:help word for the difference between words and WORDs). If we say that the average word length is 5 letters, then moving back and forward a word at a time is approximately five times faster than using
hand
lto move a character at a time.
Start off by adding
wand
bto your repertoire. These move forward and back to the start of a word. When you find yourself wanting to get to the end of a word, add e and ge to your repertoire.
Character search is near-instant for moving within a line
Thef,
F,
t,
T,
;, and
,commands make up the suite of character search motions. When you press
f{char}, Vim looks forward from the cursor position for the next occurrence of
{char}on the current line. If it finds a match, the cursor moves directly there. If no match is found, nothing happens. (Vim might beep at you, but you can mute that by
setting ‘visualbell’.)
Try this: in Vim, move your cursor to the beginning of a line with lots of text on it. Look ahead for a character that occurs with low frequency, such as a punctuation mark or uppercase letter. Press
ffollowed by the character that you picked. That’s two keystrokes. Are you there yet?
If your cursor stopped on a match before the one you were aiming for, press ; to repeat the search. Keep pressing ; until you hit your mark. If you overshoot, press , to reverse the search.
For uncommon characters, you can usually hit your target from a distance with only 2 keystrokes. Common characters aren’t such easy targets for this method, so it helps to think like a Scrabble player!
The character search commands allow for efficient navigation within the current line. I use them all the time! If these motions are not a part of your repertoire, then you owe it to yourself to get practicing with them. Character search allows you to move
around close to the speed of thought.
Use h
and l
for off-by-one errors
Sometimes, I’ll accidentally use f{char}when I should have used
t{char}, which places me one character away from where I wanted to be. Or perhaps I’ll use
f{char}to target an uncommon character that’s adjacent to the common character I actually want to hit. I refer to these scenarios as off-by-one errors. I consider them to be one of the few occasions where it’s acceptable to use the
hor
lmotion.
Use <NOP>
mappings to break bad habits
I’m not really suggesting that you permanently disable the h,
j,
k, and
lkeys. After all, they’re necessary for correcting those off-by-one errors. But if you’re having a slow day at work, then disabling
h,
j,
k, and
lfor the afternoon could make things more interesting. If it forces you out of your comfort zone and encourages you to use wordwise motions, character searches, and other motions, then it counts
as a useful exercise.
Meet hardmode
If this sounds like a worthwhile challenge, you might want to install thehardmode plugin (which I heard about from Rob Miller at
Vim London). This provides convenience commands for enabling and disabling the
h,
j,
k,
l, and arrow keys in one go. To disable cursorwise motions, run:
:call HardMode()
If it gets too difficult, you can wimp out and re-enable these keys by running:
:call EasyMode() You are weak...
相关文章推荐
- Linux初学——文件属性
- 对LCS算法及其变种的初步研究
- Your content must have a ListView whose id attribute is 'android.R.id.list'z
- Acitivty生命周期
- 为ubuntu与windows搭建桥梁samba ssh xshell
- android.content.res.Resources$NotFoundException: String resource ID #0x1
- oracle错误之未知的命令开头imp忽略了剩余行解决方案
- delete window的属性,在ie8中报错问题
- 记c#中构造函数中this的用法
- [股票] 入市
- 0909 第一次上机课之《为什么学操作系统?》
- 0909《操作系统教程》理解和认识
- 什么是REST?以及RESTful的实现
- MySQL数据库(修改表)
- Spring注解入门
- PHP输入流php://input
- Android_向通知栏发送通知
- First Bad Version
- 【Android学习】实现listview反序
- Windows_dll中共享数据段___ data_seg()