## Git

### How do I convert an SVN repository to Git when the SVN directory was renamed at some point in the history?

If you don't know the old directory name, do a svn checkout of the new directory first and examine the log with svn log -v. Next clone the old and new directories using git svn clone or svn2git, and add the old repository as a remote of the new repository. Then do a git log and note the oldest commit of the master branch, and do a git log old and note the newest commit of the old branch. Then join the two branches and clean up the repository. All together:

mkdir old-dir new-dir
cd old-dir
svn2git --notrunk --notags --nobranches --verbose old-svn-dir
cd ../new-dir
svn2git --notruck --notags --nobranches --verbose new-svn-dir
git fetch old
OLDEST_COMMIT_OF_MASTER=$(git log --format=format:%H | tail -1) NEWEST_COMMIT_OF_OLD=$(git log --format=format:%H old/master | head -1)
git replace "$OLDEST_COMMIT_OF_MASTER" "$NEWEST_COMMIT_OF_OLD"
git filter-branch
git remote rm old
rm -rf .git/refs/replace


### How do I delete all untracked and ignored files?

git clean -x -d --dry-run # Check the output
git clean -x -d --force


### How do I clear Git's credential cache in Plasma 5?

If you have the SSH_ASKPASS evironment variable set to ksshaskpass, then Git will use Ksshaskpass to prompt for credentials and then store these with KWallet. Future pushes and fetches will automatically use the credentials from KWallet. To remove or change these credentials, you need to do this from KWallet. On Plasma 5, you can do this by launching kwalletmanager5 --show. (Do not run kwalletmanager; this opens your KDE 4 wallet.) The Git credentials are stored in the ksshaskpass folder.

## Shell scripting

### How do I make sure that only one instance of a program is running?

Use a lock file:

/usr/bin/flock -n /path/to/lockfile -c /path/to/program


(Omit the -n if you want to wait rather than fail if the lock cannot be acquired.)

### How can I sort the output of du -h by size?

du -h | sort -h


### How do I grep for non-ASCII characters?

grep --color='auto' -P -n "[\x80-\xFF]"


### How do I find out what GNU/Linux distribution I am using?

Try one of the following:

cat /etc/*-release

lsb_release -a


## Networking

### How can I tunnel my web browsing through an SSH connection?

First, make an SSH connection as follows:

ssh -D SOME_PORT SOME_HOST


Then configure your browser to use 127.0.0.1:SOME_PORT as a SOCKS v5 proxy.

### How can I launch a VPN connection on a remote host without losing my SSH connection?

Say you are connected to a remote host via SSH, and want to launch a VPN connection on that host where the connection replaces the default gateway. Normally this will cause the SSH connection to hang. To avoid this, first add a routing rule such that connections to the SSH client always use the original gateway:

### How can I tell AUCTeX to display my viewer on a remote display?

A dual-monitor setup is convenient for having many windows open simultaneously. For example, it is common to have an Emacs window editing a LaTeX file on one monitor and a PDF viewer to view the output on the other monitor. But sometimes all you have are two separate machines, each with a single monitor (such as two laptops, or a laptop and a desktop). If you have the xpra tool installed on both machines, you can emulate a dual-monitor setup.

Let's assume that you want to run Emacs on the machine desktop and have the PDF viewer display on the machine laptop. First, start an xpra server on desktop using a display port of your choice:

[user@desktop]$xpra start :700  Next, have laptop attach to desktop's xpra server: [user@laptop]$ xpra attach ssh:user@desktop:700


Finally, in the Emacs running in desktop, start launching the PDF viewer using the usual C-c C-c View. When prompted for the command, insert DISPLAY=:700 at the beginning of the line. For example:

DISPLAY=:700 okular --unique foo.pdf


The nice thing about this is that it works with SyncTeX. That is, you can still type C-c C-v anywhere in your source document on desktop, and the PDF viewer on laptop will jump to the corresponding location in the PDF. Conversely, you can shift+click (or control+click, depending on the viewer) in the PDF viewer on laptop, and the Emacs running on desktop will jump to the corresponding source line.

When you are done editing, you can run xpra stop :700 on desktop to stop the xpra server. The client on laptop will automatically detach itself.

### How do I find which RPM provides a given file?

rpm -qf /path/to/file


### How can I install the build dependencies of a package with Zypper?

zypper si -d packagename


### How can I keep track of programs I install to /usr/local?

Certain discourteous programs do not provide an uninstall target in their Makefiles. A solution is to install these programs to a dedicated prefix, and then use GNU Stow to add symlinks to the system directories:

./configure --prefix=/usr/local/stow/PROGRAM_NAME --libdir=/usr/local/stow/PROGRAM_NAME/lib64
make
sudo make install
cd /usr/local/stow
sudo stow PROGRAM_NAME


## KDE

### How can I log out of KDE remotely (or from the command line)?

qdbus org.kde.ksmserver /KSMServer logout 0 0 0


### How can I get applications to display dates in ISO 8601 (YYYY-MM-DD) format?

As of Plasma 5, it is no longer possible to fine-tune the system locale settings. For example, you cannot have KDE format currencies according to one locale, times and dates according to another locale, etc. Instead you can specify only a single, global locale.

For times and dates, you can globally override the default locale by setting LC_TIME (for example, in $HOME/.i18n on openSUSE). Alternatively, you can override on a per-application basis by manually setting LC_TIME before launching the application. Unfortunately, you cannot specify a date/time format of your own choosing, but must select from among the locales installed on your system. Even more unfortunately, there is no easy way of knowing which of the locales (if any) format the date and time the way you want. Finding the right locale may be a matter of trial and error. Writing a new locale definition is supposed to be "easy" but is actually anything but (at least on openSUSE). For ISO 8601–style dates, using the en_DK locale sometimes works. So for example, to have a certain application display dates in YYYY-MM-DD format, you can put the following script somewhere in your execution path: #!/bin/bash LC_TIME=en_DK.UTF-8 exec /usr/bin/some_application "$@" >&/dev/null &


## LaTeX

### How can I set the font to Times?

Once upon a time, this was done by including the package times, though this failed to set Times as the math font. Later it was recommended to use mathptmx, which set Times for both the text body and math. But both packages are now obsolete. The following packages should be used instead:

\usepackage{newtxtext} % Use Times for main text
\usepackage{newtxmath} % Use Times for math


Note that this isn't quite the same as using times, since times also changes the default monospace font to Courier whereas newtxtext does not. If it's important to also change the monospace font to Courier, then the following code should also be added:

\DeclareRobustCommand\crfamily{\fontfamily{pcr}\selectfont}
\DeclareTextFontCommand{\texttt}{\crfamily}


### How can I allow Biblatex to perform arbitrary word breaks in DOIs?

\setcounter{biburlnumpenalty}{100} % Allow breaking at numbers (for DOIs)


### How can I make Biblatex print a longer dash for repeated authors?

\usepackage[dashed=true]{biblatex}
\renewcommand{\bibnamedash}{\rule[3pt]{3em}{0.7pt} } % long dash for repeated authors


### How can I make Biblatex print a comma between the author and year in citations?

\renewcommand*{\nameyeardelim}{\addcomma\space} % comma between author and year in citations


### How can I make Biblatex print a thin space between author initials?

\renewrobustcmd*{\bibinitdelim}{\,} % thin space between author initials


### How can I make Biblatex print the ISSN for books?

\makeatletter
\@for\next:=collection,inbook,incollection,proceedings,book,inproceedings,thesis\do{%
\xpatchbibdriver{\next}
{{\printfield{isbn}}}
{{\printfield{issn}\newunit\newblock\printfield{isbn}}}
{}{}%
}
\makeatother


### How can I make Biblatex print volume numbers for book series?

Instead of using the volume field, put the volume number in the number field. Then use the following:

% Formatting of numbered series
\DeclareFieldFormat[book,inbook,collection,incollection,proceedings,inproceedings]{number}{\bibstring{volume}~#1 \bibstring{ofseries}}
\renewbibmacro*{series+number}{%
\printfield{number}%
\printfield{series}%
\newunit%
}


### How can I make Biblatex print the volume and number as "volume(number)"?

% For periodicals and articles, set volume and number as "volume(number)"
\xpatchbibmacro{volume+number+eid}{%
}{%
}{}{}
\xpatchbibmacro{title+issuetitle}{%
}{%
}{}{}
\DeclareFieldFormat[article,periodical]{number}{\mkbibparens{#1}}


### How can I make Biblatex suppress quotation marks around article and thesis titles?

% Suppress quotation marks around article and thesis titles
\DeclareFieldFormat
[article,inbook,incollection,inproceedings,patent,unpublished]
{title}{#1\isdot}
\DeclareFieldFormat
[thesis]
{title}{\emph{#1\isdot}}


### How can I make Biblatex print a full long citation (including all authors)?

% Full long citations (including all authors)
\makeatletter
\newcommand{\tempmaxup}[1]{\def\blx@maxcitenames{\blx@maxbibnames}#1}
\makeatother
\DeclareCiteCommand{\longfullcite}[\tempmaxup]
{\usebibmacro{prenote}}
{\usedriver
{\DeclareNameAlias{sortname}{default}}
{\thefield{entrytype}}}
{\multicitedelim}
{\usebibmacro{postnote}}


### How can I mark a Biber record as "data-only" so that it can be used as a source of crossref data, but not included in the bibliography?

options = {dataonly=true},


### How can I export graphics from PowerPoint or LibreOffice for use with LaTeX?

PowerPoint 2010 has the ability to export to PDF, but this works only for entire slides. To export a single selection, use the following procedure:

1. Right-click on the graphic. Select Size and position…Size. Note the height and width. Press Close.
2. Copy the graphic (Ctrl+C).
3. Create a new presentation (Ctrl+N).
4. DesignPage Setup. Change the height and width to the values you noted previously. (You may need to add a few millimetres to each dimension.) Press OK.
5. Right-click on the slide and select Paste Options: Picture.
6. FileSave & SendCreate PDF/XPS DocumentCreate PDF/XPS.
7. Set "Save as type" to "PDF (*.pdf)". Enter a filename and press the "Publish" button.

The various LibreOffice programs (Draw, Impress, etc.) claim to be able to export a selection to PDF, but a longstanding bug prevents this from working. Instead, they export the entire page, much like PowerPoint 2010. There are a number of possible workarounds:

• Adjust the size of the page to the size of the selection, using a procedure similar to that for PowerPoint 2010 above. Then export the page to PDF.
• Export instead to EPS. (Recent versions of Tex Live's pdflatex automatically convert EPS to PDF. Alternatively, the graphic can be manually converted with the epstopdf script included with TeX Live.)
• Export the page to PDF, then crop the PDF using a tool such as BRISS or PDFCrop.

## PDFs

### How can I set the logical numbering and numbering style in a PDF?

Open the PDF file with a text editor and search for the /Catalog entry. Then append a /Pagelabels entry as follows:

/PageLabels << /Nums [
0 << /P (cover) >> % labels 1st page with the string "cover"
1 << /S /r >> % numbers pages 2-6 in small roman numerals
6 << /S /D >> % numbers pages 7-x in decimal arabic numerals
]
>>


Note that physical pages are numbered starting from 0.

Source: Renumber pages of a PDF

### How can I convert a PDF to a different page size?

You can use pdfjam (also distributed with TeX Live). For example, to convert any PDF to A4:

pdfjam --outfile out.pdf --paper a4paper in.pdf