How to copy text from command-line mode in Vim? - vim

Say, I just ran this command in Vim:
:nmap <CR> <C-]>
And now I want to copy this line and put it into my .vimrc.
How can I select and copy the whole line in command-line mode?

The fastest way is to run the command, switch to the destination
buffer (with .vimrc loaded, in this case) and paste the whole
command from the : register by typing
":p
in Normal mode.
If the command is further back in time, one can first recall it from
history (e.g., by typing the first few letters and pressing the up
arrow key ↑), rerun it, and then use the above method.
When these shortcuts are unhandy, one can resort to the general
approach of using the command-line window (see :help cmdwin).
To open it, either type q: in Normal mode, or press the key
combination set by the cedit option (Ctrl+F,
by default) in Command-line mode.

You can type Ctrl-F while in command mode to open up a special window with all previous commands. Then you can scroll to the desired line, hit yy to copy that line, then press Ctrl-C to return to command mode, and then ESC to return to normal mode. From there you can paste.
See :help cmdwin for more information on the command window.

Related

How to copy and paste in Vim's terminal mode?

I often want to copy text from a :terminal window to a normal text buffer. At the moment I exit the shell session and copy from the history.
There must be a better way to this.
Copy
To copy from a terminal window press CTRL-W N (This is a capital N)1 or CTRL-\ CTRL-N (this is not a capital N) to get into normal mode. From there you can use all usual vim commands to copy and paste stuff.
Entering insert mode will drop you back to your shell.
Paste
To paste from a register into the terminal window you have to be in Terminal-Job ("insert") mode.
Press CTRL-W " followed by the register.
:help Terminal-mode tells us:
When the job is running the contents of the terminal is under control of the
job. That includes the cursor position. Typed keys are sent to the job.
The terminal contents can change at any time. This is called Terminal-Job
mode.
Use CTRL-W N (or 'termkey' N) to switch to Terminal-Normal mode. Now the
contents of the terminal window is under control of Vim, the job output is
suspended. CTRL-\ CTRL-N does the same.
[...]
In Terminal-Normal mode you can move the cursor around with the usual Vim
commands, Visually mark text, yank text, etc. But you cannot change the
contents of the buffer. The commands that would start insert mode, such as
'i' and 'a', return to Terminal-Job mode.
See :h terminal-typing for more useful commands in terminal windows.
1Unfortunately the vim help doesn't tell you that it is a capital N, I kept the original notation
Can use Shift+Insert as a shortcut to paste from the clipboard into a running terminal session. Setup the mapping like
:tmap <S-Insert> <C-W>"+
The will result in pasting from the + register. Alternatively use the * register which sometimes works better in MS Windows.
Set the clipboard setting in your .vimrc to using system clipboard as the Vim's clipboard. Depends on your OS it may differ, assume you are using Mac OS: set clipboard=unnamedplus
Then you can use y command to copy then paste on another app just by Cmd + V, remember to exit the insert mode of the terminal by clicking any where or press Ctrl+\ Ctrl+n

Moving around the vim command line

I've been a vim user for a while now and I know how to move around the files, but is there anyway to do so on the editor command line itself (is there a name for that?).
e.g. I typed :vimgerp /sometext/ files/*.js and I realized I spelled :vimgrep incorrectly and I want to just jump to the beginning of that line and fix it. Any other sort of small tips here (jump between words -- neither w,e nor alt+left/right seem to work) also appreciated.
http://vimdoc.sourceforge.net/htmldoc/usr_20.html
<Left> one character left
<Right> one character right
<S-Left> or <C-Left> one word left
<S-Right> or <C-Right> one word right
CTRL-B or <Home> to begin of command line
CTRL-E or <End> to end of command line
The easiest method would be to use CTRL-F. This opens your command in the command-line window where you can edit your misspelled command like any other plain text.
from vimhelp
OPEN c_CTRL-F q: q/ q?
There are two ways to open the command-line window:
From Command-line mode, use the key specified with the 'cedit' option. The default is CTRL-F when 'compatible' is not set.
From Normal mode, use the "q:", "q/" or "q?" command. This starts editing an Ex command-line ("q:") or search string ("q/" or
"q?"). Note that this is not possible while recording is in progress
(the "q" stops recording then).
It's not the fastest and therefore probably only worth it for long commands, but you could just hit enter on the incorrect command as is, undo any incorrect changes if necessary, then open up your command history with q:, where you can edit it as you like. Enter then runs the command if you have your cursor over it in the command history buffer.
Also, shift+arrow left/right jumps between words in command mode, and you can use home/end to go to the beginning and end.

How to copy command string in the ex mode into clipboard?

How to copy command string in the ex mode into clipboard?
You can turn the command-line mode into a full Vim buffer named the command-line window by pressing <C-f>, or enter it directly via q:. There, you can yank as usual; e.g. via "+yy or :yank +<CR>. (This is also great for other complex edits!)
The last command-line is in register :; :let #+ = #:<CR> will copy that to the clipboard.
The least Vim-like approach would be selecting with the mouse; on Linux, the text is then in the X primary selection; on Windows, you can then use Edit > Copy to put it in the clipboard.

YankRing in insert mode

I'm using vim with YankRing.
Is it possible to paste an element from the YankRing while being in insert mode?
(Like it is possible to paste an element from the usual registers with Ctrl-R + the name of the register.)
Thanks
(NB: I'm not familiar with that particular plugin, so my examples might seem silly.)
When in Insert mode, you can always use CTRL-O (see i_CTRL-O) to execute one Normal mode command and immediately get back into Insert mode. So it appears you can just press CTRL-O followed by CTRL-P to use YankRing. Same goes for command-line mode: CTRL-O followed by :YRShow, for example.

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 ;-)

Resources