1

I am trying to use reverse searching from PDF documents to their respective *.tex files. I use vim (in terminal) as my editor and my PDF viewer is Zathura. Forward searching works however the problem with reverse searching is that vim requires a server name to be defined so that zathura can send it commands.

Is it possible to configure vim so that when running vim name.tex in terminal it will automatically define a server name which is 'name'? Essentially, I would like to make it so vim name.tex runs as if I had input vim name.tex --servername name. Is such a thing possible? I am aware that the server name cannot be defined once vim is running and needs to be part of the terminal command, however I am not experienced with this sort of thing.

EDIT: After some testing if I run vim --servername vim in multiple terminals and then run vim --serverlist in another I notice that all the servers have the name VIM followed by the number. So for example if I have four terminals open and run vim --servername vim in each and open a fifth terminal and run vim --serverlist it returns,

VIM
VIM1
VIM2
VIM3

With this if I have set synctex-editor-command 'vim --remote-silent +%{line} %{input}' in my zathurarc by <C-left click> it will reverse search to the appropriate line of my TeX code in the server with lowest numbered VIM servername. So if I close the server VIM and the remaining servers VIM1, VIM2, and VIM3 are still open the the reverse search will bring up the TeX code in VIM1. So essentially this simplifies the problem to just having a script which executes vim name.tex --servername vim.

SOLUTION

This solution is due to statox and as such I have marked him as having solved it. I simply tinkered with his idea to get the final product. For any of those who pass and are in need of a solution in ~/.bashrc place the following

alias vimsn='vim-with-servername'

vim-with-servername() {
vim --servername 'vim' "$@"
}

alias vim=vimsn

After doing this restart your terminal. Running vim file.type in terminal will now run vim --servername vim file.type.

1

If that needs to be done before vim is started what about using a bash alias?

In your ~/.bash_alias file (or ~/.bashrc or ~/.zshrc) you could add the following:

alias vimsn='vim-with-servername'

vim-with-servername() {
# Check we have one argument and it ends with .tex
if [ "$#" -ne 1 ] || [[ ! "$1" =~ "\.tex$" ]]; then
    echo 'Usage'
    echo 'vimsn filename.tex'
    return 1
fi

# Remove .tex from the filename
servername=${1%.tex}

# Start vim with the servername
vim $1 --servername $servername
}

Then when youll need to open vim the way you describe it you can use the following command:

vimsn name.tex

And that should actually run

vim name.tex --servername name
10
  • I tried your solution just now and I get the error: bash: vimsn: command not found. I tried placing this in ~\.bashrc and ~/zshrc. I did not have ~/.bash_alias but I created it and I encountered the same error as above. I have added an edit to my original post which seems to simplify the problem. May 4 at 9:38
  • So 1) if you put it in ~/.bash_aliases (there was a typo in my answer) you need to source this file in your shell rc file 2) If you put it in your shell rc file you need to put it only in the corresponding file either ~/.bashrc if you use bash, ~/.zshrc if you use zsh or another file if you use another shell 3) you need to source your rc file again to have the alias available, the easiest way to do that is to close and reopen your terminal after you modified your rc file 4) regarding your edit you can modifiy the function a bit to use 'vim' instead of the variable $servername
    – statox
    May 4 at 10:18
  • Ok so following your advice vimsn now works but only returns Usage and vimsn filename.tex however it does not run vim with the desired server name. Sorry I have very little idea of what the script is doing. May 4 at 14:31
  • No problem, I tried to be explicit in the comments but that wasn't enough :) So the if checks two things: 1) that you gave an argument to your command 2) that is argument ends with .tex so if you call vimsn with more than one file or with a path which doesn't end in .tex you will get the Usage message. How do you call vimsn?
    – statox
    May 4 at 14:55
  • I would call it as vimsn Notes.tex where Notes.tex is a specific file of mine. I played around with your code and simply putting vim 1 --servername 'vim' within {} works. So by this I mean, vim-with-servername(){ vim $1 --servername 'vim'} May 4 at 14:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.