When in insert mode in vim, I often do CTRL-R and retrieve a register content.
I like to be able to retrieve text that's not currently in a buffer, like the output of the shell command, pwd.
I was under the impression I could hit i_CTRL_R ! pwd sequence, but i_CTRL_R can only takes a register.
The expression register = seems to be the only way to compute things dynamically, but I don't know what is the minimum number of key-strokes to get the = register to be populated with a shell output, or env variables.
Vimmers?
You can insert environment variables directly with:
<C-r>=$PATH
For inserting the output of shell commands you will need :help system() if you absolutely need to stay in insert mode:
<C-r>=system('ls')
If you don't mind leaving insert mode temporarily, you can use :help :read:
<C-o>:r!ls<CR>
Related
So, this may not be a clear question because of the title. But here's the actual explanation:
I have this custom function, I want this function to be able to be called from the command line, this is done (:FunctionName arg), but now I need to make this function react to a certain mapping.
So when the user presses <leader>cs it will prompt the user for the arg part, mapping this is still kind of unclear to me, but also how to achieve this functionality. This is kind of what the Surround script does, where it lets you input the old character and the new character to replace it with.
I need this for my first script that I'm making BTW, which allows you to change the file's syntax in a manner similar to Sublime Text's way.
Thanks for all your help!
The simplest way is to remove the concluding <CR> from the mapping, so that it just enters command-line mode and fills the command line with your custom command:
:nnoremap <Leader>cs :FunctionName<Space>
You can then enter the arg and trigger the command with Enter.
Alternatively, you can query for user input via input() (and single characters with getchar(); obvious, isn't it?!), like this:
function! FunctionNameWithQuery()
let arg = input('arg: ')
execute 'FunctionName' arg
endfunction
nnoremap <Leader>cs :call FunctionNameWithQuery()<CR>
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.
How can I create a Vim command and copy it's results to clipboard?
I want to convert Markdown to HTML and copy the result to the clipboard. So far I got:
nmap md :%!/bin/markdown/Markdown.pl --html4tags
But this will substitute my opened file on Vim to the result of Markdown.
You didn't say which system you're using, but generally saving it in the +
register should work. You can call system():
:let #+=system("markdown --html4tags", join(getline(1,line("$")), "\n"))
The system() function takes the second parameter (optional) as input to the
command, and here I'm using a chain of other functions to retrieve the contents
of the current buffer. Not sure, but there should be a better way to do it (if
someone knows, please let me know).
Alternatively, you can pass markdown your file name as input directly:
:let #+=system("markdown --html4tags " . shellescape(expand("%:p")))
But keep in mind that you'll need to write the file before calling this.
Two important notes:
I didn't type your full path to markdown. Use it.
I didn't use maps here, the final result would be something like:
nnoremap md :let #+=system(...)
get the xsel package
and pipe stdout to xsel --clipboard
For instance:
cat /etc/passwd | xsel --clipboard
Is that what you're looking for?
Filling in a missing piece (2+ years late). With the clarification that the user was on a Mac and since the asker's "why doesn't it work for me?" question was not answered.
To redirect the output of a command to the system clipboard from within MacVim (GUI version) you need to set the '*' to be the "clipboard register" you need to change the clipboard setting to 'unnamed':
set clipboard 'unnamed' # 'cb' can be substituted for 'clipboard'
Then sidyll's answer should work except specify the '*' register and not the '+' register:
:let #*=system(...)
The clipboard feature is likely not compiled into the "terminal version" of MacVim and when it is available option setting is different from 'unnamed'. To see more details regarding what works where and how, see the documentation in MacVim using the Vim help command:
:help 'clipboard' (include the single quotes since it's a set option!)
(I'll skip the command mapping issue since it always takes me several tries and I still have to look it up; finding the help for the mapping commands should be easier than finding it for the * register.)
The goal is to use the current line as a TODO and send this to some external program. Something like this:
:! /usr/bin/todo "content of current line"
I know of the filtering command but this implies that I want to edit the current buffer which I do not want (:.! acts as a filter). I know how to get the current file with '%' but isn't there any way to get some other content ? Maybe by using :execute ...
:.! works as a filter, but :.w ! (mind the space!) just passes the output. See :help :w_c. I.e.
:.w !/usr/bin/todo -
You can insert contents of registers into command line, so doing something like:
"1y$ //yank current row to register 1
: CTRL-R 1 //CTRL-R followed by register id pastes register to command line
should do the trick.
You might like something like these mappings (i.e. saved in your .vimrc or pasted into a : prompt):
cmap <C-R>' <C-R>=shellescape(getline('.'))<CR>
cmap <C-R><C-R>' <C-R><C-R>=shellescape(getline('.'))<CR>
Once installed, you use them like this:
:!/usr/bin/todo ^R'
(type an actual Control‑R where the above example shows ^R).
You can think of them as command-line mode versions of the registere-based Control‑R and Control‑R Control‑R (see :help c_CTRL-R, and :help c_CTRL-R_CTRL-R) where the “imaginary” register ' always contains the shell-quoted contents of the current line.
Because these mappings use the same prefix as built-in mappings (see the :help topics mentioned above), you must enter the final single quote within timeoutlen milliseconds (see :set timeoutlen?), or it will default to the built-in mapping (see :help map-typing).
Something that I want to do from time to time is paste the output of a vim command into the buffer. E.g. when I'm editing my vimrc, it'd be nice to be able to fiddle with statusline and then be able to just do something akin to
"=set statusline?<Enter>p
Problem is, that yields
E121: Undefined variable: set
E15: Invalid expression: set statusline?
Press ENTER or type command to continue
I figure that this is possible, and that I just don't know enough about the builtin functions and how to use them (I see expand used here and there, but have never successfully made it work for me in any context), even though I (think that I) have a pretty solid understanding of normal mode.
Note that this specific example is a little contrived, but I can't think of a better one right now. For the specific use case above, I could just ":p to get the whole set command that I used during experimentation and then edit to suit, but fairly regularly I run into other cases where I want vim to tell me something and then I want to paste that output somewhere so that I can continue to look at it while continuing with my work.
You can paste an option setting:
"=&statusline<Enter>p
I don't know of any way to put the output of an arbitrary command in the buffer, however.
The values of settings are stored in variables that are prepended with an & symbol. So the value that statusline is set to can be accessed by referencing &statusline. To insert into a document one way is to use the "expression" register, <ctrl-R>=. To use it enter insert mode and press <ctrl-R> and then =. You will see an equals sign in the command line, where you can enter: &statusline and then press enter. This will insert the value into the buffer.