How to Open Another File in Background Vim from Bash Command-Line

Grunt: Executing another command-line task while watch is running in background

  • You can execute external commands from within Vim: :! grunt ...; this blocks Vim until the command is done.
  • If you need the original shell for command execution, you can :suspend Vim (also via <C-Z>), and then return to it from the original shell (after executing grunt) via fg.
  • With a terminal multiplexer like screen or tmux, you can run both Vim and other shell(s) concurrently inside the same terminal. You'll find plenty of posts describing such setups.

How to open a file or buffer in a new tab without leaving the current one and without switching edit-modes?

You can use

$ vim --servername BOB --remote-tab +":tabprevious" filename

edit

Vim has no command for opening a tab in the background. That's why we need the :tabprevious trick to open the new tab and go back to the previous one immediately. The drawback is that we are not in insert mode anymore.

Fortunately, we have gi, the normal mode mapping used to return to insert mode where it was last exited. This command works, here:

$ vim --servername BOB --remote-tab +":tabprevious" filename && vim --servername BOB --remote-send "gi"

Including and generalizing it in a shell script doesn't sound complicated.

Here is a second way, using the same ingredients but combined differently:

$ vim --servername BOB --remote-send "<Esc>:tabe filename | tabprevious<CR>gi"

Basically, we exit insert mode, do our "tab" business and get back to where we were. And we have another drawback: this method is tied to insert mode so we would end up in insert mode even if we were not there before.

Unfortunately I don't see a way to do that cleanly that doesn't involve writing a function.

How do I redirect output into Gvim as a list of files to be opened?

I can think of a few ways of doing this:

Use vimgrep

Use vimgrep: after running gvim, enter:

:vimgrep /background/ **/*.vim

This will populate the quickfix list with all of the matches (possibly more than one per file), so you can use things like :copen, :cw, :cn etc to navigate (see :help quickfix)


Use vim's built-in cleverness

Use findstr to give you a list of files and then get vim to open those files:

findstr /m background *.vim > list_of_files.txt
gvim list_of_files.txt

" In Gvim, read each file into the buffer list:
:g/^/exe 'badd' getline('.')

" Open the files in tabs:
:bufdo tabedit %

This will load each file, but will keep the list of files open as well (you can always bunload it or whatever).

Edit:

Using :tabedit on a list of files didn't work (I'd only tested :badd). You can get round this by either using badd and then bufdo (as above) or by doing something like this (put it in your vimrc):

command! -range=% OpenListedFiles <line1>,<line2>call OpenListedFiles()

function! OpenListedFiles() range
let FileList = getline(a:firstline, a:lastline)
for filename in FileList
if filereadable(filename)
exe 'tabedit' filename
endif
endfor
endfunction

Then simply open the file containing all of your required file names and type:

:OpenListedFiles

Use Vim's server functionality and some awful batch scripting

Use the server functionality and some batch script magic (which I don't understand as I use bash)

@echo off
REM Welcome to the hideous world of Windows batch scripts
findstr /m background *.vim > list_of_files.txt
REM Run GVIM (may not be required)
gvim
REM Still in command prompt or .bat file here
REM for each line in the file "list_of_files.txt", pass the line to OpenInTab
for /f %%i in (list_of_files.txt) do call:OpenInTab %%i
goto:eof

:OpenInTab
REM Open the file in a separate tab of an existing vim instance
gvim --remote-tab %~1
goto:eof

Eeeurrgh.


If it were me, I would go with the "Use vim's built-in cleverness" option. Actually, that's not true: I'd use cygwin's bash script and just use bash, but if I HAD to do it with the native tools, I'd use the built-in cleverness approach.

Freeing Terminal While Using gVim

You could run gvim in background as any other process:

gvim [file] &

After executing this command you receive a message indicating the pid of the new process. When you end it you should receive a similar message on that shell.


Edit:

The ctrl-z/fg problem is probably related to windows. This question states that GitBash would create a new shell instead of returning to the current one, so it probably doesn't work as in Linux. A possible solution would be to run your commands from gVim, either calling the shell through :! on mappings, or plugins/commands (fugitive for git, :py or some plugin for python interpreter, etc).

Vim open file after :sh

You don't need to go back to the shell to open other files: :edit, :split, :vsplit, :tabedit all allow you to edit files from anywhere on your disk (and with tab-completion):

:sp ../../path/to/file

You can play with the wildmenu, wildmode and wildignorecase options to make it even better.

You can also use the built-in Netrw plugin to navigate through your filesystem with a slightly more comfortable "graphical" explorer (see :help netrw):

:Ex
:Tex /path/to/

Also, :shell opens a new shell which most likely won't have much knowledge about your initial Vim session and thus won't be able to talk with it.

Vim opens a new folder instead of an existing file with `set enc=utf-8` enabled

Change the order of the autochdir and encoding options in your vimrc. First set the encoding then autochdir

set enc=utf-8
set autochdir

An explanation can be found here



Related Topics



Leave a reply



Submit