Vim - how to auto complete buffer contents in command-line mode - vim

Use case: I have a buffer
variable1
variable2
variable3
i want to replace variable1 with variable3 using
:%s/vari<TAB>/vari<TAB><TAB><TAB>
can this be done somehow? I don't want to have to type out or manually copy words

I vaguely remembered this old plugin from way back and, well… it still seems to do the job:

You can use the command-line window (see :help cmdline-window) to use regular Insert mode to write an Ex command-line such as :s.
You can either use q: from Normal mode to access the command-line window, or use Control+F from the Ex command-line (after typing : and even starting to write a command such as :%s/vari...)
Once in the command-line window, you can use i (or a, A, etc.) to enter Insert mode, and then you can use the usual completion keystrokes such as Control+N or Control+P to complete from contents of other visible buffers.
Once the :%s command is complete, you can simply press Return to have the command from the command-line window executed as a regular Ex command.

Related

Vim normal mode in command pallete

When i write / or : vim opens command mode - i can write command like:
vimgrep /abc/
If i click shift+p it fill the previous command.
In this case if i want to change only letter a to b I should use arrow key until i get to the letter and then rename it
abc -> bbc.
Is there any faster way - arrow keys are far away and slower to write.
Can I use normal mode when I im in the command pallete(bottom place where i write the commands).
Thank you
You are confusing "command mode" (another name for normal mode) with "command-line mode".
Shift+P (or simply P should insert P. Did you mean <C-p>?
How large is your keyboard for your arrow keys to be "far away"?
See :help cmdline-editing.
Yes, Vim's command-line has limited editing capabilities; for complex edits there, you'd have to use cursor keys. Fortunately, there's a command-line window, which allows edits just like in any other buffer.
You'll find the complete explanation at :help command-line-window. Here's just a quick guide:
If you already know this is going to be a complex edit, open via q: / q/ instead of : / /.
If you only realize this while editing, you can switch via <C-f>.

Break out from Vim insert mode when pasting

Can one put control characters into a text so that when I copy and paste it in Vim it exits insert mode and does something nasty in command mode?
The short answer seems to be "yes". I was able to put the following in my clipboard:
hello<Escape>:!date<CR>
and when I pasted it into vim while in insert mode hello was typed and then the shell opened up and the date command was run.
Obviously if I can run the date command in the shell I can do much more nasty stuff.
To get that string in my paste buffer I opened vim and typed hello<C-V><Esc>:!date<C-V><Enter>. I then had to save that file, open it with Kate and copy the contents that way (copying from vim didn't preserve the control characters).
That depends on the environment, and the Vim command used.
Graphical GVIM can differentiate pastes from typed keys, but in the terminal, this is not (generally) possible. That's why Vim has the 'paste' and 'pastetoggle' options, to tell Vim what is expected. Despite that, if the character stream contains a key like <Esc> that switches modes, Vim will do so.
Instead of pushing text into Vim, it is safer to pull with Vim's put command: "*p. There, special characters like <Esc> will be inserted literally into the buffer; Vim won't switch modes here. The only Vim command that interprets register contents as typed (and therefore is susceptible to mode switch commands) is i_CTRL-R. To avoid that, one should use any of the other command variants, e.g. i_CTRL-R_CTRL-R.
summary
Pull text into Vim instead of pushing it; if you avoid the i_CTRL-R command (or neuter it by remapping it), this is safe. Additionally, the :registers command allows you to inspect all contents before pasting.

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 to use gg=G command in VIM for Windows on XML files from the gui?

I have installed VIM on Windows XP . I want to format XML files using the GUI / command line ? How to do that ?
I went though this post but where to enter that command gg=G from the gui?
Vi(m) is a modal editor, i.e. there is input mode for text input, normal mode to jump around and manipulate the text, and command-line mode, where you enter Ex commands that start with a colon, (and some more).
The gg=G is a normal mode command; since that is the default mode, just type it after opening the XML file. For it to properly work, the filetype must have been detected, but for a default installation, that should not be a problem. You then can save the reformatted file via :w (followed by Enter), and quit Vim via :q.
Vim is a different kind of editor, and very powerful once you learn its ways. If you're interested in it, I would recommend the built-in vimtutor (%ProgramFiles%\Vim\vim73\vimtutor.bat) as a good start.
Open the file in Vim and just press the characters. You don't need a special prompt for it. For example, you can also use the keys j and k to go down and up in the file.
If you want to insert new characters, you first need to go into "insert" mode by e.g. pressing i. Everything you enter now is interpreted as ordinary content input. If you want to leave insert mode, press ESC key and return to command mode.
Besides insert and command mode, there are a few more modes. You can access a command line by pressing : while you are in command mode. You can enter visual mode by pressing v while you are in command mode.
And just in case you are lost and want to leave vim, go from command mode to the command line by pressing : and enter q followed by return or just press ZZ while in command mode ;-)

Vim "show my last command" command?

Is there a command which shows what was the last command in normal mode?
Suppose I accidently hit random key and got some unexpected result.
Sure I can undo it, but could I reveal what key was pressed and how it was interpreted?
Hit the colon (:) and then use the up arrow to start going back through previous commands. You can use the up/down arrows too to move around the list.
q: will show you command history in Vim.
q/ will show you history of searches.
And must importantly, :q will quit the mode.
The text from the last command is stored in the . register. You can see all registers by :display. Unfortunately it doesn't say what the started the normal command.
To see commands from : (command mode) you can use :hist or q: which is limited to the last 20 (by default).
Another ability is to save the undo buffer :wundo undo.bin -- but the undo buffer is binary.
But none of these actually answer your question. I'm curious if it can be done.
Entering colon : then ctrl+p shows your previous command, i.e., moving backward through your vim command history. ctrl+n moves forward.
This is very convenient if you're used to using the command line and prefer not to change your keyboard hand positioning to use arrow keys.
It is difficult to know it. You can play with the variables:
v:operator
v:count (and v:prevcount)
v:register
But you cannot fully get the last normal mode command issued.
However if you want to systematically record everything you type while in Vim, you can launch vim -W ~/.vim-last-scriptout (a Windows version: vim -W "%HOMEPATH%\Vim\.last-scriptout) You can alias it in your shell on a UNIX machine. Every single key, or control-key, will be recorded into that file. Note that if you happen to use gvim or vim -g (the GUI) you might encounter this bug.
If you want to replay this file you can use :source! (with the exclamation mark) or the -s option from the command line.
On Windows I have set gvimportable.exe -W gvim_directory\last_scriptout as my default editor in my Commander program (FreeCommander). This way I can always remember what I have typed to do something and repeat a sequence of commands on another file. Of course I have another shortcut for opening Vim and playing the scriptout.
Note that the file might be written only when Vim exits, so you have to lose your session to know what you've done.

Resources