How to run the shell command in the appointed line in vim? - vim

I have a file edited in Vim with many lines. There is a specific line that contains a shell command, which I want to run.
How can I do this through Vim?

You can use this map:
:nmap ^ GI:!^V^[yy#"Xx
(Pick your favorite key command you don't use in place of ^ for the mapping;I like ^ because I always use 0 for its default function. Enter the ^V^[ with control-V control-V control-V Esc)
Then you can type 4^ to execute line 4, or just ^ to execute the last line in the file.

try Use
:exec '!'.getline('.')
This is like to copy the current line and run it.
You can also map this command to
map <F12> :exec '!'.getline('.')
getline receives the number of the line. if you will write 4 it will the line 4. The "." it run the current line.
so for run the command in line 4 you can write.
:exec '!'.getline(4)

Related

Mapping a working substitute command to a shortcut via vimrc

The following substitute command works fine on the command line:
:s/\vlabel\{(\w|:)+}/nonumber
It searches for label{eq:xyz123} and replaces its occurence on the current line line with nonumber.
I would like to map this to a command via vimrc. I tried:
nnoremap <leader>nn :s/\vlabel\{(\w|:)+}/nonumber<CR>
but this gives the error:
E492: Not an editor command: :)+}/nonumber<CR>
What is the right way to effect this mapping?
You can use:
nnoremap <leader>nn :s/\vlabel\{(\w<bar>:)+}/nonumber<CR>
Note the use of <bar> instead of |. The error occurs because | is interpreted as a delimiter of commands; i.e., Vim is interpreting your command as if there were two separate lines:
nnoremap <leader>nn :s/\vlabel\{(\w
:)+}/nonumber<CR>
For more, you can read :help map-bar.

How to copy text from Vim command line

How to paste into vims command line was asked here here.
But how can I copy from the vim command line?
For example:
:python import sys; print(sys.executable)
Now I want to copy that line to the clipboard, for pasting it into an other editor.
Yes, it is possible.
If you executed a command :foo, the command text line will be stored in register :, you can see it by :echo #:.
To have those value in clipboard, you just let + register have the same value.
So :let #+=#: or call setreg('+',#:) should help you.
You can assign to a register the content of another register:
:let #+ = #:
Will assign in the system clipboard (#+) the last executed : command (#:).
However, this will become your last executed command. If you want to keep your python command as the last one, you can do the following:
First open the command history:
q:
Go on the line of the command you want to copy, then yank it in the system register (visually selects the whole line and yanks it):
"+yy
This will copy the whole line with the new line character at the end, if you just want the command without the new line, you can do:
v$h"+y
Finally, close the command history:
:q
q: open command-line window, it's filled with command-line history, you can copy via "+yy.

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".

In VIM command line mode what is the special character/symbol for current line?

In VIM in command line mode a "%" denotes the current file, "cword" denotes the current word under the cursor. I want to create a shortcut where I need the current line number. What is the symbol which denotes this?
. (dot) stands for the current line.
To clarify:
This is meant for stuff like :1,.s/foo/bar/g which will transform every foo to bar from the beginning of the file up to the current line.
I don't know know of a way to get the current line number expanded for a shell command, which is what you are trying to do by doing :!echo .
You can find out about the expansions that are done (like % and # for example) in :he cmdline-special.
If you want to pass the current line number to a shell command, you could do
:exe "!echo " . line(".")
To return the line number of current line at bottom of screen, use:
:.=
Commands in vim works on the current line so:
:s/foo/bar/g
will transform every foo in bar on the line you are currently on.

Resources