In what way I can make sort of a shortcut to parametrized search in vim? So when I invoke (for example) :customsearch foo, I get a search for /bar foo baz$. Or more generally /bar %s baz.
Add a command to the vimrc file such as this:
Note that the command must start with a capital letter.
command -nargs=1 Customsearch :
\ /foo <args> baz
#TonyMyddeltyn's answer shows a custom command, which is a valid approach. As you're asking for a shortcut mapping and real-time display, you can have that by defining an incomplete search mapping.
Think of first preparing the search (with parameters already inserted and the cursor placed in the middle), and then completing it by filling in the blanks and triggering searching via Enter. That first part can be supplied by a mapping, like this:
:nnoremap sf /bar baz$<Left><Left><Left><Left><Left>
Related
I have XML entries that span two or more lines sometimes, and so I can look for something like this:
something on one line
But if I try the vim command /something on one line and the line is like this:
something on one
line
then it doesn't find it, because the second text block is actually seen as
something on one^J line
Which might have something to do with the fact that I'm using a DOS formatted file.
How can I get vim search to ignore whitespace and newlines?
In vim search, _s means a new line, a space or a tab. So you can try something such as:
/something on one\_s*line
to match the example string you used as example.
Replacing the spaces with \_s\+ manually is cumbersome. The Search for visually selected text topic on the Vim Tips Wiki has a mapping that does this for you for the current visual selection. You can use it like the built-in * mapping, to search (ignoring spaces) for the current selection. Very handy!
While I like the existing answers, I was looking for a way to do the substitution of spaces to \_s* "automagically" for every search that I type.
Luckily, such a solution is possible, as explained in this reddit post. I ended up using the following command:
cnoremap <expr><space> '/?' =~ getcmdtype() ? '\_s*' : ' '
This makes it so that whenever you type a space in a search command (either / or ?), all spaces that you type are automatically replaced by \_s*.
In fact, I only wanted this behavior for LaTeX files, so I added the following to my .vimrc:
autocmd FileType tex cnoremap <expr><space> '/?' =~ getcmdtype() ? '\_s*' : ' '
I have the vim EasyMotion plugin installed.
Pressing,
<Leader><Leader>f searches forward from the current line.
<Leader><Leader>F searches backward from the current line.
Is there a way to search the entire visible part of the buffer only using 'f'? I would ideally not want to use two different bindings for searching forwards and backward. One single binding to search the entire visible portion of the buffer would be most ideal.
You can try a mapping like this:
nnoremap <leader><leader>f :execute "/\\%>" . line('w0') . "1\\%<" . line('w$') . "l"<left>
That's a confusing syntax, so I'll unpack it.
line('w0') and line('w$') return the line numbers of the first and last visible lines in the buffer, respectively, so you use them to find the range for the visible part.
The / search command allows a range to be specified, but with an odd syntax. The format is /\%>Xl\%<Yl where X is the line to start from and Y is the line to end at.
It's not possible to just drop the results from line() into a normal / invocation, but we can build a string, using . to join segments together, and once the command is built, pass it in to :exec to make it happen.
Finally, there's the <left>. That's for cursor positioning. When you execute <leader><leader>f, the whole mapping fires as though you were typing it, so you end up with the full :exec command on the line, and it ends with a ", but you want to type inside those quotes. Alternatively, you could remove "<left> from the end of the mapping, but then you'll have to remember to close the quote after typing your search term.
I'm not familiar with EasyMotion, so this may not give you exactly what you were asking for (I realized this after I typed up the answer), but it will let you run a search in the currently visible part of a buffer only, and you can probably adapt it to EasyMotion's purposes without too much difficulty.
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.
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.
This script adds this functionality to zsh:
https://github.com/soheilpro/zsh-vi-search
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 '/'.
Type v when you are in command mode of your shell, you will be taken to real ViM editor itself. Upon saving & exiting, it will automatically get executed.
I had the same issue. I didn't manage to solve it as such but found a suitable workaroud: added a binding for the edit-command-line function, which drops me to $EDITOR with current line in buffer. There it's easy to navigate to the given pattern.
See /usr/share/zsh/functions/Zle/edit-command-line on how to bind the function.
I know that the incsearch setting controls how search in vim highlights as you type. I would like to have the same incremental search and highlight when using the replace command (:%s/foo/bar/ )
The easiest way to do that is to do a search like normal, using 'incsearch' to help ensure the pattern is matching what you want. Once you've got that nailed down, you can either
Leave out the search pattern in :%s//bar/. When there's no specified search pattern, the current value of the / register is used, which will be the search you just did.
Insert the search pattern into the :s command using Ctrl+r/ (see :help c_ctrl-r) or Ctrl+rCtrl+o/ (if the search contains control characters, like ^H). This is useful if you want to make some final tweaks to the pattern or if you want to have it in your command history so you can reuse it even after performing another search.
You could add c at the end of your command like that:
:%s/foo/bar/c
It will walk through all the instances of foo and ask for your confirmation (that's what the c stands for) before changing it to bar.
It's not exactly what you are after though.
The goal is to use the current line as a TODO and send this to some external program. Something like this:
:! /usr/bin/todo "content of current line"
I know of the filtering command but this implies that I want to edit the current buffer which I do not want (:.! acts as a filter). I know how to get the current file with '%' but isn't there any way to get some other content ? Maybe by using :execute ...
:.! works as a filter, but :.w ! (mind the space!) just passes the output. See :help :w_c. I.e.
:.w !/usr/bin/todo -
You can insert contents of registers into command line, so doing something like:
"1y$ //yank current row to register 1
: CTRL-R 1 //CTRL-R followed by register id pastes register to command line
should do the trick.
You might like something like these mappings (i.e. saved in your .vimrc or pasted into a : prompt):
cmap <C-R>' <C-R>=shellescape(getline('.'))<CR>
cmap <C-R><C-R>' <C-R><C-R>=shellescape(getline('.'))<CR>
Once installed, you use them like this:
:!/usr/bin/todo ^R'
(type an actual Control‑R where the above example shows ^R).
You can think of them as command-line mode versions of the registere-based Control‑R and Control‑R Control‑R (see :help c_CTRL-R, and :help c_CTRL-R_CTRL-R) where the “imaginary” register ' always contains the shell-quoted contents of the current line.
Because these mappings use the same prefix as built-in mappings (see the :help topics mentioned above), you must enter the final single quote within timeoutlen milliseconds (see :set timeoutlen?), or it will default to the built-in mapping (see :help map-typing).