How to insert the result of a command into the text in vim?
Clash Royale CLAN TAG#URR8PPP
For instance, :echo strftime(%c)
will show the current time on the bottom, but how to insert this time string to the text (right after the cursor)?
vim
add a comment |
For instance, :echo strftime(%c)
will show the current time on the bottom, but how to insert this time string to the text (right after the cursor)?
vim
1
I believe the OP mean "How to insert the result of a Vim expression into the text?"
– Alastair Irvine
Jan 19 '15 at 10:19
It's also worth mentioning that this is not the same question as Writing a vim function to insert a block of static text, however some of the answers still apply.
– Alastair Irvine
Jan 19 '15 at 10:27
Your example is literally my use case and I salute you for it.
– Joe
Feb 5 at 16:25
add a comment |
For instance, :echo strftime(%c)
will show the current time on the bottom, but how to insert this time string to the text (right after the cursor)?
vim
For instance, :echo strftime(%c)
will show the current time on the bottom, but how to insert this time string to the text (right after the cursor)?
vim
vim
edited Feb 25 '11 at 23:24
Gilles
545k12911071623
545k12911071623
asked Feb 25 '11 at 4:33
xzhuxzhu
394148
394148
1
I believe the OP mean "How to insert the result of a Vim expression into the text?"
– Alastair Irvine
Jan 19 '15 at 10:19
It's also worth mentioning that this is not the same question as Writing a vim function to insert a block of static text, however some of the answers still apply.
– Alastair Irvine
Jan 19 '15 at 10:27
Your example is literally my use case and I salute you for it.
– Joe
Feb 5 at 16:25
add a comment |
1
I believe the OP mean "How to insert the result of a Vim expression into the text?"
– Alastair Irvine
Jan 19 '15 at 10:19
It's also worth mentioning that this is not the same question as Writing a vim function to insert a block of static text, however some of the answers still apply.
– Alastair Irvine
Jan 19 '15 at 10:27
Your example is literally my use case and I salute you for it.
– Joe
Feb 5 at 16:25
1
1
I believe the OP mean "How to insert the result of a Vim expression into the text?"
– Alastair Irvine
Jan 19 '15 at 10:19
I believe the OP mean "How to insert the result of a Vim expression into the text?"
– Alastair Irvine
Jan 19 '15 at 10:19
It's also worth mentioning that this is not the same question as Writing a vim function to insert a block of static text, however some of the answers still apply.
– Alastair Irvine
Jan 19 '15 at 10:27
It's also worth mentioning that this is not the same question as Writing a vim function to insert a block of static text, however some of the answers still apply.
– Alastair Irvine
Jan 19 '15 at 10:27
Your example is literally my use case and I salute you for it.
– Joe
Feb 5 at 16:25
Your example is literally my use case and I salute you for it.
– Joe
Feb 5 at 16:25
add a comment |
7 Answers
7
active
oldest
votes
You can use the expression register, "=
, with p
(or P
) in normal mode or <C-R>
in insert mode:
In normal mode:
(<C-M>
here means Control+M, or just press Enter/Return)
"=strftime('%c')<C-M>p
In insert mode:
(<C-M>
has the same meaning as above, <C-R>
means Control+R)
<C-R>=strftime('%c')<C-M>
If you want to insert the result of the same expression many times, then you might want to map them onto keys in your .vimrc
:
(here the <C-M>
and <C-R>
should be typed literally (a sequence of five printable characters—Vim will translate them internally))
:nmap <F2> "=strftime('%c')<C-M>p
:imap <F2> <C-R>=strftime('%c')<C-M>
2
+1 Off course! The"=
register. :-/
– Eelvex
Feb 25 '11 at 7:42
2
to get the value of a vim variable (for example, sessionoptions):<C-R>=&sessionoptions
-- it even does wildmode tab-completion!
– Justin M. Keyes
Oct 25 '12 at 18:22
2
:put =strftime('%c')<C-M>
– brunch875
Aug 13 '15 at 19:23
add a comment |
:r!date +%c
see :help :r!
1
date
is an external command and!
calls external commands while OP asks for vim commands.
– Eelvex
Feb 25 '11 at 8:51
@eelvex no he didn't. and the ! is a vim, and vi, command. This is the canonical method. Works for many other things as well.
– Keith
Feb 25 '11 at 11:29
5
@Keith: yes!
is a vi(m) command that calls external commands. You may be right OP not wanting to output only vim commands but if (s)he does,!
will not do.
– Eelvex
Feb 25 '11 at 17:19
add a comment |
These commands will insert the output of strftime("%c")
right where your cursor is:
:exe ":normal i" . strftime("%c")
and
:call feedkeys("i". strftime("%c"))
There are other ways to do what you want (like, for example, those on Mikel's answer).
Edit: Even better, for in-place insert, use the =
register as Chris Johnsen describes
add a comment |
If you want to insert the output of a vim command (as opposed to the return value of a function call), you have to capture it. This is accomplished via the :redir
command, which allows you to redirect vim's equivalent of standard output into a variable, file, register, or other target.
:redir
is sort of painfully inconvenient to use; I would write a function to encapsulate its functionality in a more convenient way, something like
funct! Exec(command)
redir =>output
silent exec a:command
redir END
return output
endfunct!
Once you've declared such a function, you can use the expression register (as explained by Chris Johnsen) to insert the output of a command at the cursor position. So, from normal mode, hit i^R=Exec('ls')
to insert the list of vim's current buffers.
Be aware that the command will execute in the function namespace, so if you use a global variable you will have to explicitly namespace it by prefixing it with g:
. Also note that Exec()
, as written above, will append a terminating newline to even one-line output. You might want to add a call to substitute()
into the function to avoid this.
Also see https://stackoverflow.com/questions/2573021/vim-how-to-redirect-ex-command-output-into-current-buffer-or-file/2573054#2573054 for more blathering on about redir
and a link to a related command.
2
This works great. I added aset paste
command before returning the output and aset nopaste
after, to avoid the staircase indent when the lines start with blanks. Actually, I wanted to save the value of the current paste option and to return it but I was unable to do it so.
– Juan Lanus
Jan 5 '15 at 21:17
1
@JuanLanus Theset nopaste
shouldn't work afterreturn output
, because the return statement is an exit point out of the function. I've put my solution to this problem as a separate answer on this page.
– Evgeni Sergeev
Apr 23 '15 at 4:57
add a comment |
:call append(line('.'), strftime("%c"))
Will put it on the next line, then you could press J
(Shift+J)to join it up to the current position.
Or if you need it all in one command, you could do
:call setline(line('.'), getline(line('.')) . strftime("%c"))
or
:call setline(line('.'), getline(line('.')) . " " . strftime("%c"))
depending on whether you want a space inserted before the date or not.
add a comment |
Improving @intuited answer to avoid the problem with leading whitespace and growing indent:
"Examples:
":call Exec('buffers')
"This will include the output of :buffers into the current buffer.
"
"Also try:
":call Exec('ls')
":call Exec('autocmd')
"
funct! Exec(command)
redir =>output
silent exec a:command
redir END
let @o = output
execute "put o"
return ''
endfunct!
This will simply insert at the current location in the file when you :call Exec('command')
from normal mode. As noted in the comment, the original (insert-mode) Ctrl+R =Exec('command')
approach with Exec(..)
returning a string could be partially corrected by using set paste
, but doesn't offer an opportunity to put the set nopaste
anywhere.
The let @o = output
syntax sets the register o
to the contents of the variable output
, as explained here: https://stackoverflow.com/a/22738310/1143274
The return ''
line is so that the default return value of 0
doesn't get inserted to the buffer.
add a comment |
This is how I do it. It puts it right after the cursor because it uses p
.
" save previous yank
let reg_save = @@
" save your text to the '@' register
let @@ = strftime('%c')
" paste it after the cursor
exec "normal! p"
" restore previous yank
let @@ = reg_save
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f8101%2fhow-to-insert-the-result-of-a-command-into-the-text-in-vim%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
7 Answers
7
active
oldest
votes
7 Answers
7
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can use the expression register, "=
, with p
(or P
) in normal mode or <C-R>
in insert mode:
In normal mode:
(<C-M>
here means Control+M, or just press Enter/Return)
"=strftime('%c')<C-M>p
In insert mode:
(<C-M>
has the same meaning as above, <C-R>
means Control+R)
<C-R>=strftime('%c')<C-M>
If you want to insert the result of the same expression many times, then you might want to map them onto keys in your .vimrc
:
(here the <C-M>
and <C-R>
should be typed literally (a sequence of five printable characters—Vim will translate them internally))
:nmap <F2> "=strftime('%c')<C-M>p
:imap <F2> <C-R>=strftime('%c')<C-M>
2
+1 Off course! The"=
register. :-/
– Eelvex
Feb 25 '11 at 7:42
2
to get the value of a vim variable (for example, sessionoptions):<C-R>=&sessionoptions
-- it even does wildmode tab-completion!
– Justin M. Keyes
Oct 25 '12 at 18:22
2
:put =strftime('%c')<C-M>
– brunch875
Aug 13 '15 at 19:23
add a comment |
You can use the expression register, "=
, with p
(or P
) in normal mode or <C-R>
in insert mode:
In normal mode:
(<C-M>
here means Control+M, or just press Enter/Return)
"=strftime('%c')<C-M>p
In insert mode:
(<C-M>
has the same meaning as above, <C-R>
means Control+R)
<C-R>=strftime('%c')<C-M>
If you want to insert the result of the same expression many times, then you might want to map them onto keys in your .vimrc
:
(here the <C-M>
and <C-R>
should be typed literally (a sequence of five printable characters—Vim will translate them internally))
:nmap <F2> "=strftime('%c')<C-M>p
:imap <F2> <C-R>=strftime('%c')<C-M>
2
+1 Off course! The"=
register. :-/
– Eelvex
Feb 25 '11 at 7:42
2
to get the value of a vim variable (for example, sessionoptions):<C-R>=&sessionoptions
-- it even does wildmode tab-completion!
– Justin M. Keyes
Oct 25 '12 at 18:22
2
:put =strftime('%c')<C-M>
– brunch875
Aug 13 '15 at 19:23
add a comment |
You can use the expression register, "=
, with p
(or P
) in normal mode or <C-R>
in insert mode:
In normal mode:
(<C-M>
here means Control+M, or just press Enter/Return)
"=strftime('%c')<C-M>p
In insert mode:
(<C-M>
has the same meaning as above, <C-R>
means Control+R)
<C-R>=strftime('%c')<C-M>
If you want to insert the result of the same expression many times, then you might want to map them onto keys in your .vimrc
:
(here the <C-M>
and <C-R>
should be typed literally (a sequence of five printable characters—Vim will translate them internally))
:nmap <F2> "=strftime('%c')<C-M>p
:imap <F2> <C-R>=strftime('%c')<C-M>
You can use the expression register, "=
, with p
(or P
) in normal mode or <C-R>
in insert mode:
In normal mode:
(<C-M>
here means Control+M, or just press Enter/Return)
"=strftime('%c')<C-M>p
In insert mode:
(<C-M>
has the same meaning as above, <C-R>
means Control+R)
<C-R>=strftime('%c')<C-M>
If you want to insert the result of the same expression many times, then you might want to map them onto keys in your .vimrc
:
(here the <C-M>
and <C-R>
should be typed literally (a sequence of five printable characters—Vim will translate them internally))
:nmap <F2> "=strftime('%c')<C-M>p
:imap <F2> <C-R>=strftime('%c')<C-M>
answered Feb 25 '11 at 7:11
Chris JohnsenChris Johnsen
15k65048
15k65048
2
+1 Off course! The"=
register. :-/
– Eelvex
Feb 25 '11 at 7:42
2
to get the value of a vim variable (for example, sessionoptions):<C-R>=&sessionoptions
-- it even does wildmode tab-completion!
– Justin M. Keyes
Oct 25 '12 at 18:22
2
:put =strftime('%c')<C-M>
– brunch875
Aug 13 '15 at 19:23
add a comment |
2
+1 Off course! The"=
register. :-/
– Eelvex
Feb 25 '11 at 7:42
2
to get the value of a vim variable (for example, sessionoptions):<C-R>=&sessionoptions
-- it even does wildmode tab-completion!
– Justin M. Keyes
Oct 25 '12 at 18:22
2
:put =strftime('%c')<C-M>
– brunch875
Aug 13 '15 at 19:23
2
2
+1 Off course! The
"=
register. :-/– Eelvex
Feb 25 '11 at 7:42
+1 Off course! The
"=
register. :-/– Eelvex
Feb 25 '11 at 7:42
2
2
to get the value of a vim variable (for example, sessionoptions):
<C-R>=&sessionoptions
-- it even does wildmode tab-completion!– Justin M. Keyes
Oct 25 '12 at 18:22
to get the value of a vim variable (for example, sessionoptions):
<C-R>=&sessionoptions
-- it even does wildmode tab-completion!– Justin M. Keyes
Oct 25 '12 at 18:22
2
2
:put =strftime('%c')<C-M>
– brunch875
Aug 13 '15 at 19:23
:put =strftime('%c')<C-M>
– brunch875
Aug 13 '15 at 19:23
add a comment |
:r!date +%c
see :help :r!
1
date
is an external command and!
calls external commands while OP asks for vim commands.
– Eelvex
Feb 25 '11 at 8:51
@eelvex no he didn't. and the ! is a vim, and vi, command. This is the canonical method. Works for many other things as well.
– Keith
Feb 25 '11 at 11:29
5
@Keith: yes!
is a vi(m) command that calls external commands. You may be right OP not wanting to output only vim commands but if (s)he does,!
will not do.
– Eelvex
Feb 25 '11 at 17:19
add a comment |
:r!date +%c
see :help :r!
1
date
is an external command and!
calls external commands while OP asks for vim commands.
– Eelvex
Feb 25 '11 at 8:51
@eelvex no he didn't. and the ! is a vim, and vi, command. This is the canonical method. Works for many other things as well.
– Keith
Feb 25 '11 at 11:29
5
@Keith: yes!
is a vi(m) command that calls external commands. You may be right OP not wanting to output only vim commands but if (s)he does,!
will not do.
– Eelvex
Feb 25 '11 at 17:19
add a comment |
:r!date +%c
see :help :r!
:r!date +%c
see :help :r!
answered Feb 25 '11 at 5:33
glenn jackmanglenn jackman
53k573114
53k573114
1
date
is an external command and!
calls external commands while OP asks for vim commands.
– Eelvex
Feb 25 '11 at 8:51
@eelvex no he didn't. and the ! is a vim, and vi, command. This is the canonical method. Works for many other things as well.
– Keith
Feb 25 '11 at 11:29
5
@Keith: yes!
is a vi(m) command that calls external commands. You may be right OP not wanting to output only vim commands but if (s)he does,!
will not do.
– Eelvex
Feb 25 '11 at 17:19
add a comment |
1
date
is an external command and!
calls external commands while OP asks for vim commands.
– Eelvex
Feb 25 '11 at 8:51
@eelvex no he didn't. and the ! is a vim, and vi, command. This is the canonical method. Works for many other things as well.
– Keith
Feb 25 '11 at 11:29
5
@Keith: yes!
is a vi(m) command that calls external commands. You may be right OP not wanting to output only vim commands but if (s)he does,!
will not do.
– Eelvex
Feb 25 '11 at 17:19
1
1
date
is an external command and !
calls external commands while OP asks for vim commands.– Eelvex
Feb 25 '11 at 8:51
date
is an external command and !
calls external commands while OP asks for vim commands.– Eelvex
Feb 25 '11 at 8:51
@eelvex no he didn't. and the ! is a vim, and vi, command. This is the canonical method. Works for many other things as well.
– Keith
Feb 25 '11 at 11:29
@eelvex no he didn't. and the ! is a vim, and vi, command. This is the canonical method. Works for many other things as well.
– Keith
Feb 25 '11 at 11:29
5
5
@Keith: yes
!
is a vi(m) command that calls external commands. You may be right OP not wanting to output only vim commands but if (s)he does, !
will not do.– Eelvex
Feb 25 '11 at 17:19
@Keith: yes
!
is a vi(m) command that calls external commands. You may be right OP not wanting to output only vim commands but if (s)he does, !
will not do.– Eelvex
Feb 25 '11 at 17:19
add a comment |
These commands will insert the output of strftime("%c")
right where your cursor is:
:exe ":normal i" . strftime("%c")
and
:call feedkeys("i". strftime("%c"))
There are other ways to do what you want (like, for example, those on Mikel's answer).
Edit: Even better, for in-place insert, use the =
register as Chris Johnsen describes
add a comment |
These commands will insert the output of strftime("%c")
right where your cursor is:
:exe ":normal i" . strftime("%c")
and
:call feedkeys("i". strftime("%c"))
There are other ways to do what you want (like, for example, those on Mikel's answer).
Edit: Even better, for in-place insert, use the =
register as Chris Johnsen describes
add a comment |
These commands will insert the output of strftime("%c")
right where your cursor is:
:exe ":normal i" . strftime("%c")
and
:call feedkeys("i". strftime("%c"))
There are other ways to do what you want (like, for example, those on Mikel's answer).
Edit: Even better, for in-place insert, use the =
register as Chris Johnsen describes
These commands will insert the output of strftime("%c")
right where your cursor is:
:exe ":normal i" . strftime("%c")
and
:call feedkeys("i". strftime("%c"))
There are other ways to do what you want (like, for example, those on Mikel's answer).
Edit: Even better, for in-place insert, use the =
register as Chris Johnsen describes
edited Apr 13 '17 at 12:37
Community♦
1
1
answered Feb 25 '11 at 7:09
EelvexEelvex
536313
536313
add a comment |
add a comment |
If you want to insert the output of a vim command (as opposed to the return value of a function call), you have to capture it. This is accomplished via the :redir
command, which allows you to redirect vim's equivalent of standard output into a variable, file, register, or other target.
:redir
is sort of painfully inconvenient to use; I would write a function to encapsulate its functionality in a more convenient way, something like
funct! Exec(command)
redir =>output
silent exec a:command
redir END
return output
endfunct!
Once you've declared such a function, you can use the expression register (as explained by Chris Johnsen) to insert the output of a command at the cursor position. So, from normal mode, hit i^R=Exec('ls')
to insert the list of vim's current buffers.
Be aware that the command will execute in the function namespace, so if you use a global variable you will have to explicitly namespace it by prefixing it with g:
. Also note that Exec()
, as written above, will append a terminating newline to even one-line output. You might want to add a call to substitute()
into the function to avoid this.
Also see https://stackoverflow.com/questions/2573021/vim-how-to-redirect-ex-command-output-into-current-buffer-or-file/2573054#2573054 for more blathering on about redir
and a link to a related command.
2
This works great. I added aset paste
command before returning the output and aset nopaste
after, to avoid the staircase indent when the lines start with blanks. Actually, I wanted to save the value of the current paste option and to return it but I was unable to do it so.
– Juan Lanus
Jan 5 '15 at 21:17
1
@JuanLanus Theset nopaste
shouldn't work afterreturn output
, because the return statement is an exit point out of the function. I've put my solution to this problem as a separate answer on this page.
– Evgeni Sergeev
Apr 23 '15 at 4:57
add a comment |
If you want to insert the output of a vim command (as opposed to the return value of a function call), you have to capture it. This is accomplished via the :redir
command, which allows you to redirect vim's equivalent of standard output into a variable, file, register, or other target.
:redir
is sort of painfully inconvenient to use; I would write a function to encapsulate its functionality in a more convenient way, something like
funct! Exec(command)
redir =>output
silent exec a:command
redir END
return output
endfunct!
Once you've declared such a function, you can use the expression register (as explained by Chris Johnsen) to insert the output of a command at the cursor position. So, from normal mode, hit i^R=Exec('ls')
to insert the list of vim's current buffers.
Be aware that the command will execute in the function namespace, so if you use a global variable you will have to explicitly namespace it by prefixing it with g:
. Also note that Exec()
, as written above, will append a terminating newline to even one-line output. You might want to add a call to substitute()
into the function to avoid this.
Also see https://stackoverflow.com/questions/2573021/vim-how-to-redirect-ex-command-output-into-current-buffer-or-file/2573054#2573054 for more blathering on about redir
and a link to a related command.
2
This works great. I added aset paste
command before returning the output and aset nopaste
after, to avoid the staircase indent when the lines start with blanks. Actually, I wanted to save the value of the current paste option and to return it but I was unable to do it so.
– Juan Lanus
Jan 5 '15 at 21:17
1
@JuanLanus Theset nopaste
shouldn't work afterreturn output
, because the return statement is an exit point out of the function. I've put my solution to this problem as a separate answer on this page.
– Evgeni Sergeev
Apr 23 '15 at 4:57
add a comment |
If you want to insert the output of a vim command (as opposed to the return value of a function call), you have to capture it. This is accomplished via the :redir
command, which allows you to redirect vim's equivalent of standard output into a variable, file, register, or other target.
:redir
is sort of painfully inconvenient to use; I would write a function to encapsulate its functionality in a more convenient way, something like
funct! Exec(command)
redir =>output
silent exec a:command
redir END
return output
endfunct!
Once you've declared such a function, you can use the expression register (as explained by Chris Johnsen) to insert the output of a command at the cursor position. So, from normal mode, hit i^R=Exec('ls')
to insert the list of vim's current buffers.
Be aware that the command will execute in the function namespace, so if you use a global variable you will have to explicitly namespace it by prefixing it with g:
. Also note that Exec()
, as written above, will append a terminating newline to even one-line output. You might want to add a call to substitute()
into the function to avoid this.
Also see https://stackoverflow.com/questions/2573021/vim-how-to-redirect-ex-command-output-into-current-buffer-or-file/2573054#2573054 for more blathering on about redir
and a link to a related command.
If you want to insert the output of a vim command (as opposed to the return value of a function call), you have to capture it. This is accomplished via the :redir
command, which allows you to redirect vim's equivalent of standard output into a variable, file, register, or other target.
:redir
is sort of painfully inconvenient to use; I would write a function to encapsulate its functionality in a more convenient way, something like
funct! Exec(command)
redir =>output
silent exec a:command
redir END
return output
endfunct!
Once you've declared such a function, you can use the expression register (as explained by Chris Johnsen) to insert the output of a command at the cursor position. So, from normal mode, hit i^R=Exec('ls')
to insert the list of vim's current buffers.
Be aware that the command will execute in the function namespace, so if you use a global variable you will have to explicitly namespace it by prefixing it with g:
. Also note that Exec()
, as written above, will append a terminating newline to even one-line output. You might want to add a call to substitute()
into the function to avoid this.
Also see https://stackoverflow.com/questions/2573021/vim-how-to-redirect-ex-command-output-into-current-buffer-or-file/2573054#2573054 for more blathering on about redir
and a link to a related command.
edited May 23 '17 at 12:40
Community♦
1
1
answered Feb 27 '11 at 17:22
intuitedintuited
1,90331934
1,90331934
2
This works great. I added aset paste
command before returning the output and aset nopaste
after, to avoid the staircase indent when the lines start with blanks. Actually, I wanted to save the value of the current paste option and to return it but I was unable to do it so.
– Juan Lanus
Jan 5 '15 at 21:17
1
@JuanLanus Theset nopaste
shouldn't work afterreturn output
, because the return statement is an exit point out of the function. I've put my solution to this problem as a separate answer on this page.
– Evgeni Sergeev
Apr 23 '15 at 4:57
add a comment |
2
This works great. I added aset paste
command before returning the output and aset nopaste
after, to avoid the staircase indent when the lines start with blanks. Actually, I wanted to save the value of the current paste option and to return it but I was unable to do it so.
– Juan Lanus
Jan 5 '15 at 21:17
1
@JuanLanus Theset nopaste
shouldn't work afterreturn output
, because the return statement is an exit point out of the function. I've put my solution to this problem as a separate answer on this page.
– Evgeni Sergeev
Apr 23 '15 at 4:57
2
2
This works great. I added a
set paste
command before returning the output and a set nopaste
after, to avoid the staircase indent when the lines start with blanks. Actually, I wanted to save the value of the current paste option and to return it but I was unable to do it so.– Juan Lanus
Jan 5 '15 at 21:17
This works great. I added a
set paste
command before returning the output and a set nopaste
after, to avoid the staircase indent when the lines start with blanks. Actually, I wanted to save the value of the current paste option and to return it but I was unable to do it so.– Juan Lanus
Jan 5 '15 at 21:17
1
1
@JuanLanus The
set nopaste
shouldn't work after return output
, because the return statement is an exit point out of the function. I've put my solution to this problem as a separate answer on this page.– Evgeni Sergeev
Apr 23 '15 at 4:57
@JuanLanus The
set nopaste
shouldn't work after return output
, because the return statement is an exit point out of the function. I've put my solution to this problem as a separate answer on this page.– Evgeni Sergeev
Apr 23 '15 at 4:57
add a comment |
:call append(line('.'), strftime("%c"))
Will put it on the next line, then you could press J
(Shift+J)to join it up to the current position.
Or if you need it all in one command, you could do
:call setline(line('.'), getline(line('.')) . strftime("%c"))
or
:call setline(line('.'), getline(line('.')) . " " . strftime("%c"))
depending on whether you want a space inserted before the date or not.
add a comment |
:call append(line('.'), strftime("%c"))
Will put it on the next line, then you could press J
(Shift+J)to join it up to the current position.
Or if you need it all in one command, you could do
:call setline(line('.'), getline(line('.')) . strftime("%c"))
or
:call setline(line('.'), getline(line('.')) . " " . strftime("%c"))
depending on whether you want a space inserted before the date or not.
add a comment |
:call append(line('.'), strftime("%c"))
Will put it on the next line, then you could press J
(Shift+J)to join it up to the current position.
Or if you need it all in one command, you could do
:call setline(line('.'), getline(line('.')) . strftime("%c"))
or
:call setline(line('.'), getline(line('.')) . " " . strftime("%c"))
depending on whether you want a space inserted before the date or not.
:call append(line('.'), strftime("%c"))
Will put it on the next line, then you could press J
(Shift+J)to join it up to the current position.
Or if you need it all in one command, you could do
:call setline(line('.'), getline(line('.')) . strftime("%c"))
or
:call setline(line('.'), getline(line('.')) . " " . strftime("%c"))
depending on whether you want a space inserted before the date or not.
edited Feb 25 '11 at 4:43
answered Feb 25 '11 at 4:37
MikelMikel
40.1k10103128
40.1k10103128
add a comment |
add a comment |
Improving @intuited answer to avoid the problem with leading whitespace and growing indent:
"Examples:
":call Exec('buffers')
"This will include the output of :buffers into the current buffer.
"
"Also try:
":call Exec('ls')
":call Exec('autocmd')
"
funct! Exec(command)
redir =>output
silent exec a:command
redir END
let @o = output
execute "put o"
return ''
endfunct!
This will simply insert at the current location in the file when you :call Exec('command')
from normal mode. As noted in the comment, the original (insert-mode) Ctrl+R =Exec('command')
approach with Exec(..)
returning a string could be partially corrected by using set paste
, but doesn't offer an opportunity to put the set nopaste
anywhere.
The let @o = output
syntax sets the register o
to the contents of the variable output
, as explained here: https://stackoverflow.com/a/22738310/1143274
The return ''
line is so that the default return value of 0
doesn't get inserted to the buffer.
add a comment |
Improving @intuited answer to avoid the problem with leading whitespace and growing indent:
"Examples:
":call Exec('buffers')
"This will include the output of :buffers into the current buffer.
"
"Also try:
":call Exec('ls')
":call Exec('autocmd')
"
funct! Exec(command)
redir =>output
silent exec a:command
redir END
let @o = output
execute "put o"
return ''
endfunct!
This will simply insert at the current location in the file when you :call Exec('command')
from normal mode. As noted in the comment, the original (insert-mode) Ctrl+R =Exec('command')
approach with Exec(..)
returning a string could be partially corrected by using set paste
, but doesn't offer an opportunity to put the set nopaste
anywhere.
The let @o = output
syntax sets the register o
to the contents of the variable output
, as explained here: https://stackoverflow.com/a/22738310/1143274
The return ''
line is so that the default return value of 0
doesn't get inserted to the buffer.
add a comment |
Improving @intuited answer to avoid the problem with leading whitespace and growing indent:
"Examples:
":call Exec('buffers')
"This will include the output of :buffers into the current buffer.
"
"Also try:
":call Exec('ls')
":call Exec('autocmd')
"
funct! Exec(command)
redir =>output
silent exec a:command
redir END
let @o = output
execute "put o"
return ''
endfunct!
This will simply insert at the current location in the file when you :call Exec('command')
from normal mode. As noted in the comment, the original (insert-mode) Ctrl+R =Exec('command')
approach with Exec(..)
returning a string could be partially corrected by using set paste
, but doesn't offer an opportunity to put the set nopaste
anywhere.
The let @o = output
syntax sets the register o
to the contents of the variable output
, as explained here: https://stackoverflow.com/a/22738310/1143274
The return ''
line is so that the default return value of 0
doesn't get inserted to the buffer.
Improving @intuited answer to avoid the problem with leading whitespace and growing indent:
"Examples:
":call Exec('buffers')
"This will include the output of :buffers into the current buffer.
"
"Also try:
":call Exec('ls')
":call Exec('autocmd')
"
funct! Exec(command)
redir =>output
silent exec a:command
redir END
let @o = output
execute "put o"
return ''
endfunct!
This will simply insert at the current location in the file when you :call Exec('command')
from normal mode. As noted in the comment, the original (insert-mode) Ctrl+R =Exec('command')
approach with Exec(..)
returning a string could be partially corrected by using set paste
, but doesn't offer an opportunity to put the set nopaste
anywhere.
The let @o = output
syntax sets the register o
to the contents of the variable output
, as explained here: https://stackoverflow.com/a/22738310/1143274
The return ''
line is so that the default return value of 0
doesn't get inserted to the buffer.
edited Mar 6 at 14:08
ruohola
1034
1034
answered Apr 23 '15 at 4:55
Evgeni SergeevEvgeni Sergeev
936179
936179
add a comment |
add a comment |
This is how I do it. It puts it right after the cursor because it uses p
.
" save previous yank
let reg_save = @@
" save your text to the '@' register
let @@ = strftime('%c')
" paste it after the cursor
exec "normal! p"
" restore previous yank
let @@ = reg_save
add a comment |
This is how I do it. It puts it right after the cursor because it uses p
.
" save previous yank
let reg_save = @@
" save your text to the '@' register
let @@ = strftime('%c')
" paste it after the cursor
exec "normal! p"
" restore previous yank
let @@ = reg_save
add a comment |
This is how I do it. It puts it right after the cursor because it uses p
.
" save previous yank
let reg_save = @@
" save your text to the '@' register
let @@ = strftime('%c')
" paste it after the cursor
exec "normal! p"
" restore previous yank
let @@ = reg_save
This is how I do it. It puts it right after the cursor because it uses p
.
" save previous yank
let reg_save = @@
" save your text to the '@' register
let @@ = strftime('%c')
" paste it after the cursor
exec "normal! p"
" restore previous yank
let @@ = reg_save
answered May 20 '18 at 8:22
Juan IbiapinaJuan Ibiapina
111
111
add a comment |
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f8101%2fhow-to-insert-the-result-of-a-command-into-the-text-in-vim%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
I believe the OP mean "How to insert the result of a Vim expression into the text?"
– Alastair Irvine
Jan 19 '15 at 10:19
It's also worth mentioning that this is not the same question as Writing a vim function to insert a block of static text, however some of the answers still apply.
– Alastair Irvine
Jan 19 '15 at 10:27
Your example is literally my use case and I salute you for it.
– Joe
Feb 5 at 16:25