Vi - Use search command with yank - linux

I'm trying to use the yank command like this:
:0,/string,yy
To yank starting from the beginning of the current line, until the first instance of the word "string".
Obviously the above command does not work, as it tries to look for "string,yy". Can someone help me with this?
Thank you.

You can use a range and the :yank ex command to do so
:.,/string/yank
This can be shortened more to :,/string/y as the current line . can be assumed and :y is short for :yank.
Ex commands work line-wise. If you are trying to do this in normal mode and wish to yank characterwise then you are after the following:
y/string<cr>
For more help see:
:h :y
:h :range

Related

How to move to end of a file upon opening it via a command in .vimrc using vim/MacVim?

I'm trying to open a file using a command I set in my .vimrc file. The relevant line in my .vimrc is similar to the following:
command Of so /Users/Dude/Working/open_file.txt
With open_file.txt containing the following:
tabnew /Users/Dude/Working/Project/config.txt
What I'd like to do when executing the 'Of' command is navigate to the end of config.txt. I've tried adding a large line number which is unlikely to exceed the number of lines in the file like so:
tabnew /Users/Dude/Working/Project/config.txt
250000
This takes me to the end of the file but doesn't seem like the right way to do it. Ideally, I'd also like to add a new line after the last line and navigate there too.
A few things:
I would suggest you use full names instead of short names. e.g. so -> source.
source is probably the wrong choice here as you can do everything with the right-hand-side of command
May want to use ! with command so you can resource your vimrc file. e.g. command! Of ...
$ represents the last line of the file. No need to choose a magic number
Create a new line can be done with :normal o or :put _
So with some tweaks we get the following command:
command! Of tabedit /Users/Dude/Working/Project/config.txt | $put_
For more help see:
:h :command
:h :put
:h :range
:h :bar
Have a look at :h :normal in your case just write :norm Go instead of your number there.
:tabnew, like most variants of :edit (and the command-line arguments when launching Vim), takes arbitrary Ex commands via the [+cmd] argument. The $ command will move to the end of the file:
tabnew +$ /Users/Dude/Working/Project/config.txt

How to paste the Yanked lines in vim command line after typing other command?

I wanted to paste the yanked line in the vim command prompt after typing certain command.
I saw a solution where they asked to enter <Ctrl-R><Shift-"> to paste the yanked lines in the vim command prompt, however I am having the following problems:
When I try like, :tabnew and then type <Ctrl-R><Shift-">, whatever yanked line gets pasted after :tabnew line.
Eg: :tabnew /disk/bin/hello.log
The above solution doesn't work if I map the same above command in the vimrc. I tried adding the following map in my .vimrc:
:map <S-P> :<C-R><S-">
When I try :tabnew and type <S-P>, it is not pasting the yanked line, i.e. the mapped command is not working.
Can anyone help me on the above scenario?
FOLLOW-UP QUERY:
Is it possible to mix normal mode and command line mode operations?
For Eg:
a. I have a line in text file which is a directory path and wanted to open that directory in vim.
b. Instead of doing Yanking [S-Y] the line and then doing mapped command [map <C-T><C-O> :tabnew <C-R><S-"><bs><CR>] to open the directory for vim, is it possible to do something as given below ?
nnoremap <F7> <S-Y>cnoremap:tabnew <C-R><S-"><bs><CR>
Please drop you comments/suggestions?
The : command line prompt is "Command-line-mode" (see :h Command-line-mode, :h cmdline, or :h : [all show the same help]). You can map keys in that mode using :cnoremap. So you seem to be looking for this:
:cnoremap <s-p> <c-r>"<bs>
The backspace at the end removes the trailing end-of-line character that is (probably) at the end of the buffer.
I very strongly suggest you use a different mapping than <s-p>, because that will be triggered every time you try to type a capital "P".

Why does the <C-n> syntax for using control key work with :command command but not with :normal command?

I am trying to execute a normal mode command Ctrl-n or j from Ex mode. Normally, one would do this using the :normal command. For example, the following command moves the cursor one line down.
:normal j
So does the following command. Note: ^N is typed by pressing Ctrl-v Ctrl-n.
:normal ^N
But the following command does not work. This command seems to have no effect on the buffer.
:normal <C-n>
However, when I create a new Ex command for Ctrl-n using the following command, it works!
:command Down <C-n>
Even this works, although normal is redundant here.
:command Down normal <C-n>
Now, I can use the Ex command :Down to move the cursor one line down.
My question is why does the <C-n> syntax not work with the :normal command but works with the :command command?
use :exec and escape the <c-x>:
for example:
:exec "normal \<c-n>"
in fact the instruction you can find in :h :normal help doc:
to use |:execute|, which uses an
expression as argument. This allows the use of
printable characters to represent special characters.
Example: >
:exe "normal \<c-w>\<c-w>"
Your question is probably academic (or you are trying to solve another problem) but, for what it's worth, you can already do :+ and :join.

Re-execute command by :history option in VIM

How can you execute a command again listed in the
:history
option in vim. There are numbers shown. Is the only way to copy the command by hand, then re-enter it? Or is there something like in shell script.
:history is only there for you to look at it.
To re-execute a previous command, you have two options:
Use <Up> and <Down> at the command prompt:
:m10
(do stuff)
:<Up>
Use the "command-line window":
You can call it with q: and navigate with search and use the beautiful normal mode commands we all love.
Position the cursor on a line and hit <CR> to re-execute the command.
Edit a command and hit <CR> to execute the new command.
You can quit the command-line window with :q.
See :help cmdline-window.
I use q: shortcut in normal mode to open interactive command history window. You just move to the right command and execute it by pressing Enter. You can find more information and other ways of accessing history here.
What I like to do is type the first few characters in the command and press <UP>. For example if you want to repeat an edit command of the file file.txt you could:
:e fil<UP><ENTER>
If the first <UP> does not give you the command you want, keep pressing<UP> until you find the command you were looking for.
If Vim is compiled with +eval you can use histget( {history} [, {index}])
:exe histget('c', 15)
That isn't exactly convenient to type, so you can also create a user-defined command:
:com -nargs=1 HI exe histget('c', <args>)
Thereafter you can use HI {index} to execute the history entry:
:HI 15

vim command line editting

is there a way to use vim/vi in the vim command line? Sometimes I write a long
command in vim such as:
:!./script /home/user/pet --flag=1
and I want to change for instance "user" by "other". What I usually do is to
navigate the command line with right arrow which is time consuming and even more
when I want to go to the beginning of the line. I would like to have something
like "0" to go there or w/b to move by words. Or use j/k to go to the next/previous
command.
Thanks.
:h cedit
in command line, type ctrl-F(default) to enter command window.
or in normal mode type q:
(for search, type q/)
Vim has a feature called the "commandline window". You can enter it with Control-F by default when you're already on the commandline, or q: from normal mode, edit the commandline using vim commands, and press enter to execute. It also contains your command history so that you can yank previous commands if you like. See :help cmdline-window for more information.
I'm not aware how you can use Vim commands to edit a command directly on the command line, but if you enter the command window q: you get can use regular Vim editing to edit commands.
From there you can execute commands by hitting <CR> or use Ctrl-C to copy the command to the regular command line.
If you run set -o vi you will have vim capabilities in your command line. Just put 'set -o vi' in your .bashrc file or equivalent to have it by default.

Resources