Context
The idea is to remove "processed" files from the args list as I finish with them. Say I've loaded a bunch of files into Vim's argument list. I'm going through them one at a time. Sometimes, I want to remove a file from the argument list, because I'm done with it, and then I want to move to the next file in the list. That's what the three step dance does. By the end of doing this dance for a while, the argument list contains only the files that I still need to process. In this situation, processing means reading a file deeply for understanding.
What have I tried?
My current approach is to delete the current item from the argument list, then go to the next, then return to the previous.
:argdel %
:next
:prev
What would I like?
What I would like is a single command to delete the current item from the argument list and replace the buffer contents with the new current item.
You can chain the commands:
:%argd|n|N<CR>
which is really one command and not substantially longer than your first one.
You can create a custom command:
:command! MyCommand %argdelete | next | Next
In the zsh prompt, I have a custom script to display the amount of time the last command ran for.
I've got this working, but how do I check if the user entered nothing and just pressed "Enter" or "Ctrl + C", in the previous prompt?
In this case, I do not want to show the amount of time the user spent idle, as this is not useful information for me.
$history[$((HISTCMD-1))] doesn't appear to work in this case. If the user typed nothing and just hit enter, it will just show the last command that was run, and not be an empty string.
If you define a function with the name zshaddhistory, it will be invoked every time enter has been pressed. $1 is the command being entered, and this can also be an empty command. You could set a global variable to indicate that an empty line has been entered, and use this variable later on.
Be careful with the return value from this function: If it returns 0, the command is saved into the history; otherwise it is not. You find more on this in the zshparam man-page, where _HISTORY_IGNORE_ is described, because the main use of this function is to control whether or not a command should be added into the history. Hence, an alternative would be to add empty lines to the history too and use your original approach with $history to access it. This has the disadvantage that seeing empty history entries is not really user-friendly.
Given the word foo in a text, if I type ciwbar<ESC> while recording it as a macro in w register and then type in the command line
:<C-r>w it will brings me :ciwbar^[
However if i type :<C-r>. it will only brings me bar
But both commands, the #w and dot, are able to perform the change inner word bar in other parts of my text
Is there any way to get all the content from the dot as I got the w register? I would like to paste it and work to automate the last command in macros in some steps forward
The . register and the . command are not the same thing. The . register stores the last text you have inserted and the . command repeats the last non-movement normal-mode command you executed.
Rather than try to force it to work another way, why not call a different registry and store values in there? The . registry will be overwritten by the last inserted text but many of the other registries are only changed when you actively tell vim to, so you can fill them yourself with the exact keypresses you want and call them again later.
Type
:reg
To see a list of what is currently inside each of your registries
Also have a look at this fantastic resource explaining vim's registers
https://www.brianstorti.com/vim-registers/
The following is from the documentation about the quickfix list and location list. But I am not sure what actually different. The image below shows the same things from the location list and quickfix list. When do I use one or another in vimgrep and lvimgrep.
In Vim the quickfix commands are used more generally to find a list of positions
in files.For example, |:vimgrep| finds pattern matches. You can use the positions
in a script with the |getqflist()| function. Thus you can do a lot more than the
edit/compile/fix cycle!
...
...
*location-list* *E776*
A location list is similar to a quickfix list and contains a list of positions
in files. A location list is associated with a window and each window can have
a separate location list. A location list can be associated with only one window.
The location list is independent of the quickfix list.
...
UPDATE
I found the following from here.
These commands all fill a list with the results of their search. "grep" and
"vimgrep" fill the "quickfix list", which can be opened with :cw or :copen,
and is a list shared between ALL windows. "lgrep" and "lvimgrep" fill the
"location list," which is local to the current window, and can be opened
with :lw or :lopen. Both of these lists can be used to instantly jump to
the matching line in whatever file it occurs in.
So the difference is all windows for quickfix list and local window for location list. However I can open location list from any other windows. So what is the difference then??
The location list is local to the current window so you can have as many location lists as windows: 30 windows? No problem, here are your 30 concurrent location lists.
The quickfix list is global so you can't have more than one available at a time. There are commands that allow you to replace the current quickfix list with a previous one but you can't have two concurrent quickfix lists.
Don't confuse the location/quickfix "lists" (the data structures) with the location/quickfix "windows" (the windows displaying the content of those data structures). The "windows" have similar behaviors but the "lists" don't. The difference is important because those windows are thankfully not the only ways to interact with those lists: there are many commands that allow us to move through those lists without opening the associated windows and knowing the difference between those lists is key to using those commands efficiently.
Hands-on illustrated example:
Do :lvim foo % in foo.txt to create a location list for the window containing foo.txt.
Do :lne a few times to jump to a few foo in foo.txt.
Focus on bar.txt and do :lne. What happens?
Now, do :lvim bar % in bar.txt to create a location list for the window containing bar.txt.
Do :lne a few times. What matches do you jump to? In which buffer? In which window?
Switch to the other window and do :lne a few times. What happens?
Switch again to bar.txt. What does :lne do?
Now, do :vim bar % in bar.txt to create a quickfix list.
Do :cn a few times to jump to a few bar in bar.txt.
Now, focus on foo.txt, what does :cn do?
Conclusion: the location you jump to with :lne depends on the window you are in, which makes the location list local to a window, but the error you jump to with :cn is not, which makes the quickfix list global.
Both lists have relatively clear roles IMO: the quickfix list (and thus the quickfix window) is usually and quite logically devoted to errors and the location list seems (to me) fit for search.
Just a question to improve my bash skills. I always do this:
$ history | grep some_long_command
...
...
123 some_long_command1.........
124 some_long_command2.........
...
I can then run the command the command I found by doing:
!123
However, I often want to do this:
some_long_command1foobar
I.e. change the command before I run it. Can you use bash to run this command instead:
#some_long_command1
so it gets commented.
Then I don't have to use my mouse to highlight the command, edit it and then run it (I can just use the keyboard - faster).
I suppose I could write a script to do it but there might already be functionality built in somewhere....?
I'd suggest instead of using the history command, you use ctrl+r and start typing that command. When you press an arrow key as if to go to modify it, it will drop out of autocomplete recognition, and will let you edit before running.
UPDATE: also, if you want to cycle through the different commands that contain the string you just typed, keep on pressing ctrl+r
Actually, you can just append :p to the command to print it without actually running it. For example:
$ ls -la
$ !!:p
Will print out ls -la as the previous command without running it, and you can just press ā (up) to find it and edit it.
You can also do
!123:p
to print out the 123rd command as your previous command.
You can also try fc command to edit the command in the history.
WIKI says,
āfcā is a standard program on Unix that lists or edits and reexecutes,
commands previously entered to an interactive shell. fc is a built-in
command in the bash shell; help fc will show usage information.
Apart from reverse-incremental search(Ctrl+R), we have some more bash shortcuts:
From man bash:
previous-history (C-p)
Fetch the previous command from the history list, moving back in the list.
next-history (C-n)
Fetch the next command from the history list, moving forward in the list.
beginning-of-history (M-<)
Move to the first line in the history.
end-of-history (M->)
Move to the end of the input history, i.e., the line currently being entered.
reverse-search-history (C-r)
Search backward starting at the current line and moving 'up' through the history as necessary. This is an incremental search.
forward-search-history (C-s)
Search forward starting at the current line and moving 'down' through the history as necessary. This is an incremental search.
non-incremental-reverse-search-history (M-p)
Search backward through the history starting at the current line using a non-incremental search for a string supplied by the user.
non-incremental-forward-search-history (M-n)
Search forward through the history using a non-incremental search for a string supplied by the user.
yank-nth-arg (M-C-y)
Insert the first argument to the previous command (usually the second word on the previous line) at point. With an argument n, insert the nth word from the previous command (the words in the previous command begin with word 0). A negative argument inserts the nth word from the end of the previous command. Once the argument n is computed, the argument is extracted as if the "!n" history expansion had been specified.
yank-last-arg (M-., M-_)
Insert the last argument to the previous command (the last word of the previous history entry). With an argument, behave exactly like yank-nth-arg. Successive calls to yank-last-arg move back through the history list, inserting the last argument of each line in turn. The history expansion facilities are used to extract the last argument, as if the "!$" history expansion had been specified.
shell-expand-line (M-C-e)
Expand the line as the shell does. This performs alias and history expansion as well as all of the shell word expansions. See HISTORY EXPANSION below for a description of history expansion.
history-expand-line (M-^)
Perform history expansion on the current line. See HISTORY EXPANSION below for a description of history expansion.
insert-last-argument (M-., M-_)
A synonym for yank-last-arg.
operate-and-get-next (C-o)
Accept the current line for execution and fetch the next line relative to the current line from the history for editing. Any argument is ignored.
edit-and-execute-command (C-xC-e)
Invoke an editor on the current command line, and execute the result as shell commands.
!123:gs/old/new/
Will run command 123 replacing the string 'old' with the string 'new'.
You can get to edit mode by hitting M-^ (option-shift-6 on a mac).
Type this:
!123M-^
And you'll be editing command #123. It's sort of like using ctrl-r, but starting with exclamation-point syntax.
Instead of using the history command, bind history-search-backward/history-search-forward to key shortcuts which can be remembered easily (I prefer PgUp/PgDown). To do that, put this into your .inputrc file:
"<key code>": history-search-backward
"<key code>": history-search-forward
To get <key code>, type Ctrl-V <key> in the shell, and replace the starting ^[ with \e in whatever was output.
After this is set up, you can just type some and press PgUp to get some_long_command. If you need some_long_command with_some_arg but there is a similar command some_long_command with_some_other_arg later in the history, you can cycle through until you reach it by typing some and then hitting PgUp repeatedly, or you can type some, hit PgUp, move the cursor to where the two commands start to differ, type a few characters and hit PgUp once more. This ability to quickly page through / differentiate between similar commands makes it in my opinion a much more comfortable tool than Ctrl-R.
You can also put
shopt -s histverify
in your .bash_profile, which causes any history expansion to appear on your command line without running it, allowing you to edit before doing so.
You may wan to try "suggest box"-like history https://github.com/dvorka/hstr - it reads Bash history and allows for quick navigation.
To get the last command simply type hh, navigate to the command and use right arrow to get it on command line (where you can edit it and/or add comment).
^p to get the last typed command in unix/solaris
Put
alias r='fc -s'
in your .bashrc (home dir)
then you can just type in
r <whatever>
at the command prompt and you will execute a copy of the last <whatever> command (same params) that is in your history. just hit up arrow to see what you have executed if you feel the need.