How to open files in web browsers (e.g Firefox) within editors like vim or emacs? - vim

How to open files in browsers (e.g Firefox) within editors like vim or emacs? Notepad++ open files in browsers by pressing Ctrl+Shift+Alt+X (Firefox). Is there a way to do this in gVim or Emacs?

browse-url-of-file is an interactive compiled Lisp function in
`browse-url.el'.
It is bound to <menu-bar> <HTML> <Load this Buffer in Browser>, C-c
C-z v.
(browse-url-of-file &optional file)
Ask a WWW browser to display file.
Display the current buffer's file if file is nil or if called
interactively. Turn the filename into a URL with function
browse-url-file-url. Pass the URL to a browser using the
browse-url function then run browse-url-of-file-hook.

In emacs I don't think this is built in, I may be wrong, but if not here is a function to do it:
(defun open-in-browser()
(interactive)
(let ((filename (buffer-file-name)))
(browse-url (concat "file://" filename))))

For whatever reason, my EmacsW32 on WinXP install kept sending browse-url directives to shell with "open file:// alone, and that didn't work so well*. Cutting it off at the knees, and modifying justin's original as below worked for me:
(defun open-in-browser()
"open buffer in browser, unless it is not a file. Then fail silently (ouch)."
(interactive)
(if (buffer-file-name)
(let ((filename (buffer-file-name)))
(shell-command (concat "start firefox.exe \"file://" filename "\"")))))
Needs some improvement. As well as replacement of your favorite browser. d**n you, hard-coding.
* I think the problem was the system-type check in browse-url-default-windows-browser, but not positive.

In gVim:
:!start cmd /c "C:\Users\pierre\AppData\Local\Google\Chrome\Application\chrome.exe" file:///"%:p""
You need the file:// URI to indicate that it is from the file system, this will work with all browsers. %:p produces the full file path for the current file. The quotes are necessary.
Simply map that to whatever you choose. You may need to do set shell=cmd.exe if you've set your shell to bash or something else.
In emacs (quoting justinhj):
(defun open-in-browser()
(interactive)
(let ((filename (buffer-file-name)))
(browse-url (concat "file://" filename))))

You mean you'd like to open the file currently being edited in a web browser?
In Vim, use something like :!firefox %.
Edit: You could, in fact, use nmap <silent> <C-M-X> :!firefox %<CR> to cause Vim to act very much like Notepad++ (though this mapping won't care whether you press shift or not).
Note that not every browser will actually render the file's contents when given the filename on the command line; e.g. Google Chrome will open a "save as" dialogue instead, as if you were downloading the file in question. Look up your browser's docs if in doubt. Firefox will 'just work', though.

Depending what you want to do with this, you might consider Emacs + MozRepl, which basically lets you send javascript commands to Firefox via telnet. Unfortunately I can't seem to write the elisp to make this work, but a related trick for reloading webpages from within emacs is shown by Sard in What's in your .emacs?. More information on integrating emacs and mozrepl from the original source and also here for a cool trick that updates the page in the browser as you type in the emacs buffer - it's pretty nice for getting instant feedback when working with html.
I reckon the same thing would work with vim, but I've only used it in emacs.

I do it an Elisp function using shell command xdg-open.
Then I define a key in html-mode to call the function.
You've gotta be comfortable adding stuff to your .emacs file.
(defun open-html()
"Get the HTML file path & open it"
(interactive)
(let (html-file-path)
(setq html-file-path (buffer-file-name))
(shell-command (format "xdg-open '%s'" html-file-path)))
)

This answer is based on Emacs 26.2
Emacs has the functions for opening a file in a browser built in but the behavior on different platforms may be different. Looking into the source code and documentation of browse-url-of-file by entering C-h f browse-url-of-file you'll see that the variable browse-url-browse-function determines which browser is used. You can then customize this variable to use, say, Chrome, by choosing browse-url-chrome and then apply and save the change. To access the customization page either entering C-h f browse-url-browser-function and then selecting the customize hyperlink, or M-x customize then searching for browse-url-browser-function.

Related

Failing to search multiple buffers in Emacs using "icicle-search-buffer" and C-RET

I am trying to use the icicle-search-file/icicle-search-buffer commands, but am having the same problem with both where C-RET seems to perform no operation on completion options once I have already entered the search context pattern.
The operations described are using Emacs 24.3.1 on Ubuntu 13.10 with icicles, workgroups2, and some custom key bindings all done with H-_ or C-c _ to avoid conflicts. I have icicles and workgroups2 set to be on at start in my .emacs file. I don’t have anything custom bound to C-c `.
I have learned to use icicle-search (C-c `) with progressive completion (S-SPC) and it works well, which is why I wanted to figure out these commands.
I have called the icicle-search-buffer command with each of these options just to be sure there is no difference, and the result are the same:
C-9 C-c `
M-s M-s b
M-x icicle-search-buffer RET
I enter the search regex (.* RET - to make things simple) just as with icicle-search, and am then prompted for the buffers to search. I move through the completion options with arrow keys and use C-RET on the buffers I want to search. The result of this is the buffer I chose disappearing from the completion list and no obvious result appearing. Hitting RET at any point just searches the buffer I had active before beginning the search, just as if I had used C-c `. It does not search the currently selected buffer in the completions list.
Does anyone see where I am failing to understand the usage of these commands? I have read through the information on the Icicles - Search Commands, Overview page.
The "Search Multiple Buffers, Files, and Bookmarks" section seems to describe this simply, but beyond using C-RET not much is said that suggests to me where my problem is.
I also followed the link in that section to the Icicles - Multi-Commands page
which was helpful in seeing how icicles modifies vanilla Emacs commands and how multi-commands work in general, but I could not determine from that where my mistake is.
You were doing nothing wrong. (And your description of using Icicles search is a good one.)
I introduced a bug recently.
Should be OK now. Please download the latest version of icicles-mac.el. Or wait (up to a day) for it to be mirrored on MELPA and download it from there. Sorry for your trouble, and thanks for bringing this to my attention.
IMPORTANT:
If you byte-compile Icicles (recommended), then whenever (as now) icicles-mac.el is updated, you
must load icicles-mac.el (not just icicles-mac.elc), then compile it, then *recompile ALL***of the other **Icicles source files as well. You want to make sure that all byte-compiled files are up-to-date.
This is normal for Lisp. Code that depends on macros needs to be byte-compiled anew after loading the updated macros.

Generally, how do I "go to definition" in VIM? Then how do I with golang?

Two part question:
First, when using VIM what process do I take and what keys do I type to "go to definition" or "go to declaration" etc.? This document might be the answer to my question, but I can't get it to work, so I'm unsure. It looks like its merely text matching the string rather than finding the true definition. If I can get this to work, then will I be able to jump outside of the current document to a definition/declaration? Or does this only work within a single document?
Second, how do I make this work specifically with the Go programming language? It sure would be nice to "click" the Client in
clnt := &http.Client{Transport: tr}
And be taken to the actual code that defines an http.Client.
Possible? How?
As you guess, gd (and other commands) is merely text matching, vim doesn't understand the syntax as it is just a text editor, :h gd will explain how gd works.
Usually, 'go to definition' is brought by using CTRL-] and tag files. A user manual about this topic can be read by :h 29.1.
First you need to generate a tags file for your project, as latest Exuberant Ctags has supported golang (from here), command
cd /path/to/your/project
ctags -f tags -R --fields=+K+a
will do the job.
Second, open vim, by default vim will find tag files under working directory (according to 'tags' option), if the tag file is found successfully, then CTRL-]` should works well.
Also check two useful plugins Tagbar and Easytags.
For golang, you can use the application godef to do it. The pluging vim-go helps you on setting everything, so, you just type 'gd' in a definition and it goes to the exact definition.
https://github.com/fatih/vim-go/blob/master/doc/vim-go.txt

call vi from within vim (useful for svn commit)

I am using RedHat EL 5. I use gvim 7.1 compiled using GTK. What I want is to be able to do an svn commit (which uses vi/vim) from within gvim. Currently the only problem is that I get output which is garbled.
For example, calling :!vi produces this from within gvim:
[7;1H~
[8;1H~
[9;1H~
[10;1H~
[11;1H~
[12;1H~
[13;1H~
[14;1H~
[15;1H~
[16;1H~
[
17;1H~
[18;1H~
[19;1H~
[20;1H~
[21;1H~
[22;1H~
[23;1H~
[24;1H~
[25;1H~
[26;1H~
[27;1H~
[28;1H~
[29;1H~
[30;1H~
[31
;1H~
[32;1H~
[33;1H~
[34;1H~
[35;1H~
[36;1H~
[37;1H~
[38;1H~
[39;1H~
[15;42HVIM - Vi IMproved[17;43Hversion
7.0.237[18;39Hby Bram Moolenaar et al.[19;29HVim is open source and freely distributable[21;36HHe
lp poor children in Uganda![22;28Htype :help iccf<Enter> for information [24;28Htype :q<En
ter> to exit [25;28Htype :help<Enter> or <F1> for on-line help[26;28Htyp
e :help version7<Enter> for version info[1;1H
How do I configure vi/vim/gvim to solve this problem and thereby enable my svn commits to look proper when called from gvim.
Thank you,
Nachum
Don't use vi as the command, use gvim -f instead.
The problem is that vi (or vim in a console) requires a terminal that can do stuff like move the cursor around, etc. gvim's pty is a very basic ASCII-only terminal.
Plain old vim doesn't have this issue because it just pipes the subporcess directly to your terminal, hence all of the escape sequences still work.
You can use gvim -f instead, so that a new gvim window will pop up for your commit message. (the -f prevents backgrounding) This isn't exactly what you asked for (since you get a new window) but it's the closest you can get to what you asked for without adding full terminal support to vim.
I use VCSCommand, a nice VCS wrapper that works with SVN, GIT and others. :VCSCommit or ,cc opens a new window under the current one, lets you type your message and does the actual commit on write. Sure that's one more plugin in your setup but the conveniance may be worth it. It is for me.

Use Vim to "colourize" files or input streams

This may be an odd question, but still. I use cat to display a file in bash (KDE Konsole),
cat foobar.rb
Now, I would like to use Vim to colourize that foobar.rb file according to what you would get when you start foobar.rb in Vim. Edit: But only for display purpose, on the terminal.
I am not sure this is possible, but I thought it would be neat if I could use Vim for that.
I really just want colourized keywords, and Vim has the perfect colour definitions.
So I thought combining this would be great.
Is this possible in Vim out of the box though?
One approach would be to use a library such as Pygments, which is a general purpose syntax highlighter. You could write a wrapper called ccat or something that would apply syntax highlighting to an input file and write to stdout.
If you want to page up and down in a highlighted file, you can use less with the -R switch, which passes control characters through to the terminal directly, preserving colours. So:
ccat file.rb | less -R
But at that point, you're pretty much at the capabilities of view.
I'm not sure if I understand your question correctly, but if you are only looking for a command that will give you a read-only view of the input file (like cat) but with coloured keywords, use view. view is an alternative way to start vim in read-only mode, so you have all syntax highlighting possibilities. From the vim man page:
view Start in read-only mode. You will be protected from writing
the files. Can also be done with the "-R" argument.
gvim gview
The GUI version. Starts a new window. Can also be done with
the "-g" argument.
evim eview
The GUI version in easy mode. Starts a new window. Can also
be done with the "-y" argument.
rvim rview rgvim rgview
Like the above, but with restrictions. It will not be possi-
ble to start shell commands, or suspend Vim. Can also be
done with the "-Z" argument.
I have always seen view on systems that have vim installed.
Closest is the less script that comes with vim:
cat myfile | vim -u /usr/share/vim/vim72/macros/less.vim -
Note the - argument to vim. You may need to change the vim72 to your version (and the whole path if you have it installed elsewhere)
Now, this isn't exactly what you want, because its behaviour is less-like, in that you have to press keys to make it scroll down or complete. However, they are briefer than usual vim. For example, space to scroll down; and q to quit (not :q).
You want a cat-like version; me too. But there doesn't seem to be one.
EDIT uh, there's also a vimpager project, that includes vimcat - exactly what you want. But it doesn't come with vim, and I haven't tried it yet.
vim.org: http://www.vim.org/scripts/script.php?script_id=1723
github: https://github.com/rkitover/vimpager

Kill/Yank (cut/paste) in ZSH

I use zsh and have the Emacs keybindings set up for it. I'd love to have it replicate all my regular Emacs text manipulation commands. One which I miss is the Kill/Yank keys. It would be nice if I could select text (using C-SPC - this works) and then do something like a kill-region (C-w - This right now deletes the previous word). The yank (C-y) works fine and I can even even cycle through them using M-y.
Does anyone have such a setup working?
A simple "\C-w": kill-region in my .inputrc file binds the key to the function I want.
Update: Spoke too soon. The above only does it for applications that use the readline libraries. Zsh uses its own zle. The way to configure similar behaviour is to stick bindkey "\C-w" kill-region into your .zshrc

Resources