Vim :'<,'> When entering command mode - vim

Sometimes in vim I appear to be entering a keymap unintentionally when attempting to enter command mode. For instance, when attempting to write :w I, sometimes, end up with this:
:'<,'>w
Which throws the error E481: No Range Allowed
It's mostly just a minor annoyance, and I'm more wanting to know what am I doing to initiate the command line in this way with the brackets.

:'<,'>w appears when you start a command line while being in visual mode. It allows to apply this command on a portion of your document, e.g. to sort some lines. In your case, you have accidentally hit v before entering your command.

Adding to Vincent's correct answer, if you happen to come upon a command that doesn't support a range and gives you the E481 error (though the given :write does support ranges), you can just remove the '<,'> prefilled content by pressing Ctrl + U, and then start typing the command. This is quicker than Esc and re-triggering command-line mode via :.

Related

clearing selection after running substitute command

is there a way to clear highlighted text after running a substitute command in vim? For example, when I run:
:'<,'>s/<\([^>].*\)>/<!--\1-->/
to comment out the current line in an html file, after the command runs all of the text in the doc is highlighted and I have to hit the spacebar to clear the selection.
Is there anything I can add immediately after or as part of the substitute command so any selection is cleared automatically after the command runs so I don't have to hit the spacebar to clear it?
Well, <Space> doesn't clear anything by default so you have it must have it mapped to something like :nohl<CR>. Therefore, you need to execute the command that you mapped to <Space> after your substitution.
This is done by "chaining" Ex commands with |:
:'<,'>s/<\([^>].*\)>/<!--\1-->/|nohl

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