]> git.lizzy.rs Git - micro.git/blobdiff - cmd/micro/command.go
Minor optimizations
[micro.git] / cmd / micro / command.go
index 712bb321f5a97e5daae93837f79e093ca802efa4..ebb7936551d82353829d5c5fdbed19fe892d9510 100644 (file)
@@ -1,24 +1,25 @@
 package main
 
 import (
-       "bytes"
        "fmt"
-       "io"
-       "io/ioutil"
        "os"
-       "os/exec"
-       "os/signal"
+       "path/filepath"
        "regexp"
+       "runtime"
+       "strconv"
        "strings"
 
-       "github.com/mitchellh/go-homedir"
+       humanize "github.com/dustin/go-humanize"
+       "github.com/zyedidia/micro/cmd/micro/shellwords"
 )
 
+// A Command contains a action (a function to call) as well as information about how to autocomplete the command
 type Command struct {
        action      func([]string)
        completions []Completion
 }
 
+// A StrCommand is similar to a command but keeps the name of the action
 type StrCommand struct {
        action      string
        completions []Completion
@@ -26,22 +27,37 @@ type StrCommand struct {
 
 var commands map[string]Command
 
-var commandActions = map[string]func([]string){
-       "Set":       Set,
-       "SetLocal":  SetLocal,
-       "Show":      Show,
-       "Run":       Run,
-       "Bind":      Bind,
-       "Quit":      Quit,
-       "Save":      Save,
-       "Replace":   Replace,
-       "VSplit":    VSplit,
-       "HSplit":    HSplit,
-       "Tab":       NewTab,
-       "Help":      Help,
-       "Eval":      Eval,
-       "ToggleLog": ToggleLog,
-       "Plugin":    PluginCmd,
+var commandActions map[string]func([]string)
+
+func init() {
+       commandActions = map[string]func([]string){
+               "Set":        Set,
+               "SetLocal":   SetLocal,
+               "Show":       Show,
+               "ShowKey":    ShowKey,
+               "Run":        Run,
+               "Bind":       Bind,
+               "Quit":       Quit,
+               "Save":       Save,
+               "Replace":    Replace,
+               "ReplaceAll": ReplaceAll,
+               "VSplit":     VSplit,
+               "HSplit":     HSplit,
+               "Tab":        NewTab,
+               "Help":       Help,
+               "Eval":       Eval,
+               "ToggleLog":  ToggleLog,
+               "Plugin":     PluginCmd,
+               "Reload":     Reload,
+               "Cd":         Cd,
+               "Pwd":        Pwd,
+               "Open":       Open,
+               "TabSwitch":  TabSwitch,
+               "Term":       Term,
+               "MemUsage":   MemUsage,
+               "Retab":      Retab,
+               "Raw":        Raw,
+       }
 }
 
 // InitCommands initializes the default commands
@@ -74,21 +90,54 @@ func MakeCommand(name, function string, completions ...Completion) {
 // DefaultCommands returns a map containing micro's default commands
 func DefaultCommands() map[string]StrCommand {
        return map[string]StrCommand{
-               "set":      {"Set", []Completion{OptionCompletion, NoCompletion}},
-               "setlocal": {"SetLocal", []Completion{OptionCompletion, NoCompletion}},
-               "show":     {"Show", []Completion{OptionCompletion, NoCompletion}},
-               "bind":     {"Bind", []Completion{NoCompletion}},
-               "run":      {"Run", []Completion{NoCompletion}},
-               "quit":     {"Quit", []Completion{NoCompletion}},
-               "save":     {"Save", []Completion{NoCompletion}},
-               "replace":  {"Replace", []Completion{NoCompletion}},
-               "vsplit":   {"VSplit", []Completion{FileCompletion, NoCompletion}},
-               "hsplit":   {"HSplit", []Completion{FileCompletion, NoCompletion}},
-               "tab":      {"Tab", []Completion{FileCompletion, NoCompletion}},
-               "help":     {"Help", []Completion{HelpCompletion, NoCompletion}},
-               "eval":     {"Eval", []Completion{NoCompletion}},
-               "log":      {"ToggleLog", []Completion{NoCompletion}},
-               "plugin":   {"Plugin", []Completion{PluginCmdCompletion, PluginNameCompletion}},
+               "set":        {"Set", []Completion{OptionCompletion, OptionValueCompletion}},
+               "setlocal":   {"SetLocal", []Completion{OptionCompletion, OptionValueCompletion}},
+               "show":       {"Show", []Completion{OptionCompletion, NoCompletion}},
+               "showkey":    {"ShowKey", []Completion{NoCompletion}},
+               "bind":       {"Bind", []Completion{NoCompletion}},
+               "run":        {"Run", []Completion{NoCompletion}},
+               "quit":       {"Quit", []Completion{NoCompletion}},
+               "save":       {"Save", []Completion{NoCompletion}},
+               "replace":    {"Replace", []Completion{NoCompletion}},
+               "replaceall": {"ReplaceAll", []Completion{NoCompletion}},
+               "vsplit":     {"VSplit", []Completion{FileCompletion, NoCompletion}},
+               "hsplit":     {"HSplit", []Completion{FileCompletion, NoCompletion}},
+               "tab":        {"Tab", []Completion{FileCompletion, NoCompletion}},
+               "help":       {"Help", []Completion{HelpCompletion, NoCompletion}},
+               "eval":       {"Eval", []Completion{NoCompletion}},
+               "log":        {"ToggleLog", []Completion{NoCompletion}},
+               "plugin":     {"Plugin", []Completion{PluginCmdCompletion, PluginNameCompletion}},
+               "reload":     {"Reload", []Completion{NoCompletion}},
+               "cd":         {"Cd", []Completion{FileCompletion}},
+               "pwd":        {"Pwd", []Completion{NoCompletion}},
+               "open":       {"Open", []Completion{FileCompletion}},
+               "tabswitch":  {"TabSwitch", []Completion{NoCompletion}},
+               "term":       {"Term", []Completion{NoCompletion}},
+               "memusage":   {"MemUsage", []Completion{NoCompletion}},
+               "retab":      {"Retab", []Completion{NoCompletion}},
+               "raw":        {"Raw", []Completion{NoCompletion}},
+       }
+}
+
+// CommandEditAction returns a bindable function that opens a prompt with
+// the given string and executes the command when the user presses
+// enter
+func CommandEditAction(prompt string) func(*View, bool) bool {
+       return func(v *View, usePlugin bool) bool {
+               input, canceled := messenger.Prompt("> ", prompt, "Command", CommandCompletion)
+               if !canceled {
+                       HandleCommand(input)
+               }
+               return false
+       }
+}
+
+// CommandAction returns a bindable function which executes the
+// given command
+func CommandAction(cmd string) func(*View, bool) bool {
+       return func(v *View, usePlugin bool) bool {
+               HandleCommand(cmd)
+               return false
        }
 }
 
@@ -121,15 +170,13 @@ func PluginCmd(args []string) {
                        removed := ""
                        for _, plugin := range args[1:] {
                                // check if the plugin exists.
-                               for _, lp := range loadedPlugins {
-                                       if lp == plugin {
-                                               UninstallPlugin(plugin)
-                                               removed += plugin + " "
-                                               continue
-                                       }
+                               if _, ok := loadedPlugins[plugin]; ok {
+                                       UninstallPlugin(plugin)
+                                       removed += plugin + " "
+                                       continue
                                }
                        }
-                       if !IsSpaces(removed) {
+                       if !IsSpaces([]byte(removed)) {
                                messenger.Message("Removed ", removed)
                        } else {
                                messenger.Error("The requested plugins do not exist")
@@ -162,22 +209,166 @@ func PluginCmd(args []string) {
                                        ToggleLog([]string{})
                                }
                        }
+               case "available":
+                       packages := GetAllPluginPackages()
+                       messenger.AddLog("Available Plugins:")
+                       for _, pkg := range packages {
+                               messenger.AddLog(pkg.Name)
+                       }
+                       if CurView().Type != vtLog {
+                               ToggleLog([]string{})
+                       }
                }
        } else {
                messenger.Error("Not enough arguments")
        }
 }
 
+// Retab changes all spaces to tabs or all tabs to spaces
+// depending on the user's settings
+func Retab(args []string) {
+       CurView().Retab(true)
+}
+
+// Raw opens a new raw view which displays the escape sequences micro
+// is receiving in real-time
+func Raw(args []string) {
+       buf := NewBufferFromString("", "Raw events")
+
+       view := NewView(buf)
+       view.Buf.Insert(view.Cursor.Loc, "Warning: Showing raw event escape codes\n")
+       view.Buf.Insert(view.Cursor.Loc, "Use CtrlQ to exit\n")
+       view.Type = vtRaw
+       tab := NewTabFromView(view)
+       tab.SetNum(len(tabs))
+       tabs = append(tabs, tab)
+       curTab = len(tabs) - 1
+       if len(tabs) == 2 {
+               for _, t := range tabs {
+                       for _, v := range t.views {
+                               v.ToggleTabbar()
+                       }
+               }
+       }
+}
+
+// TabSwitch switches to a given tab either by name or by number
+func TabSwitch(args []string) {
+       if len(args) > 0 {
+               num, err := strconv.Atoi(args[0])
+               if err != nil {
+                       // Check for tab with this name
+
+                       found := false
+                       for _, t := range tabs {
+                               v := t.views[t.CurView]
+                               if v.Buf.GetName() == args[0] {
+                                       curTab = v.TabNum
+                                       found = true
+                               }
+                       }
+                       if !found {
+                               messenger.Error("Could not find tab: ", err)
+                       }
+               } else {
+                       num--
+                       if num >= 0 && num < len(tabs) {
+                               curTab = num
+                       } else {
+                               messenger.Error("Invalid tab index")
+                       }
+               }
+       }
+}
+
+// Cd changes the current working directory
+func Cd(args []string) {
+       if len(args) > 0 {
+               path := ReplaceHome(args[0])
+               err := os.Chdir(path)
+               if err != nil {
+                       messenger.Error("Error with cd: ", err)
+                       return
+               }
+               wd, _ := os.Getwd()
+               for _, tab := range tabs {
+                       for _, view := range tab.views {
+                               if len(view.Buf.name) == 0 {
+                                       continue
+                               }
+
+                               view.Buf.Path, _ = MakeRelative(view.Buf.AbsPath, wd)
+                               if p, _ := filepath.Abs(view.Buf.Path); !strings.Contains(p, wd) {
+                                       view.Buf.Path = view.Buf.AbsPath
+                               }
+                       }
+               }
+       }
+}
+
+// MemUsage prints micro's memory usage
+// Alloc shows how many bytes are currently in use
+// Sys shows how many bytes have been requested from the operating system
+// NumGC shows how many times the GC has been run
+// Note that Go commonly reserves more memory from the OS than is currently in-use/required
+// Additionally, even if Go returns memory to the OS, the OS does not always claim it because
+// there may be plenty of memory to spare
+func MemUsage(args []string) {
+       var mem runtime.MemStats
+       runtime.ReadMemStats(&mem)
+
+       messenger.Message(fmt.Sprintf("Alloc: %v, Sys: %v, NumGC: %v", humanize.Bytes(mem.Alloc), humanize.Bytes(mem.Sys), mem.NumGC))
+}
+
+// Pwd prints the current working directory
+func Pwd(args []string) {
+       wd, err := os.Getwd()
+       if err != nil {
+               messenger.Message(err.Error())
+       } else {
+               messenger.Message(wd)
+       }
+}
+
+// Open opens a new buffer with a given filename
+func Open(args []string) {
+       if len(args) > 0 {
+               filename := args[0]
+               // the filename might or might not be quoted, so unquote first then join the strings.
+               args, err := shellwords.Split(filename)
+               if err != nil {
+                       messenger.Error("Error parsing args ", err)
+                       return
+               }
+               filename = strings.Join(args, " ")
+
+               CurView().Open(filename)
+       } else {
+               messenger.Error("No filename")
+       }
+}
+
+// ToggleLog toggles the log view
 func ToggleLog(args []string) {
        buffer := messenger.getBuffer()
        if CurView().Type != vtLog {
                CurView().HSplit(buffer)
                CurView().Type = vtLog
+               RedrawAll()
+               buffer.Cursor.Loc = buffer.Start()
+               CurView().Relocate()
+               buffer.Cursor.Loc = buffer.End()
+               CurView().Relocate()
        } else {
                CurView().Quit(true)
        }
 }
 
+// Reload reloads all files (syntax files, colorschemes...)
+func Reload(args []string) {
+       LoadAll()
+}
+
 // Help tries to open the given help page in a horizontal split
 func Help(args []string) {
        if len(args) < 1 {
@@ -197,19 +388,26 @@ func Help(args []string) {
 // If no file is given, it opens an empty buffer in a new split
 func VSplit(args []string) {
        if len(args) == 0 {
-               CurView().VSplit(NewBuffer([]byte{}, ""))
+               CurView().VSplit(NewBufferFromString("", ""))
        } else {
                filename := args[0]
-               home, _ := homedir.Dir()
-               filename = strings.Replace(filename, "~", home, 1)
-               file, err := ioutil.ReadFile(filename)
+               filename = ReplaceHome(filename)
+               file, err := os.Open(filename)
+               fileInfo, _ := os.Stat(filename)
+
+               if err == nil && fileInfo.IsDir() {
+                       messenger.Error(filename, " is a directory")
+                       return
+               }
+
+               defer file.Close()
 
                var buf *Buffer
                if err != nil {
                        // File does not exist -- create an empty buffer with that name
-                       buf = NewBuffer([]byte{}, filename)
+                       buf = NewBufferFromString("", filename)
                } else {
-                       buf = NewBuffer(file, filename)
+                       buf = NewBuffer(file, FSize(file), filename)
                }
                CurView().VSplit(buf)
        }
@@ -219,19 +417,26 @@ func VSplit(args []string) {
 // If no file is given, it opens an empty buffer in a new split
 func HSplit(args []string) {
        if len(args) == 0 {
-               CurView().HSplit(NewBuffer([]byte{}, ""))
+               CurView().HSplit(NewBufferFromString("", ""))
        } else {
                filename := args[0]
-               home, _ := homedir.Dir()
-               filename = strings.Replace(filename, "~", home, 1)
-               file, err := ioutil.ReadFile(filename)
+               filename = ReplaceHome(filename)
+               file, err := os.Open(filename)
+               fileInfo, _ := os.Stat(filename)
+
+               if err == nil && fileInfo.IsDir() {
+                       messenger.Error(filename, " is a directory")
+                       return
+               }
+
+               defer file.Close()
 
                var buf *Buffer
                if err != nil {
                        // File does not exist -- create an empty buffer with that name
-                       buf = NewBuffer([]byte{}, filename)
+                       buf = NewBufferFromString("", filename)
                } else {
-                       buf = NewBuffer(file, filename)
+                       buf = NewBuffer(file, FSize(file), filename)
                }
                CurView().HSplit(buf)
        }
@@ -255,14 +460,28 @@ func NewTab(args []string) {
                CurView().AddTab(true)
        } else {
                filename := args[0]
-               home, _ := homedir.Dir()
-               filename = strings.Replace(filename, "~", home, 1)
-               file, _ := ioutil.ReadFile(filename)
+               filename = ReplaceHome(filename)
+               file, err := os.Open(filename)
+               fileInfo, _ := os.Stat(filename)
+
+               if err == nil && fileInfo.IsDir() {
+                       messenger.Error(filename, " is a directory")
+                       return
+               }
+
+               defer file.Close()
 
-               tab := NewTabFromView(NewView(NewBuffer(file, filename)))
+               var buf *Buffer
+               if err != nil {
+                       buf = NewBufferFromString("", filename)
+               } else {
+                       buf = NewBuffer(file, FSize(file), filename)
+               }
+
+               tab := NewTabFromView(NewView(buf))
                tab.SetNum(len(tabs))
                tabs = append(tabs, tab)
-               curTab++
+               curTab = len(tabs) - 1
                if len(tabs) == 2 {
                        for _, t := range tabs {
                                for _, v := range t.views {
@@ -280,8 +499,8 @@ func Set(args []string) {
                return
        }
 
-       option := strings.TrimSpace(args[0])
-       value := strings.TrimSpace(args[1])
+       option := args[0]
+       value := args[1]
 
        SetOptionAndSettings(option, value)
 }
@@ -293,8 +512,8 @@ func SetLocal(args []string) {
                return
        }
 
-       option := strings.TrimSpace(args[0])
-       value := strings.TrimSpace(args[1])
+       option := args[0]
+       value := args[1]
 
        err := SetLocalOption(option, value, CurView())
        if err != nil {
@@ -319,6 +538,20 @@ func Show(args []string) {
        messenger.Message(option)
 }
 
+// ShowKey displays the action that a key is bound to
+func ShowKey(args []string) {
+       if len(args) < 1 {
+               messenger.Error("Please provide a key to show")
+               return
+       }
+
+       if action, ok := bindingsStr[args[0]]; ok {
+               messenger.Message(action)
+       } else {
+               messenger.Message(args[0], " has no binding")
+       }
+}
+
 // Bind creates a new keybinding
 func Bind(args []string) {
        if len(args) < 2 {
@@ -331,7 +564,7 @@ func Bind(args []string) {
 // Run runs a shell command in the background
 func Run(args []string) {
        // Run a shell command in the background (openTerm is false)
-       HandleShellCommand(JoinCommandArgs(args...), false, true)
+       HandleShellCommand(shellwords.Join(args...), false, true)
 }
 
 // Quit closes the main view
@@ -352,22 +585,38 @@ func Save(args []string) {
 
 // Replace runs search and replace
 func Replace(args []string) {
-       if len(args) < 2 {
+       if len(args) < 2 || len(args) > 4 {
                // We need to find both a search and replace expression
                messenger.Error("Invalid replace statement: " + strings.Join(args, " "))
                return
        }
 
-       var flags string
-       if len(args) == 3 {
-               // The user included some flags
-               flags = args[2]
+       all := false
+       noRegex := false
+
+       if len(args) > 2 {
+               for _, arg := range args[2:] {
+                       switch arg {
+                       case "-a":
+                               all = true
+                       case "-l":
+                               noRegex = true
+                       default:
+                               messenger.Error("Invalid flag: " + arg)
+                               return
+                       }
+               }
        }
 
        search := string(args[0])
+
+       if noRegex {
+               search = regexp.QuoteMeta(search)
+       }
+
        replace := string(args[1])
 
-       regex, err := regexp.Compile(search)
+       regex, err := regexp.Compile("(?m)" + search)
        if err != nil {
                // There was an error with the user's regex
                messenger.Error(err.Error())
@@ -377,7 +626,32 @@ func Replace(args []string) {
        view := CurView()
 
        found := 0
-       if strings.Contains(flags, "c") {
+       replaceAll := func() {
+               var deltas []Delta
+               deltaXOffset := Count(replace) - Count(search)
+               for i := 0; i < view.Buf.LinesNum(); i++ {
+                       matches := regex.FindAllIndex(view.Buf.lines[i].data, -1)
+                       str := string(view.Buf.lines[i].data)
+
+                       if matches != nil {
+                               xOffset := 0
+                               for _, m := range matches {
+                                       from := Loc{runePos(m[0], str) + xOffset, i}
+                                       to := Loc{runePos(m[1], str) + xOffset, i}
+
+                                       xOffset += deltaXOffset
+
+                                       deltas = append(deltas, Delta{replace, from, to})
+                                       found++
+                               }
+                       }
+               }
+               view.Buf.MultipleReplace(deltas)
+       }
+
+       if all {
+               replaceAll()
+       } else {
                for {
                        // The 'check' flag was used
                        Search(search, view, true)
@@ -385,11 +659,8 @@ func Replace(args []string) {
                                break
                        }
                        view.Relocate()
-                       if view.Buf.Settings["syntax"].(bool) {
-                               view.matches = Match(view)
-                       }
                        RedrawAll()
-                       choice, canceled := messenger.YesNoPrompt("Perform replacement? (y,n)")
+                       choice, canceled := messenger.LetterPrompt("Perform replacement? (y,n,a)", 'y', 'n', 'a')
                        if canceled {
                                if view.Cursor.HasSelection() {
                                        view.Cursor.Loc = view.Cursor.CurSelection[0]
@@ -397,41 +668,25 @@ func Replace(args []string) {
                                }
                                messenger.Reset()
                                break
-                       }
-                       if choice {
+                       } else if choice == 'a' {
+                               if view.Cursor.HasSelection() {
+                                       view.Cursor.Loc = view.Cursor.CurSelection[0]
+                                       view.Cursor.ResetSelection()
+                               }
+                               messenger.Reset()
+                               replaceAll()
+                               break
+                       } else if choice == 'y' {
                                view.Cursor.DeleteSelection()
                                view.Buf.Insert(view.Cursor.Loc, replace)
                                view.Cursor.ResetSelection()
                                messenger.Reset()
                                found++
-                       } else {
-                               if view.Cursor.HasSelection() {
-                                       searchStart = ToCharPos(view.Cursor.CurSelection[1], view.Buf)
-                               } else {
-                                       searchStart = ToCharPos(view.Cursor.Loc, view.Buf)
-                               }
-                               continue
                        }
-               }
-       } else {
-               matches := regex.FindAllStringIndex(view.Buf.String(), -1)
-               if matches != nil && len(matches) > 0 {
-                       adjust := 0
-                       prevMatch := matches[0]
-                       from := FromCharPos(prevMatch[0], view.Buf)
-                       to := from.Move(Count(search), view.Buf)
-                       adjust += Count(replace) - Count(search)
-                       view.Buf.Replace(from, to, replace)
-                       if len(matches) > 1 {
-                               for _, match := range matches[1:] {
-                                       found++
-                                       from = from.Move(match[0]-prevMatch[0]+adjust, view.Buf)
-                                       to := from.Move(Count(search), view.Buf)
-                                       // TermMessage(match[0], " ", prevMatch[0], " ", adjust, "\n", from, " ", to)
-                                       view.Buf.Replace(from, to, replace)
-                                       prevMatch = match
-                                       // adjust += Count(replace) - Count(search)
-                               }
+                       if view.Cursor.HasSelection() {
+                               searchStart = view.Cursor.CurSelection[1]
+                       } else {
+                               searchStart = view.Cursor.Loc
                        }
                }
        }
@@ -446,93 +701,33 @@ func Replace(args []string) {
        }
 }
 
-// RunShellCommand executes a shell command and returns the output/error
-func RunShellCommand(input string) (string, error) {
-       inputCmd := SplitCommandArgs(input)[0]
-       args := SplitCommandArgs(input)[1:]
-
-       cmd := exec.Command(inputCmd, args...)
-       outputBytes := &bytes.Buffer{}
-       cmd.Stdout = outputBytes
-       cmd.Stderr = outputBytes
-       cmd.Start()
-       err := cmd.Wait() // wait for command to finish
-       outstring := outputBytes.String()
-       return outstring, err
-}
-
-// HandleShellCommand runs the shell command
-// The openTerm argument specifies whether a terminal should be opened (for viewing output
-// or interacting with stdin)
-func HandleShellCommand(input string, openTerm bool, waitToFinish bool) string {
-       inputCmd := SplitCommandArgs(input)[0]
-       if !openTerm {
-               // Simply run the command in the background and notify the user when it's done
-               messenger.Message("Running...")
-               go func() {
-                       output, err := RunShellCommand(input)
-                       totalLines := strings.Split(output, "\n")
-
-                       if len(totalLines) < 3 {
-                               if err == nil {
-                                       messenger.Message(inputCmd, " exited without error")
-                               } else {
-                                       messenger.Message(inputCmd, " exited with error: ", err, ": ", output)
-                               }
-                       } else {
-                               messenger.Message(output)
-                       }
-                       // We have to make sure to redraw
-                       RedrawAll()
-               }()
-       } else {
-               // Shut down the screen because we're going to interact directly with the shell
-               screen.Fini()
-               screen = nil
-
-               args := SplitCommandArgs(input)[1:]
-
-               // Set up everything for the command
-               var outputBuf bytes.Buffer
-               cmd := exec.Command(inputCmd, args...)
-               cmd.Stdin = os.Stdin
-               cmd.Stdout = io.MultiWriter(os.Stdout, &outputBuf)
-               cmd.Stderr = os.Stderr
-
-               // This is a trap for Ctrl-C so that it doesn't kill micro
-               // Instead we trap Ctrl-C to kill the program we're running
-               c := make(chan os.Signal, 1)
-               signal.Notify(c, os.Interrupt)
-               go func() {
-                       for range c {
-                               cmd.Process.Kill()
-                       }
-               }()
-
-               cmd.Start()
-               err := cmd.Wait()
-
-               output := outputBuf.String()
-               if err != nil {
-                       output = err.Error()
-               }
-
-               if waitToFinish {
-                       // This is just so we don't return right away and let the user press enter to return
-                       TermMessage("")
-               }
-
-               // Start the screen back up
-               InitScreen()
+// ReplaceAll replaces search term all at once
+func ReplaceAll(args []string) {
+       // aliased to Replace command
+       Replace(append(args, "-a"))
+}
 
-               return output
+// Term opens a terminal in the current view
+func Term(args []string) {
+       var err error
+       if len(args) == 0 {
+               err = CurView().StartTerminal([]string{os.Getenv("SHELL"), "-i"}, true, false, "")
+       } else {
+               err = CurView().StartTerminal(args, true, false, "")
+       }
+       if err != nil {
+               messenger.Error(err)
        }
-       return ""
 }
 
 // HandleCommand handles input from the user
 func HandleCommand(input string) {
-       args := SplitCommandArgs(input)
+       args, err := shellwords.Split(input)
+       if err != nil {
+               messenger.Error("Error parsing args ", err)
+               return
+       }
+
        inputCmd := args[0]
 
        if _, ok := commands[inputCmd]; !ok {