66
votes
Accepted
How to run bash command over current file and replace buffer with result?
:!{cmd} sends {cmd} to the shell which executes it and shows its output on the screen.
:{range}!{filter} sends the lines from the current buffer inside {range} as the input of the {filter} program ...
51
votes
Accepted
Execute current buffer as bash script from vim
I wrote a pretty extensive answer about this over on stack-overflow. The basic idea is that since the write command is about writing not saving, you can write the text in your buffer into an external ...
44
votes
Accepted
Extract the result of a shell command to a variable in a .vimrc file?
You can do this with the system function:
let language = system('echo $LANG')
Bonus point: if your output is a list, you can use the systemlist instead to get back a list. e.g.
let files = ...
33
votes
How to go to the next grep result?
:grep/:vimgrep both use the quickfix list to store locations. As you have noticed vim will automatically jump to the first occurrence (use ! to avoid this behavior). Simply use :cnext to go to the ...
21
votes
How to run bash command over current file and replace buffer with result?
By sheer luck I found out that both:
:%!jq . %
or shorter:
:%!jq .
work, yet I don't understand why this updates the buffer.
18
votes
How do I insert the output of an external command at the cursor position?
In normal mode press double !, then enter the command, or just type the following:
:.!ls
17
votes
Replace selection with output of external command
You could delete the text and insert the output of a command using CtrlR-=:
CTRL-R {0-9a-z"%#*+:.-=} i_CTRL-R
Insert the contents of a register. ...
17
votes
Extract the result of a shell command to a variable in a .vimrc file?
If LANG is an environment variable you can just do:
let language = $LANG
Or, even simpler:
if $LANG == 'en'
…
endif
17
votes
Accepted
Use Vim with virtualenv
I actually fixed this myself a long time ago. The problem is that when you run !python you're starting up a new shell with your existing environment. But, this means workon xxxx wasn't called to ...
16
votes
Accepted
How do I get Vim to compile my .tex file when executing :w?
Don't be so inefficient! Your time is valuable. Use the vimtex plugin offered by @lervag: see here. You can control all of the plugins you install by using Vundle or any similar plugin manager (vim-...
16
votes
Accepted
get exit status from system() call
Mentioned in the comments above, the v:shell_error variable gives the result of the last shell command. So the exit status can be tested as follows:
let output = system("my_shell_command")
...
11
votes
Accepted
External command on unsaved buffer
You can use the "filter" command,:!, which like most commands can be prefixed with a range:
:{range}!cmd
If you prefix this with a range, it will pipe those lines to the command's stdin, and replace ...
11
votes
Accepted
How to avoid to press ENTER twice, when compiling with :make inside Vim?
Vim displays the "Press ENTER" prompt when the number of lines printed in the command area is greater than the number of lines it has available. This is to ensure the user doesn't miss a message.
The ...
11
votes
Accepted
How do I search google from Vim?
You have a couple of options here:
Using a plugin:
vim-g
gsearch
vim-quicklink
Or, if you prefer a lightweight solution, you can try the following:
function! GoogleSearch()
let searchterm = ...
10
votes
Can I issue an interactive shell in Vim window?
Just use :vertical terminal and then just map it to a key or something.
Also, I have this in my vimrc: map <Leader>t :set mouse=a<CR>:vertical terminal<CR> That enables me to switch ...
10
votes
How can I import output from linux commands into vim for editing?
You could use :read (see :h :read):
:[range]r[ead] [++opt] !{cmd}
Execute {cmd} and insert its standard output below
the cursor or the specified line. A temporary file is
For ...
9
votes
How to execute shell commands silently?
If you don't care about exit code, you can go with this:
:call system('/usr/bin/zathura using-docker.pdf &')
9
votes
How to execute shell commands silently?
Running a command in the background
I was running a command that blocked for a bit, and I didn't really care about the output. This can be taken care of by starting the process attached not to the ...
9
votes
How can I integrate gdb with Vim?
Vim 8.1 adds debugger integration, see :help terminal-debug. The summary is that you run
:packadd termdebug
:Termdebug myprogram
And then you can set breakpads either in the gdb buffer using b myfunc,...
9
votes
Is it possible to have the output of a :! command in a split rather than the whole window?
You can use this command :
:new | 0read ! command
This will open up a new split and puts the output of the command in this split (though not in the currently active split as you mentioned).
new - ...
9
votes
How to use backtick-expansion to populate the arglist?
I still don't know how to use backtick-expansion to populate the arglist with an arbitrary shell command, however I've found a workaround.
From :help `=:
You can have the backticks expanded as a ...
9
votes
Accepted
Use shell variable substitution with current filepath
Why doesn't this work?
This doesn't work for exactly why you say, vim is replacing the % with the path.
So, your non working line
:! mv % ${%/.txt/.asc}
is expanding to
:! mv myfile.txt ${myfile....
9
votes
Why do exclamation ! shell commands in GVIM show error: "syntax error near unexpected token (" for "ls() ("?
Instead of adding -i to shellcmdflags and reading your entire .bashrc just for loading aliases, keep your aliases in a separate file, like ~/.bash_aliases for Ubuntu, and add to your vimrc:
let $...
9
votes
Prevent "Press ENTER or type command to continue" prompt after external command executes
If what you're looking for is a way to automate the sequence of commands (:silent grep and redraw!), then consider creating a new user-defined command for that operation.
(In fact, Vim documentation ...
9
votes
Accepted
job_start() command only works with seemingly random job options
I think I figured this out myself. I believe the problem was that my job was being deleted before the zip command finished. I guess Vim deletes a job object if there are no references to it. So the ...
8
votes
How to execute shell commands silently?
Vim 8 introduced jobs support. One can run external command in the background without relying on plugins. For example, to run a markdown server (markserv) at current location and not block vim session:...
8
votes
How can I integrate gdb with Vim?
GDB edit command
Opens an editor on the current line using the command:
$EDITOR +<current-line> <current-file>
The default editor is ex, but vim also understands the +<current-line&...
8
votes
Accepted
How do I know i am in a shell from vi command :sh?
In a regular shell:
$ env | grep -i vim
EDITOR=vim
After using :shell
$ env | grep -i vim
EDITOR=vim
VIMRUNTIME=/usr/share/vim/vim80
VIM=/usr/share/vim
MYVIMRC=/home/martin/.vim/vimrc
So it looks ...
8
votes
How to get line numbers of selected text
The line numbers of the selected text are available in the marks < and >. So you can use e.g. echo getpos("'<") and echo getpos("'>") to get the start and end of the selected text.
...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
external-command × 231command-line × 36
vimscript × 25
shell × 15
terminal × 14
key-bindings × 13
vimrc × 12
bash × 12
neovim × 10
microsoft-windows × 10
ex-mode × 8
autocmd × 7
buffers × 6
filetype-tex × 6
register × 6
search × 5
gvim × 5
functions × 5
linux × 5
filter × 5
visual-mode × 4
save × 4
filesystem × 4
indentation × 3
vim-windows × 3