             let lastLine = buffer.substring(by: range.end.line, from: 0, to: range.end.column)
             text.append(lastLine)
         }
-        return text
+        
+        var minSpaceCount = Int.max
+        var array = text.components(separatedBy: "\n")
+        array.forEach { text in
+            let count = text.beginingSpaceCount()
+            if minSpaceCount > count {
+                minSpaceCount = count
+            }
+        }
+        
+        array = array.map { text in
+            text.trimming(by: minSpaceCount)
+        }
+        
+        return array.joined(separator: "\n")
+    }
+}
+
+fileprivate extension String {
+    
+    func beginingSpaceCount() -> Int {
+        var count = 0
+        for character in characters {
+            guard String(character) == " " else {
+                break
+            }
+            count += 1
+        }
+        return count
+    }
+    
+    func trimming(by count: Int) -> String {
+        return substring(from: count)
+    }
+    
+    func substring(from: Int, to: Int? = nil) -> String {
+        let lineLetters = characters.map { String($0) }
+        let lineLettersSlice = lineLetters[from..<(to ?? characters.count)]
+        return lineLettersSlice.joined()
     }
 }
 
 fileprivate extension XCSourceTextBuffer {
     
-    func substring(by index: Int, from: Int, to: Int?) -> String {
+    func substring(by index: Int, from: Int, to: Int? = nil) -> String {
         let line = lines[index] as! String
-        let lineLetters = line.characters.map { String($0) }
-        let lineLettersSlice = lineLetters[from..<(to ?? line.characters.count)]
-        return lineLettersSlice.joined()
+        return line.substring(from: from, to: to)
     }
 }

                continue
            }
            
            let firstLine = buffer.substring(by: range.start.line, from: range.start.column, to: nil)
            text.append(firstLine)
            
            if range.end.line - range.start.line > 1 {

            pasteboardString += selectedText
            pasteboardString += "\n"
            pasteboardString += "```"

            NSPasteboard.general().declareTypes([NSPasteboardTypeString], owner: nil)
            NSPasteboard.general().setString(pasteboardString, forType: NSPasteboardTypeString)
        }
