How do I repeat the last external command - vim

I would like to have the following behavior for a key in vim (for example the F8 key):
Find the most recent ex command starting with :! and repeat it.
For example, if my command history is
(...)
:!python abc.py
/hi
:%s/hi/ho/g
:w
then by pressing F8 I would like vim to run :!python abc.py

:!! repeats the last :!{cmd}. You can map it to F8 like so:
:nnoremap <F8> :!!<CR>

You could try the following mapping:
nnoremap <F8> q:?^!<CR><CR>
Explanation:
q: - open the command-line window
?^! - search for a line starting with !
<CR><CR> - hit enter twice to execute that command

nnoremap <F8> :!<UP><ENTER>

Related

Resize Splits in Vim after NERDTreeToggle

I am looking for a way to automatically resize my open v-split panes in Vim after I call NERDTreeToggle.
I have NERDTreeToggle being called on the shortcut "ctrl+\" at the moment, and ideally what I want is to call the keyboard shortcut "ctrl+w =" immediately afterwards.
Any ideas? Thanks.
If this is your current mapping:
:nnoremap <C-\> :NERDTreeToggle<CR>
You can just append the window command after it:
:nnoremap <C-\> :NERDTreeToggle<CR><C-w>=
Alternatively, you can execute this from command-line mode as well, via :normal!:
:nnoremap <C-\> :NERDTreeToggle<Bar>execute "normal! \<lt>C-w>="<CR>
Note that for window commands, there's also a special :wincmd to invoke them:
:nnoremap <C-\> :NERDTreeToggle<Bar>wincmd =<CR>

Vimscript: one command is not calling the other

I have the following code in my vimrc file:
" move line to end of file and add a timestamp
noremap ,d ddGp,t
" append a timestamp to the end of the line
nnoremap ,t A <Esc>"=strftime("%H:%M")<CR>p
In the above code, ,t works when called by itself. However, when called by ,d the ,t command does nothing. The rest of the ,d command functions as expected. Why is this? How do I fix it?
,d is marked as nore which turns recursive mappings off, and thus fails to recognize ,t as a command. Thank you FDinoff for the answer.
More info:
What is the difference between the remap, noremap, nnoremap and vnoremap mapping commands in vim?

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.

Vim map command to save file and run a test

How could I map a command to save the current file and then run it?
I am trying this:
:nnoremap <leader>r :w<CR>| !python %
This runs the python command instantly but not when I call the leader key. What am I missing here?
Just to follow up on what Conner was saying, you can do something like this:
nnoremap <leader>r :w \| !python %<cr>
Aha, I don't need the pipe character when writing a map, just a second colon:
:nnoremap <leader>r :w<CR> :!python % <CR>

Move forward/backwards one word in command mode?

Lets say I am copying a file with the vim command mode and my cursor is at the end of the line.
:!cp path/to/original/file path/to/new/file
Is there a way I can jump back a word like I can in the shell by typing Esc b?
You cannot use "Esc b" because, obviously, that would discard the command you where typing. However you can bind some keys to move around.
The question as already be answered here : Navigating in Vim's Command Mode
The easy way is just to add :
cnoremap <C-a> <Home>
cnoremap <C-e> <End>
cnoremap <C-p> <Up>
cnoremap <C-n> <Down>
cnoremap <C-b> <Left>
cnoremap <C-f> <Right>
cnoremap <M-b> <S-Left>
cnoremap <M-f> <S-Right>
In your .vimrc
For entering and editing complex commands, you may like working directly in the command line window which is accessed with the normal mode command q:. See :h 20.5 and :h q:. Or if you are already in command mode, you can access the command line window with C-f.
For example, in normal mode type q: to get into the command line window. (or type C-f from command line mode.
You can move around previous commands using standard motions and you can edit as usual.
When you want to execute a command that you just edited, press enter in normal mode in
this command line window. The line your cursor is on will be executed as a command in
the window you were in before you opened the command line window.
Another option to consider is to edit/yank the command from another buffer. You can do this by yanking the desired text and pasting it in command mode by typing C-R n, where n is the register you yanked to.
BTW: I like the mappings that #rks provided. But if you don't have these mappings, you can use the out of the box commands. Look up :h c_<S-Left> and :h c_<S-Right> and :h 20.1.
A nice vim feature is ctrl-f. Typing ^f (or whatever key is specified in the cedit option, with ctrl-f being the default) from command line mode has the same effect as typing q: from normal mode; it pulls your entire command history into a window and lets you edit it as a buffer. Try :help cmdwin for more details.
In vim's command mode, I just use ctrl-left and ctrl-right arrows. The same works in bash - I wasn't aware of the esc-b method there.
No editing of the .vimrc file is required for this on my Ubuntu and Debian systems, but YMMV on others. It's presumably based on the standard configuration that's packaged for the OS

Resources