Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vim - navigating characters in command mode

Tags:

vim

Relatively new to VIM and having a great time using it.

One very minor annoyance I've been having is command mode character navigation when I want to revise a command. I'm used to using readline shortcuts on the regular (non-vim) command line but these shortcuts don't seem to work in : command mode.

For example, using ctrl + b to go back a character ends up sending me to the start of the line, or using alt + f to go forward a word ends up clearing the line and exiting command mode.

The only way I've found to navigate in command mode is to use the arrow keys, but I'm under the impression you should avoid the arrow keys in vim for max efficiency.

What is the standard way to navigate around in : command mode? Do vim users usually use the arrow keys here? Is there a different way to modify commands?

As a more concrete example, I've been using vimgrep a lot to search through files. I'll do a command like:

:vimgrep /font-family/j my-project/**/*.less | cope

Later, I'll want to use the same search but look for a different property, so I hit : then ctrl + p to access my previous vimgrep. Now here I have to use the arrow keys to navigate backwards to the search string and modify it. It would be much faster if I could use readline to navigate backwards by word then delete by word.

like image 443
kand Avatar asked Mar 11 '23 13:03

kand


2 Answers

For small edits, Backspace and light use of the cursor keys should be fine. For anything larger, I would advise to use the command-line window:

In the command-line window the command line can be edited just like editing text in any window.

So, there's no need to mentally switch to readline key mappings; just use the full editing power (including any custom mappings) of Vim!

You can switch via <C-F> from the command-line, or directly enter it (from normal mode) by pressing q: instead of :. All the details can be found at :help cmdline-window.

like image 166
Ingo Karkat Avatar answered Mar 16 '23 14:03

Ingo Karkat


I like this question. Long time vim user, but new-ish here, so I can't vote it up. But indeed, perhaps unofficially, many vim fans feel that most of the time the goal is to not have your hands leave home row position (fingers move, hands relatively still).

I will admit, when it comes to command mode, I use the arrows. With P being on my pinky finger, I would miss-hit ctrl-P a lot, and it's faster to slide my right hand down (on my Natural keyboard) and find the up-arrow by quick feel, instantly, to do the same thing. Once I'm there, left-right arrows are also easy to find without looking or delay. Also Ctrl-arrows let you skip by word.

One of the great things about vim is the :help. I have easily spent tens of hours over the years reading through it, and it solves 95% of my problems if I have enough time and working-memory to push deep enough into it.

Here is what I found for :help readline:

READLINE readline.vim ft-readline-syntax

The readline library is primarily used by the BASH shell, which adds quite a few commands and options to the ones already available. To highlight these items as well you can add the following to your |vimrc| or just type it in the command line before loading a file with the readline syntax:

let readline_has_bash = 1

This will add highlighting for the commands that BASH (version 2.05a and later, and part earlier) adds.

Give it a try! (I am using vim 7.4)

like image 29
Starman Avatar answered Mar 16 '23 13:03

Starman