Vim auto-completion on substitutions and searches in the command line - vim

Is it possible to use Ctrl+n style auto-completion when doing searches and substitution commands in vim?
What I mean is: say I have a variable named myNumber in my current file. If I enter insert mode and type myNu and then hit Ctrl+n, it will autocomplete the word to myNumber (assuming there are no other words that start with those letters).
Is there any way to get this same functionality on the command line? Is there a setting or plugin that will allow me to type /myNu and then hit a key to complete my text search to /myNumber ? Or, more realistically: let me type :s/myN and complete that to :s/myNumber so I can more quickly type out substitution commands?

While in command mode, press Ctrl+f — you will enter command line window. There you can edit your command like in vim. Auto-completion should work too.

Looks like the plugin http://www.vim.org/scripts/script.php?script_id=2222 does more or less exactly what I wanted. Upvoting Kent though, because that's really nifty.

This plugin also enables auto-completion in command line.
https://github.com/vim-scripts/sherlock.vim

Related

How do I change the way vim autocompletes commands?

I use the :split in vim all the time, I however neveer uses :spelldump or :spell* in general.
Is there any way to make :split be the first commmand to appear when autocompleting on :sp ?
Like I said in the comment you don't need to autocomplete for this particular use case, :sp will do the trick. If you enter part of a builtin command that is ambiguous, Vim prefers one command over the other:
:s could be :substitute, :split, :spelldump, etc., but for Vim it is :s[ubstitute]
:sp could be :split, :spelldump, :sprevious, etc., but for Vim it is :sp[lit]
In the help this is indicated with square brackets around the optional part of the command just as is shown above.
To answer the general question: I don't think you can change the way Vim autocompletes commands. You can define your own shorter command (which must be uppercase), e.g. :command! S split. Or you could define a mapping, e.g. :nnoremap \s :split<CR>. Finally, you could use a builtin normal mode command, which for this particular use case is simply CtrlW followed by S.

Keep vim always in command line mode with a ":"

Is there a way to make vim stuck in command mode with a : already typed in?
In that way, for instance:
I would type /fooEnter and the cursor would go to the beginning of the next line containing foo.
Next, I would be still on command line mode with a : already typed in for the next command.
Yes, start it in Ex mode, by invoking it either as ex or as vi -e.
You can also enter Ex mode from the normal visual mode by typing Q (must be upper case).
You can return from Ex mode to normal visual mode by using the vi command.
EDIT : This doesn't actually do what the OP is looking for. He wants to keep the visual display while keeping the cursor on the bottom command line. That may not be possible.
No, but you can map ; to : to put yourself "closer" to command mode.
I'll link to the Vim wiki instead of reposting identical information here.
http://vim.wikia.com/wiki/Map_semicolon_to_colon
You can build your own REPL, like this:
:while 1 | execute input(':') | redraw | endwhile
This is just a conceptual demo; you probably want to add a condition to quit this special mode. Also, commands like :append would need special handling to work properly.
As a last try, I could just initialize vim with -servername=FOO and then code a little script that would read from stdin and send remote-send to FOO whenever it detects(by parsing) a whole command was typed on stdin.
Then I would just use Vim and this other script side by side on different xterms/gnu screens.
EDIT
OK, I will use this one. This way I can even make :a command to enter vim's Insert mode and switch back to command mode when entering a line with a single .. This way I would also have syntax highlight on the fly when inserting text (you know, vim has a very pretty visual display of the text, I'm just too used with ed's interface). When I have so time I'll write this script and link it here.

How do I repeatedly search & replace a long string of text in vim?

I'm aware of the vim replace command, which is of the form, eg:
:%s/old/new/gc
But what if either of these strings is long? How can I use something like visual selection mode, the clipboard or vim registers instead of having to type the old/new text in?
You can use q: to bring up a command-line window. This lets you use all the vim editing commands to edit the vim command line, including p to paste. So, you could copy the text into a register, paste it into the command line window, and execute it that way.
I recently discovered this feature via vimcasts.
According to the manual, you can use Ctrl+R to insert the contents of a register into the current position in the command line. The manual also claims that Ctrl+Y inserts the text highlighted with the mouse into the command line. Remember that in X11 and some other systems, you can also paste text into a program from the system clipboard using the middle mouse button or a menu command in your terminal emulator.
I think to avoid have your command line be huge you can use this to solve your issue
:%s/foo/\=#a/g
That replaces "foo" with whatever is in register a.
If you're trying to do a substitute with a long complicated search pattern, here's a good way of going about it:
Try out the search pattern using some test cases and refine it until you have the pattern you want. I find incsearch really helps, especially with complicated regular expressions.
You can then use :%s//new to replace all instances of the last searched for pattern.
If you've entered a pattern and want to copy it out of the search history, you can use q/ to bring up a command line window containing recent search patterns very similar to the q: one that contains recent command history.
On the other hand, if you're asking about how to copy and paste text into the substitute command:
I'd write the pattern out in insert mode and yank the search and replacement into two distinct registers using, say, "ay and "by and then use :%s/<C-R>a/<C-R>b/gc to do the substitute. There are lots of variations of the yank command, but this one should also work automatically when using a visual selection.
If you're copying in text from the clipboard, you can use <C-R>* to paste it's contents in insert mode.
I have the following mapping in my .vimrc
vnoremap <leader>r "ry:%s/^Rr/
So I visually select the thing I want to replace, and hit ,r, type the replacement and hit return. If I want to paste the replacement, I yank it before selecting the text to replace, and then use <C-r>" to paste it as the replacement before hitting return.
Note: to insert ^R in your .vimrc, you actually type <C-v><C-r>.

how to write a shortcut map in vim?

I want to add a shortcut like I type :open_my_document, it become :e /var/book/document.txt.
how to?
If you want to create a custom command, use :command:
:command Open_my_document e /var/book/document.txt
Then you can use:
:Open_my_document
Normally you can just type :Ope and then hit tab and vim will complete the full command name.
Note that your command needs to start with a capital letter because only built-in vim commands can start with lower-case letters.
I think you might be looking for something like :h cabbr or :h cmap.
If you use cabbr, your text will not change once you enter your custom command. If you use cmap, it will. It's easier to just try both out to see which you'd rather use.
I don't know advantages/disadvantages of one or the other, personally, I use cmap.
Add this to your .vimrc:
map :open_my_document :e /var/book/document.txt

How do you search through Vim's command history?

I would like to have the following search in Vim too
(reverse-i-search)`':
Enter a word of your previous command, and you get the full command.
I know the chronological history tool in Vim
q:
However, it is not that useful as the fuzzy reverse search.
How can you have a similar reverse search in Vim as in the terminal?
Type q: in the normal mode to open commands window. You can search/edit here using regular vim commands. You start in Normal mode. Press Enter to execute a command.
This approach lets you search across whole command not just beginning of line.
Enter the first letters of your previous command and push <Up> arrow (or Ctrl+p).
:set li<up>
:set lines=75
Don't forget to check history option and set it to big enough value
:set history=1000
Press Ctrl+F in command mode to open the command history window. Then, you can use / , ? , and other search commands. Press Enter to execute a command from the history.
For more about the command history window, see :h cmdwin .
Here are the docs for Vim's commandline history, also see this part of the docs on Vim's commandline history that covers the key bindings while in the history. It looks like you can say :foo and then hit the up arrow to find the last command that started with foo.
With FZF fuzzy search command: :History:
Source: https://github.com/junegunn/fzf.vim
I was looking for this as well (finally after wondering why it wasn't built-in for some time) and decided I couldn't resist whipping up an implementation, so here you go: https://github.com/goldfeld/ctrlr.vim
It should work just like the shell's--well there are still a couple basic things missing (like pressing ^R again to skip to next match), but all that I use is in this first release, and I plan to add the rest in the coming weeks as I get time.

Resources