Is it safe to use cat <<-EOF - bash

cat <<-EOF
#lots of text
EOF
This syntax sure makes it simpler to manage lots of lines and also is just a single call to cat rather than a bunch of echos.
But is it safe to use <<-?
There are many text editor settings that may mess with spaces and tabs and its annoying to keep track of those. (portability is a concern)
Using only << would make the source code look dull without indentation.
Is there any safer and clean way to do this?
I have made a small edit and now it doesn't seem to be opinion based in any way.

You should realize that some users set their editors to automatically convert tabs into spaces. So when you work with multiple users on the same script, it is paramount to set up some base rules for this, which is annoying as you will end up with a one-day tab-vs-spaces war. Furthermore, I often consider tabs evil. They are good as a field separator, but they are awful when visualization depends on it. Everything depends on where the tabstops are defined and this can change from editor to editor and terminal to terminal. Sometimes. the tabstops are defined as a multiple of 8, sometimes as a multiple of 4. There is even the Posix command tabs which allows you to set the tabstops in any way you want
$ tabs 1,6,10,14,18,22,26,30,34,38,42,46,50,54,58,62,67
So ... tabs are evil!
My suggestion to you is to use the following:
printf "%s\n" 'fantastic line1' \
'awesome line2' \
'amazing line3' \
'unrivaled line4'

Related

indentation with a single dollar sign

This is an Emacs config question.
I've got code indentation trouble using Emacs-26.1 with markdown-mode version 2.4-dev in Ubuntu 19.04. I also have Polymode and Auctex installed. I am pretty sure that Emacs is trying to use tex mode when it should use markdown mode, but it is not always doing that.
Sometimes I get a giant indent. I am writing in an "*.md" file, intending to write markdown. As I write about shell programs, I sometimes have one dollar sign in code examples. I'm pasting a literal example. I've tried to insert a commented-out dollar sign to prevent this, but Emacs indents that enoromously.
```shell
az vm create --resource-group $grpName --name pj80 \
--image UbuntuLTS --custom-data cloud-init-tc.txt \
--generate-ssh-keys
```
<!-- $ -->
The automatic indentation tries moves all text under the variable "grpname". It does that until I insert another dollar sign. I guess this is happening because Polymode wants to interpret the dollar sign as a math inline.
When text should be flush left, I often get a 4 space indent if I accidentally hit TAB.
There are too many moving parts in this, I don't know which I should be trying to fix. Thanks in advance.
A couple things I would check:
C-h m to verify the buffer is using the correct modes you expect.
Check what indent-line-function and indent-region-function are set to use. Consider browsing the source for these functions and see if there are any obvious configuration points for your mode.

Fix indentation for entire project in Xcode 7?

I've inherited a huge iOS project which is all using two-space indenting. I need that to be four-space indenting. I know that I can hit Ctrl+I per file, but I'm wondering whether Xcode (or AppCode, for that matter) has a means of re-indenting every source file in the entire project in one whack.
Failing that, I’m wondering how I might go about writing some sort of script (using Automator, or perhaps an Xcode plugin, or even something on the command line) to achieve this without going insane.
You can use SwiftFormat to make indentation on all of your project .swift files
$ brew install swiftformat
$ cd <path to your project>
$ swiftformat .
Refactoring and Re-Indenting are quite different operations. I'm making this distinction so that folks newer to coding / coding IDEs aren't confused.
Refactoring: taking an existing variable / block of code and propagating changes through the project where it is used. For example, refactoring (renaming) a variable foobar to foobar2 would automatically find all cases where foobar is used and change it to foobar2
Re-indenting: changing the structure / flow / alignment of the code to match the desired coding style for clarity / correctness, particularly ensuring that nested items like conditionals and loops are neatly aligned to improve code readability.
You can use SourceKit, but it is a bit complicated to use, so instead you can use SourceKitten which is a wrapper on top of SourceKit.
You have to search for all .swift files and run sourcekitten format --file path/to/file
find . -type f -name "*.swift" | while read line; do
sourcekitten format --file "$line";
done
So, I did manage this with AppCode in the end. We ended up doing a wider-scale refactor using AppCode’s various tools. I’m not sure if I would’ve been able to do JUST the spacing, their Twitter support suggested that I could, but didn’t detail exactly how.

Any way to make terminal control sequences portable?

I'm currently in the process of planning out a custom Vim-like editor. It's going to be written in C and I want it to be as portable as possible between as many types of systems as possible.
I'm aware of curses (ncurses, I suppose), the tput command, and how terminals use control sequence (Esc-[ and the CSI character) to change backgrounds, move the cursor, etc.
Of the options above, it seems like ncurses would be the most recommended way of printing for the editor. BUT ncurses also has a LOT of stuff that I rather wouldn't use, and if it's reasonably feasible I'd rather make my own system. I'm not against using it, but .. anyways.
So, my question is: Is there any way to use control sequences in the vast majority of terminals without using a library? Whether through tput or another method?
Thanks!
tput(1) uses the terminfo(5) (or older termcap(5)) database, which provides the mapping from abstract commands such as move cursor to x,y to escape sequences for different terminals. When you run a command such as
$ tput cup 10 3 # move cursor to row/column 10/3
, the terminfo database is queried to find the correct string for your terminal, which is then simply written to stdout. To find the available commands (e.g., cup), look at the cap-name column in terminfo(5). tput determines what terminal you are using by looking at the TERM environment variable.
(This means that you can check what escape characters are being generated by simply doing $ tput [command] > [file] and opening [file] in some editor that can show control characters, which can be handy for exploration. The infocmp(1) command can also be used for this.)
If you use tput (or the underlying tputs(3)), your program is hence automatically portable to different terminals. This is what Vim uses by the way.
However -- in the modern world, pretty much all terminals (or terminal emulators rather) use ANSI escape codes, along with some extensions (see XTerm Control Sequences). I believe the escapes supported by xterm and their behavior have become something of a de facto standard at this point, with other terminal emulators simply copying xterm's behavior. Some text-based UI libraries like termbox seem to do away with support for non-ANSI terminals altogether, and output ANSI escapes directly.
Besides the already-mentioned termbox, there's also S-Lang, which includes a terminal handling component. I believe those are the two most popular "ncurses replacements". I'd give ncurses some time first though.

Structural Highlighting in Vim

I have seem some cool Structural Highlighting in Visual Studio (might be using CodeKana). Can I achieve same type of Structural Highlighting in vim ? For any language. I am a python developer I would love to have it for Python.
You can almost emulate this via listchars by using hard tabstops instead of spaces for indentation.
Something like this should suffice:
set noexpandtab
set list
set listchars=tab:\|
Note that there is a space after the | character.
You can pick a better character to get a connected line, but this is just an example. Also note that this will not continue the lines with no indentation, so you may end up with something like:
if foo:
| bar
| baz
...unless you add a tab on the blank line.
What OP asks for is definitely not built in. I assume someone could add it as a plugin extension. An existing plugin does do multi-color highlighting of parens that is somewhat similar, maybe it could be used as a base to build a more full-fledged solution:
RainbowParenthesesInVim

Convert plain text to latex code programmatically

I'd like to take some user input text and quickly parse it to produce some latex code. At the moment, I'm replacing % with \% and \n with \n\n, but I'm wondering if there are other replacements I should be making to make the conversion from plain text to latex.
I'm not super worried about safety here (can you even write malicious latex code?), as this should only be used by the user to convert their own text into latex, and so they should probably be allowed to used their own latex markup in the pre-converted text, but I'd like to make sure the output doesn't include accidental latex commands if possible. If there's a good library to make such a conversion, I'd take a look.
Apparently, the following characters
\ { } $ ^ _ % ~ # &
are special in LaTeX, so you should make sure to escape them (prefixing with backslash will do for some of them, see Thomas' answer for special cases) or tell your users not to use them unless they deliberately want to use LaTeX commands (or a mix of both, depending on the character).
Some additional pitfalls:
Not every line break in the text might be intended as a new paragraph.
If your users use a language other than English (or Latin), you will need to \usepackage something that deals with the encoding (like utf8) or convert the characters yourself (e.g. ä -> \"a).
As dmckee points out, quotes also need to be treated separately.
EDIT: Since this has become the accepted answer, I also added the points raised in the other answers, so this is now a summary.
As Heinzi said, the following need attention:
\ { } $ ^ _ % ~ # &
Most can be escaped with a backslash, but \ becomes \textbackslash and ~ becomes \textasciitilde.
I think you might want to leave line breaks alone. LaTeX handles these in exactly the same way as many content management systems; many people have come to expect that "double line break" = "paragraph break". Heck, even stackoverflow itself works that way.
(You cannot write malicious LaTeX code; everything that happens inside LaTeX stays inside LaTeX. Unless you explicitly enable write18 when running latex, but it's disabled by default.)
Heinzi has already shown most of the basic characters that need to be escaped, but the hard part here is insuring that the quoting comes out right.
She said "He didn't do it".
needs to be converted to
She said ``He didn't do it''.
which looks easy in this trivial case, but is full of gatcha's that require careful handling. For modest size texts, I generally use a naive substitution generated in sed and diddle the results by hand. Things are both easier and harder if your "plain text" uses curly quotes.
Here "naive quote substitution" means that quotes followed by word characters are replaced by (one or two as appropriate) back ticks, and all others are replaced by (one or two) single-quotes ('). That catches most cases in prose, but you will have to clean up all the triple-quote cases by hand.
Another possible solution is to make all "special" characters into ordinary ones before inserting the user's text. That might avoid many headaches, but might also create new ones...
You can do this by changing the catcode of the character. The TeX Wikibook knows more.
\catcode`\$=12
will turn $ into an ordinary character. However, for some reason some characters don't come out as you'd expect. \ becomes a double open quote, { becomes a dash... and redefining } inside a group ({...}) makes TeX choke entirely.
Long story short: only recommended if you know what you're doing.

Resources