I was writing a shell script and ran into a problem: Is there a way to open a file using the user's specified text editor?
The user's chosen editor should be in $EDITOR, but you must still choose a sane default.
"${EDITOR:-vi}" file.txt
Ignacio's right (though arguably, the fallback should be ed, which POSIX requires to be present, although it's essentially only useful to old-timers).
If you're thinking about graphical editors, xdg-open file.txt is what you're after.
note: xdg-open file.xml will open in a Web-Browser, most likely.
So, try;
# select your default sensible-editor from all installed editors, or not.
select-editor
# Open Default Text Editor
sensible-editor file.xml
Related
I'm using gedit. I have multiple tabs open, some are simply text that I have copied from other tabs, and haven't been saved to any file.
Is there a way to compare text that is selected in one tab to text selected in another tab? I know that I can use meld, diffuse, diff, etc., but those (afaik) require the selections to be saved to files first.
I remember using an editor (maybe emacs?) many years ago that could do something similar. But now I almost exclusively use gedit for text file editing.
Any ideas anyone?
TIA
ken
Okay, got a pointer from google:
In gedit, I created a new external tool that will run diff with the clipboard contents against the currently selected text.
Here's the bash code for the tool:
#!/bin/bash
SEL=$(cat)
diff -B <(echo -en "$SEL") <(xclip -selection c -o)
Note that this requires xclip, which is in most linux's repos. Sorry, don't know about Windoze.
I'd like to have a command I can insert into a command pipeline that adds color escapes to its input according to vim's syntax highlighting capabilities.
For example:
cat somefile.js | vim - <???> | less
The resulting text would be that of somefile.js, but colorized according to how the current vim configuration would do it in-editor.
It occurs to me that this must be possible. I agree that the example up there isn't what a sane man might call exactly useful, but that doesn't mean the idea never is.
I think your idea has one basic flaw: that nobody ever thought about allowing such a thing.
Clearly vim is capable of doing syntax highlighting. But I'll bet you an ice cream cone that if you can manage to get vim to stream text through and process it, that you won't like the results.
Consider what happens when you pipe text through more (or less if you prefer). When it goes to the terminal, these programs display one screenful and wait for you to hit the space bar. But if you redirect stdout to some other place than the terminal, these programs notice this and simply copy their input to their output unchanged.
If vim doesn't notice that you are piping text through, it is likely to send cursor-movement commands that you probably don't want in your output. If vim does notice, it is likely to just pass the text, and not syntax-color it. Only if vim does do the syntax-coloring but does not inject cursor-movement stuff will your idea work.
You could try it. Here's an answer that discusses piping stuff through vim:
Execute a command within Vim from the command line
But I say why not pipe your text through a program that was designed and intended to have text piped through it? Pygments can colorize every major programming language and markup format.
http://pygments.org/
The major advantage I see for your idea: you can customize the way vim does syntax coloring, get it the way you want it, and then also use vim to process your text. But it's probably not that hard to customize Pygments, and it might even be satisfactory out of the box, in which case it would definitely be the easiest way to go. And Pygments not only has ANSI sequence output, it also has HTML output, RTF, LaTeX, etc. So if you get Pygments working the way you want it to, it should be able to output whatever output format you need; vim will only have the ANSI sequence one.
There's a Perl module called Text::VimColor that I've heard will do kinda what you're looking for.
http://search.cpan.org/dist/Text-VimColor/
But let me ask this: Why do want it to go through less? Why not use vim as a perfectly good file viewer? view - will read from standard input in read-only mode.
https://gist.github.com/echristopherson/4090959
Via https://superuser.com/a/554531/7198.
Tried on /etc/passwd and it works surprisingly well!
This might be what you're after
cat filename.sh | vim - -c 'syntax on; syn=bash'
This is ugly, but you could alias this:
alias vim.sh="vim -c 'syntax on; syn=bash'"
Then use like this:
cat filename.sh | vim.sh -
Use vimcat !
wget -O /usr/local/bin/vimcat "https://www.vim.org/scripts/download_script.php?src_id=23422"
chmod 755 /usr/local/bin/vimcat
vimcat /etc/passwd
See also: https://www.vim.org/scripts/script.php?script_id=4325
Is there a way to make cat, less etc. print tab characters instead of tabs being converted to spaces? I am annoyed by this when I copy code from the terminal to an editor.
I am seeing two problems here.
First, destination editor can covert TAB to number of spaces. Some
editor has default feature to convert TAB to number of spaces. If
you disable this feature TAB character you copied from terminal will
be copied as TAB(instead of space) to an editor.
Windows Notepad++ has similar feature
. If you are using vim, this page will be helpful for
vim tab and space conversion
Another, source file in your case terminal may be representing tab
as spaces, please check that first. You can use cat -t filename to
see if you have any TAB in source file or not. That command will
display TAB character as ^I.
It seems this is not possible with less (see answer to the same question on unix.stackexchange).
As a workaround, it works with cat or, for some minimal paging capabilities, with the more command.
I am working with text files that contain a lot of unicode characters (≼, ⊓, ⊔, ...). Vim displays them fine, but when I print they are replaced by a generic character. Gedit prints them without problem, but it's a bit of a pain to launch another editor just to print.
Is there a way to get vim (on Linux/Gnome) to print properly? I tried using vim-gnome, in hope that it would use the same infrastructure as gedit, but it does not.
Vim is only able to use 8-bit encoding for printing. If there is encoding that includes all those characters all you need is to use
set printencoding={encoding}
If there is not then you can’t print it from vim directly. You can use :TOhtml command suggested by #DaoWen, do
:TOhtml
:w /tmp/print.html
:!command-that-makes-browser-print-a-file(I-do-not-know-one) /tmp/print.html
:!rm /tmp/print.html
. You can also use my formatvim plugin to print this to pdf through latex (don’t forget to file bug reports: latex-xcolor output is untested):
:Format format latex-xcolor to /tmp/print.tex
:!pdflatex /tmp/print.tex && lp /tmp/print.pdf && rm /tmp/print.*
(you can use html output as well, but that will not make me know a command to print it). Of course, you can map these to a single key.
Try using the :TOhtml command to convert your document to output your buffer in HTML format. You should be able to print the resulting file from your browser.
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