Skip to main content
Share Your Experience: Take the 2024 Developer Survey
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 ...
DJMcMayhem's user avatar
  • 17.7k
45 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 = ...
nobe4's user avatar
  • 16.1k
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 ...
Peter Rincker's user avatar
18 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. ...
muru's user avatar
  • 25k
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
Ruslan Osmanov's user avatar
18 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 ...
Tommy A's user avatar
  • 6,790
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
muru's user avatar
  • 25k
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-...
fuzzybear3965's user avatar
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") ...
Jasha's user avatar
  • 513
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 = ...
nobe4's user avatar
  • 16.1k
11 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 ...
statox's user avatar
  • 50.1k
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 ...
Jose Membreno's user avatar
10 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 ...
filbranden's user avatar
  • 29k
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 &')
hsnotebook's user avatar
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 breakpoints either in the gdb buffer using b ...
thakis's user avatar
  • 261
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 - ...
meain's user avatar
  • 247
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 $...
muru's user avatar
  • 25k
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 ...
Trevor's user avatar
  • 487
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:...
KFL's user avatar
  • 585
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&...
Ciro Santilli OurBigBook.com's user avatar
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 ...
Martin Tournoij's user avatar
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. ...
Christian Brabandt's user avatar
8 votes
Accepted

`Read` seems to bring an extra line - how to prevent this?

I don't have a good explanation for why Vim does this, it just does. :help :read says that the :read command inserts text below the cursor and Vim considers the cursor in an empty buffer to be on the ...
garyjohn's user avatar
  • 6,349
8 votes
Accepted

Copy output of bash command to system clipboard?

Everything after ! will be a command, including |. So what you're running is: :redir @+ :w !node | redir END This makes sense, since using | is shell commands is common to pipe output. The easiest ...
Martin Tournoij's user avatar
8 votes
Accepted

How to get the names of all open buffers?

I suppose by "open files" you mean active buffers (see :h windows-intro). That could be achieved with the command :ls a (use h-flag to get hidden buffers if needed). To pack it into a function you ...
Matt's user avatar
  • 20.8k
8 votes
Accepted

separate stdout and stderr of system() command

Vim's system() indeed captures both standard out and standard error by default. If you just want one of those, just use the normal shell means to redirect the other to the null device. For example, ...
Ingo Karkat's user avatar
  • 17.9k
7 votes
Accepted

Error when sourcing my .vimrc

command will give an error if a command with that name already exists. Vim has no way of knowing that the command in your vimrc is actually the same command; from Vim's viewpoint it's a "new" command. ...
Martin Tournoij's user avatar
7 votes
Accepted

How do I insert the output of an external command at the cursor position?

One can execute any external command in command line using the system function: :let @a = system("ls -ltr") And when you want to paste, press Ctrl+R and immediately a to paste the content next to ...
SibiCoder's user avatar
  • 3,412
7 votes
Accepted

Use search matches as parameters in external command

To avoid leaving the buffer for entering the password, you can use sudo's -S flag: -S, --stdin Write the prompt to the standard error and read the password from the ...
muru's user avatar
  • 25k

Only top scored, non community-wiki answers of a minimum length are eligible