Reuse last externally executed command in VIM - vim

In VIM, I want to execute a command (like :!mkdir src/main/scala/xxx)
Then, I want to also make a subdirectory of the just created directory.
Can I have VIM retype the last used command and then I append the sub directory name to it
(So I can have :!mkdir scr/main/scala/xxx/yyy without retyping the whole stuff).

Can't you just hit : then the up arrow to go through your command history?

:!mkdir test
:!!/test2
will do what you want, see :h :!. Citation from there:
Any '!' in {cmd} is replaced with the previous external command (see also 'cpoptions').
// Why don't you use -p switch to mkdir? mkdir -p test/test2/test3/... will create directory with all its parents if they do not exist.

Related

How to run a list of vim commands in the current buffer from an external file

I frequently use Vim to parse and clean up data exported from other sources. Often, I find myself running a sequence of substitute commands. I'd like to save those commands to some kind of script file, then run it on the current buffer.
An example of the commands found in sample script could be:
:%s/pattern1/pattern2/g
:%s/pattern3/pattern4/g
:v/pattern5/d
... etc
If the script were in the same directory as the current buffer file (or maybe in the .vim folder), how could I load and run these commands as if I were typing them in manually?
If you have a Vim window open and want to execute VimL on a buffer, you can do:
:source /path/to/vim/file/name.vim
Put your commands in script.vim:
:%s/pattern1/pattern2/g
:%s/pattern3/pattern4/g
:v/pattern5/d
Use it like this:
$ vim filetocleanup -s script.vim
See :help -s.

meaning of "command" prefix and suffix of "!"

I am learning vim from vimtutor and I am currently on lesson 5 where they introduce external command prefix !command and external command suffix command!. I tested the command ls and did the following:
After typing :!ls, I got:
Desktop Downloads Music Public TEST
Documents Pictures Templates Videos
Press ENTER or type command to continue
When I saw the lists of folders and files, I knew that it acted like typing ls in the terminal, but when I typed :ls!, it got:
:ls!
1 %a "/tmp/tutorhN8t15" line 600
Press ENTER or type command to continue
Which made me confused of what the external command ! really means. What does it really mean?
:!command executes external command command.
:command! executes internal command command with a "bang" that usually modifies its behavior.
So…
:!ls executes your shell's ls command, which lists the files and directories in the working directory.
See :help :!.
:ls! executes Vim's ls command in a way that forces it to show listed and unlisted buffers.
See :help :ls.

what is the load order of scripts when you start up vim?

If you start up vim with something like this:
vim -S myscript.vim file.txt
What is the load order of scripts? Does myscript.vim get loaded after or before ~/.vimrc.
If you pass in vimscript commands to vim directly on the command line, when do they get executed relative to sourced and default vimscripts?
I believe vimrc is always first. You can run :scriptnames to get a list of sourced scripts in order in which they were first sourced in your Vim instance.
The help entry is way too long to post here, but it lists the order of everything that vim does at initialization. See :help initialization.
The answer is myscript.vim gets loaded dead last.
The vim -V option is a lifesaver here. (Capital -V, because -v starts in vi mode.) Just ran across it, after searching further since although the other answers answered your question, they don't show what wasn't sourced because it wasn't found. If I could send it back in time, I'd save myself a lot of time banging my head against strace output.
This will not only show you all of the scriptnames that it did source in order, but also all of the scriptnames that it would have sourced if they existed in order. So, you can discover what files you can create to load at the appropriate time.
$ vim -V
Adding it to your vim arguments easily answers the question.
$ vim -V -S myscript.vim file.txt
It shows myscript.vim as dead last.
It prints a ton, and winds up at a "Press ENTER or type command to continue" prompt, which lets you step through Autocommands.

Alternative to Up Arrow + Enter to run previous command?

Sometimes I have to run a command many times in succession, for example to see if a service has started, and it becomes tedious to move my hands away from my normal typing position to press the Up Arrow and Enter keys repeatedly. Is there a way to run the previous command without the Up Arrow and Enter keys, perhaps with an elaborate shell script?
I've tried the following, but it is unsatisfactory because it cannot execute aliases, and it is a little slow.
history | tail -2 | head -1 | cut -d' ' -f4- | cat > prev_command.txt
sleep .01
chmod 777 prev_command.txt
eval prev_command.txt
rm prev_command.txt
Ideally I'd have an alias to this script so I can type in something like "prev" in the command line and hit Enter to run the previous command again.
In bash, you can press ctrlp to go to the previous command -- that's a lot better than having to move to the arrow keys.
See also: https://github.com/fliptheweb/bash-shortcuts-cheat-sheet/
Use
!!
to run your previous command.
sudo !!
also works , for the record.
Instead of running the same command many times in succession, why not watch it instead? watch will run a specified command repeatedly and display the output in stdout so you can see it change over time.
watchcommand
I often use the "history expansion" feature in bash (usually activated with cntlR) -- it interactively searches through your history for the previous closest match.
See the bash manual section Searching for Commands in the History, and also Using History Interactively.
Are you an emacs or vi user? You can use
set -o vi
set -o emacs
to set emacs or vi keybindings. You can then use the emacs or vi key bindings in bash. I don't know if this should work for other shells. I believe the vi mode starts in insert mode, so you need to hit esc to enter command mode. In emacs mode (the default), you can use ctrl+p and then ctrl+j to move to the previous line and do a carriage return.
Otherwise, you can use !! as someone else suggested.
In bash:
$ help fc
fc: fc [-e ename] [-lnr] [first] [last] or fc -s [pat=rep] [command]
Display or execute commands from the history list.
fc is used to list or edit and re-execute commands from the history list.
FIRST and LAST can be numbers specifying the range, or FIRST can be a
string, which means the most recent command beginning with that
string.
Options:
-e ENAME select which editor to use. Default is FCEDIT, then EDITOR,
then vi
-l list lines instead of editing
-n omit line numbers when listing
-r reverse the order of the lines (newest listed first)
With the `fc -s [pat=rep ...] [command]' format, COMMAND is
re-executed after the substitution OLD=NEW is performed.
A useful alias to use with this is r='fc -s', so that typing `r cc'
runs the last command beginning with `cc' and typing `r' re-executes
the last command.
Exit Status:
Returns success or status of executed command; non-zero if an error occurs.
Note the suggestion for alias r; I use this frequently.
Depending on what terminal you're using, I know a lot used to have F3 as an option for repeating, but that's still outside the normal range for typing as well unless you have a special keyboard with more accessible function keys.
My keyboard makes the function keys easily accessible, but I don't do much command line work in unix any more, so I wouldn't be able to tell you for sure whether or not this is still possible.

Bash Shell - What is equivalent of DOS shell F8?

When working an interactive bash session, one aspect from the Windows shell I miss is the F8 key where you start typing a command, hit F8 and the shell finds the most recent command entered in history that matches what you have typed so far. e.g.
me#Ubntu07:~>cd /home/jb<F8 Key Here>
brings up my prior command:
me#Ubntu07:~>cd /home/jboss/server/default/log
Is there any way to do this in bash ?
Hit Ctrl-R before you start typing.
(There may well be another version which finds commands based on what's already been typed - I wouldn't know, as Ctrl-R has always been good enough for me :)
Pressing Ctrl-R again shows the next match etc.
My Gentoo is configured in a way that I can press PgUp and PgDn to scroll through those commands in the command history that start with what’s currently in my command line.
# cd<PgUp>
results in:
# cd hydrogen
That’s pretty much the same function. It is defined in my /etc/inputrc with the following lines:
# mappings for "page up" and "page down" to step to the beginning/end
# of the history
"\e[5~": history-search-backward
"\e[6~": history-search-forward
I have these lines in my .inputrc file:
"\e[A": history-search-backward
"\e[B": history-search-forward
This binds history search to the up and down arrow keys. So you can start typing a command, kextload say, and then each tap of the up arrow will complete the line with the previous command that started with kextload.
All of my config files are public on github.
http://github.com/jonshea/config-files/tree/master
In your case !jb would print and then run that command.
e.g.,
$ nano logconfig.properties
$ !n
nano logconfig.properties
$
Of course if you want to be on the safe side, use ctrl-r first to bring up the interactive command history.
Ctrl + R does a history search. It's a bit different in that first you hit Ctrl + R and then type what you're looking for.
If you're just talking about a command, you can use the !<cmd> to do the last one. For example, say you entered python runscript.py a while ago; you can type:
!py
or something along those lines to run that command again.
To repeat an argument to a command, you could do something like this:
echo !py:1
which would echo runscript.py back to the terminal, in this example. The number after the colon refers to the argument you'd like to use from the given command.
There's a lot of other great information about the bash history here.
If you use vi input mode (set -o vi in bash or via set editing-mode vi in .inputrc), you can use normal vi commands to search the history (/). This gives you full regular expressions, too, which can be helpful for finding a complex command.

Resources