Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Search current line in ZSH (vi mode)

Tags:

vim

zsh

How do I search/navigate within the current line in zsh? For example, if the cursor is at the end of the line..

// [] indicates cursor position
user@hostname: vim /etx/apache2/sites-enabled/defaul[t]

In vi normal mode, I'd like to use backward-search (?), type etx and have the cursor move like so:

// [] indicates cursor position
user@hostname: vim /[e]tx/apache2/sites-enabled/default

However, / and ? are mapped to history search, not inline search. I know I can just type 9b and get there, but I find searching and moving to the match is easier than counting the number of words to jump.

Not sure if this was clear at all, let me know if I need to clarify things.

like image 884
dfickling Avatar asked Apr 05 '13 15:04

dfickling


2 Answers

I hope I understood you right. You want to in zsh command line, move your cursor faster when you type commands.

e.g.

user@hostname: vim /etx/apache2/sites-enabled/defaul[t]

You want to move to the first e

I don't use vi-binding, but f and F are your friends.

In that example, you could 5Fe move backwards to the 5th e . If you don't want to count, you could Fe, then press ;, till it moves to the right position.

check vim help for detail:

:h f
:h F

Also faster way would be 0fe, for this example. Moving cursor to beginning, then to e

If I misunderstood your question, please leave comment, I would remove the answer.

like image 69
Kent Avatar answered Sep 29 '22 09:09

Kent


Maybe the ~/.inputrc file has mapped these keys to something strange? Or you're not fully understanding how the search history works.

Let's start fresh: Remap these keys with bindkey:

bindkey -M vicmd "?" history-incremental-search-backward
bindkey -M vicmd "/" history-incremental-search-forward

Now, when you press 'esc' (for vi normal mode) and '?' you'll get a bck-i-search command:

%user@hostname: vim /etx/apache2/sites-enabled/defaul[t]
bck-i-search:

At this point, you type what you want to search for, e.g. 'etx'. And, the cursor moves to that position in this line. Note: if it doesn't find that pattern in this current line, it keeps on searching your history. This behavior is considered a feature!

You might notice that you can not repeatedly search (like pressing 'N' in vim). In this case add a few isearch bindings:

bindkey -M isearch '^N' history-incremental-search-backward
bindkey -M isearch '^R' history-incremental-search-forward

Now, pressing control-N repeats your search, while pressing control-S reverses the direction of the repeated search (note: the default order of this keybinding is reversed from forward to backward, since one is more often looking from end of the history back).

In short: treat the current line as the 'top' of your history. Using the vicmd '/' or '?' searches the entirety of that history. The '?' searches top down, while '/' searches from wherever the cursor is currently located in your history towards the 'top'. Another way of thinking about this is to imagine your history as one big file, and the current line your on is at bottom of that file. If that helps you grok it, you may feel that '?' is more pertinent than '/'.

like image 22
gregory Avatar answered Sep 29 '22 10:09

gregory