There is a way to use something like gobble with lstinputlistings by using the xkeyvalpackage.
The idea is based on this answer to a related question.
The basic idea is to shift the whole content to the left using xleftmargin and adapt the position of the numbers by numbersep.
First we define a command which simply holds the argument passed to the basicstyle key of lstlistings
\def\lsttextstyle{\footnotesize\ttfamily}
This is used when defining a new length that holds the length of a whitespace in that specific textformat (it is used again in the \lstset part)
\newlength{\myspace}
\settowidth{\myspace}{\lsttextstyle{\ }}
Additionaly to length will be needed for the macro following
\newlength{\mytemp}
\newlength{\mytempb}
Then using the package xkeyval we define a new key called xgobble
\makeatletter
\define@key{lst}{xgobble}{%
\setlength{\mytemp}{\lst@xleftmargin}
\addtolength{\mytemp}{-#1\myspace}
\def\lst@xleftmargin{\mytemp}%
\setlength{\mytempb}{\lst@numbersep}
\addtolength{\mytempb}{-#1\myspace}
\def\lst@numbersep{\mytempb}%
}
\makeatother
Using the current value xleftmargin to set \mytemp subtracting the length of one whitespace multiplied by the value passed to xgobble and writing the result back to xleftmargin the whole code is shifted to the left. The similar thing is done with numbersepto shift the numbers back to the right (their original position).
It can simply be used like
\lstinputlisting[xgobble=<value>]{sourcecode.file}
Here is the full MWE
\documentclass{scrartcl}
\usepackage{listings}
\usepackage{xkeyval}
\def\lsttextstyle{\footnotesize\ttfamily}
\lstset{
language=bash,
xleftmargin=4\myspace,
basicstyle=\lsttextstyle,
numbers=left,
numbersep=2\myspace
}
\newlength{\myspace}
\settowidth{\myspace}{\lsttextstyle{\ }}
\newlength{\mytemp}
\newlength{\mytempb}
\makeatletter
\define@key{lst}{xgobble}{%
\setlength{\mytemp}{\lst@xleftmargin}
\addtolength{\mytemp}{-#1\myspace}
\def\lst@xleftmargin{\mytemp}%
\setlength{\mytempb}{\lst@numbersep}
\addtolength{\mytempb}{-#1\myspace}
\def\lst@numbersep{\mytempb}%
}
\makeatother
\begin{document}
This is the normal code of the full script
\begin{verbatim}
\lstinputlisting{testcode.sh}
\end{verbatim}
\lstinputlisting{testcode.sh}
now the echo part without indentation
\begin{verbatim}
\lstinputlisting[linerange={6-8}]{testcode.sh}
\end{verbatim}
\lstinputlisting[linerange={6-8}]{testcode.sh}
and now three examples with indentation
\begin{verbatim}
\lstinputlisting[linerange={6-8},xgobble=2]{testcode.sh}
\lstinputlisting[linerange={7-8},xgobble=4]{testcode.sh}
\lstinputlisting[linerange={7-8},xgobble=0]{testcode.sh}
\end{verbatim}
\lstinputlisting[linerange={6-8},xgobble=2]{testcode.sh}
\lstinputlisting[linerange={7-8},xgobble=4]{testcode.sh}
\lstinputlisting[linerange={7-8},xgobble=0]{testcode.sh}
\end{document}
... and the example bash script which probably doesn't even work:
#!/bin/bash
echo "Hello World!"
if ( whatever ) ;
echo "this"
echo "is"
echo "indented"
echo "and this is an incredible long line to check if linebreaking still works fine"
fi
Since I know hardly anything about xkeyval, I am not sure how stable that whole construct is.
gobblehas no effect when usinglstinputlisting. Is it impractical to unindent by hand? Do you have a large code section, or lots of files to be unindented? In any event, you may want to give a try to themintedpackage. – guillem Nov 19 '12 at 15:11mintedpackage. – Bakuriu Nov 19 '12 at 15:48gobbleoption ofmintedpackage works well on\inputminted[gobble=n,...]{language}{file}. It eliminates the first n characters, even if they are not whitespaces. – guillem Nov 20 '12 at 14:07\VerbatimInputfrom packagefancyvrbIt takes gobble into account – Nov 21 '12 at 07:05