├── README.md ├── doc ├── README.md └── edit-in-vim.gif ├── edit-text-in-vim.applescript └── open-file-in-vim.applescript /README.md: -------------------------------------------------------------------------------- 1 | # vim-macos-scripts 2 | Applescript files to create Automator actions invoking VIM in Terminal.app 3 | 4 | ## What is this? 5 | These Applescript files can be used with the Shortcuts or Automator application on MacOSX. They allow you to create Quick Actions to edit text files or text snippets in the VIM command-line editor. 6 | 7 | ![edit-in-vim-example](/doc/edit-in-vim.gif?raw=true "Edit in VIM") 8 | 9 | ## How to use (Shortcuts) 10 | Installation should work similar to below. Add a script action. 11 | 12 | #### edit-text-in-vim 13 | - Create a new Shortcut 14 | - Add an AppleScript Action 15 | - Copy-Paste the text of `edit-text-in-vim.applescript` to the Applescript action 16 | - Add a "Stop and Return script result" Action 17 | - In the Settings of the Shortcut: 18 | - Set available in "Services" 19 | - Add a keyboard shortcut if wanted 20 | 21 | #### open-file-in-vim 22 | - Create a new Shortcut 23 | - Add an AppleScript Action 24 | - Copy-Paste the text of `open-file-in-vim.applescript` to the Applescript action 25 | - In the Settings of the Shortcut: 26 | - Set available in "Services" and "Finder" 27 | - Add a keyboard shortcut if wanted 28 | 29 | ## How to use (Automator) 30 | #### Prepare Terminal.app 31 | - Open Terminal.app 32 | - Press `Command-,` to open its settings 33 | - Under "Profile" add a new profile called `vim` using the `+`button. 34 | - This profile will be used when a new VIM window is opened, adapt it to your liking 35 | - Under "Profile"->"Shell" set "When the shell exits" to "Close window" 36 | 37 | #### edit-text-in-vim 38 | - Open Automator 39 | - Create a new "Contextual Workflow" (Quick Action) 40 | - In the top bar 41 | - Allow the action to receive Plain Text 42 | - Set an fitting icon 43 | - Set the "replaces selected text" checkbox 44 | - Add an Applescript action to the flow (search "Applescript" in the search bar) 45 | - Copy-Paste the text of `edit-text-in-vim.applescript` to the Applescript action 46 | - Save the Automator action as "Edit in VIM" 47 | 48 | Now the action will automatically appear when you right-click selected text. Edit the text in VIM, save it, quit VIM and the text will automatically be pasted back to the original application. 49 | 50 | _Hint: In the MacOS settings under "Keyboard" you can assign a keyboard shortcut to this action._ 51 | 52 | #### open-file-in-vim 53 | - Open Automator 54 | - Create a new "Contextual Workflow" (Quick Action) 55 | - In the top bar 56 | - Allow the action to receive Files and Folders 57 | - Set an fitting icon 58 | - Add an Applescript action to the flow (search "Applescript" in the search bar) 59 | - Copy-Paste the text of `open-file-in-vim.applescript` to the Applescript action 60 | - Save the Automator action as "Open in VIM" 61 | 62 | Now the action will automatically appear in Finder when you select or right-click a file. Execute it to edit the file directly in VIM. The parent folder of the file will be set as working directory. If multiple files are selected they will open in VIM tabs. If a single `.vim` file is opened VIM will be started with the `-S` parameter to open the file as a session file. 63 | 64 | #### open-file-in-vim as Application 65 | You can also use `open-file-in-vim.applescript` to create an Automator "Application" instead of a "Quick Action". 66 | 67 | - Open Automator 68 | - Create a new "Application" 69 | - Add an Applescript action to the flow (search "Applescript" in the search bar) 70 | - Copy-Paste the text of `open-file-in-vim.applescript` to the Applescript action 71 | - Save the Application as "VIM" in either /Applications or /User/name/Applications 72 | 73 | You can set the application to always open files of a specific type through their Finder information panel. You can also use it to open a new instance of VIM without opening a file. 74 | 75 | #### Bonus: current file in Terminal top bar 76 | As a bonus, you can see the currently edited file in the top bar of Terminal if you add this to your `.vimrc`: 77 | 78 | ``` 79 | set t_ts=^[]6; 80 | set t_fs=^G 81 | set title 82 | set titlestring=%{\"file://\".hostname().expand(\"%:p\")} 83 | ``` 84 | 85 | - To get the `^[` character press `Ctrl-v` then `Esc` when in insert mode. 86 | - To get the `^G` character, press `Ctrl-v` then `Ctrl-G` when in insert mode. 87 | 88 | The rest are "normal" characters. 89 | 90 | _Hint: If the above doesn't work for you try changing the last line to_ 91 | 92 | `auto BufEnter * let &titlestring = "file://" . substitute(hostname().expand("%:p"), " ", "+", "")` 93 | 94 | ## Alternative for tmux 95 | If you are using tmux and its running anyway you might be interested in these shell scripts to use instead of opening Terminal windows. 96 | 97 | #### edit-text-in-vim 98 | - Add a new Shortcut 99 | - Add a Script action 100 | - Set the script: 101 | ``` 102 | TMPFILE=$(mktemp) 103 | cat>$TMPFILE 104 | osascript -e 'tell application "Terminal" to activate' 105 | tmux new-window "vim '$TMPFILE';tmux wait-for -S edit_return" 106 | tmux wait edit_return 107 | cat $TMPFILE 108 | ``` 109 | - Set "Input as stdin" 110 | - Add a "Stop and return script value" 111 | - In the Settings of the Shortcut: 112 | - Set available in "Services" 113 | - Add a keyboard shortcut if wanted 114 | 115 | #### open-file-in-vim 116 | - Add a new Shortcut 117 | - Add a Script action 118 | - Set the script: 119 | ``` 120 | osascript -e 'tell application "Terminal" to activate' 121 | FOLDER=$(dirname $1) 122 | tmux new-window "cd $FOLDER; vim '$1'" 123 | ``` 124 | - Set "Input as arguments" 125 | - In the Settings of the Shortcut: 126 | - Set available in "Services" and "Finder" 127 | - Add a keyboard shortcut if wanted 128 | -------------------------------------------------------------------------------- /doc/README.md: -------------------------------------------------------------------------------- 1 | This folder is for documentation and images. 2 | -------------------------------------------------------------------------------- /doc/edit-in-vim.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/normen/vim-macos-scripts/15a2300e8fd172da1b5ffa15a3e5f69f69907339/doc/edit-in-vim.gif -------------------------------------------------------------------------------- /edit-text-in-vim.applescript: -------------------------------------------------------------------------------- 1 | -- opens VIM with temp file created from input, returns changed file contents 2 | -- Released under MIT license by Normen Hansen 3 | 4 | on run {input, parameters} 5 | -- Save calling application name to bring it back to front 6 | -- (disabled for now as bringing back to front doesn't work) 7 | --tell application "System Events" 8 | -- set originalAppName to name of first process whose frontmost is true 9 | --end tell 10 | 11 | -- Create temp file with text content for VIM to open. 12 | -- We're using "/tmp/" but it should be: 13 | -- (POSIX path of (path to temporary items folder from user domain)) 14 | -- That doesn't work though, vim has no access to files there. 15 | -- Maybe Terminal.app has no access to that folder. 16 | set posixtmpfile to "/tmp/" & "vim_" & text 3 thru -1 of ((random number) as string) 17 | try 18 | set fhandle to open for access posixtmpfile with write permission 19 | write input to fhandle as «class utf8» 20 | close access fhandle 21 | on error 22 | try 23 | close access posixtmpfile 24 | end try 25 | end try 26 | 27 | set command to {"vim "} 28 | set end of command to (posixtmpfile as string) 29 | set end of command to ";exit" 30 | -- compile command 31 | set command to command as string 32 | set myTab to null 33 | tell application "Terminal" 34 | -- check if Terminal is already running 35 | if it is not running then 36 | set myTab to (do script command in window 1) 37 | else 38 | set myTab to (do script command) 39 | end if 40 | -- get our own window 41 | set myWindow to first window of (every window whose tabs contains myTab) 42 | -- set Terminal.app window settings and window size and get to foreground 43 | set current settings of myWindow to settings set "vim" 44 | set size of myWindow to {910, 800} 45 | activate 46 | -- wait for command to finish 47 | try 48 | repeat while (exists myWindow) and (myTab is busy) 49 | delay 0.1 50 | end repeat 51 | end try 52 | end tell 53 | -- read file contents and return 54 | set myString to (read posixtmpfile as «class utf8») 55 | -- delete file posixtmpfile 56 | do shell script "/bin/rm " & quoted form of posixtmpfile 57 | -- TODO: bring original app to front again 58 | -- Can't do it directly as the app is blocked by calling this applescript. 59 | -- Doing "tell application originalAppName to activate" causes a deadlock. 60 | -- Calling osascript as a background process doesn't work either. 61 | -- I guess it would be in order if Apple added that behavior 62 | -- automatically when selecting "replace text". 63 | return myString 64 | end run 65 | -------------------------------------------------------------------------------- /open-file-in-vim.applescript: -------------------------------------------------------------------------------- 1 | -- opens VIM with file, file list, .vim session file or standalone 2 | -- sets current folder to parent folder of first file 3 | -- Released under MIT license by Normen Hansen 4 | 5 | on run {input, parameters} 6 | set command to {} 7 | -- check if theres input files at all 8 | if input is null or input is {} or ((item 1 in input) as string) is "" then 9 | -- no files, open vim without parameters 10 | set end of command to "vim;exit" 11 | else 12 | set firstFile to (item 1 in input) as string 13 | -- get parent folder thru finder, TODO: if folder is given use that 14 | tell application "Finder" to set pathFolderParent to quoted form of (POSIX path of ((folder of item firstFile) as string)) 15 | set end of command to "cd " 16 | set end of command to (pathFolderParent as string) 17 | set end of command to ";vim" 18 | -- support .vim session files 19 | if firstFile ends with ".vim" then 20 | set end of command to " -S " 21 | else 22 | -- use tabs 23 | set end of command to " -p " 24 | --set end of command to " " 25 | end if 26 | -- compile all file paths 27 | set fileList to {} 28 | repeat with i from 1 to count input 29 | set end of fileList to quoted form of (POSIX path of (item i of input as string)) & space 30 | end repeat 31 | set end of command to (fileList as string) 32 | set end of command to ";exit" 33 | end if 34 | -- compile command 35 | set command to command as string 36 | set myTab to null 37 | tell application "Terminal" 38 | -- check if Terminal is already running 39 | if it is not running then 40 | set myTab to (do script command in window 1) 41 | else 42 | set myTab to (do script command) 43 | end if 44 | -- get our own window 45 | set myWindow to first window of (every window whose tabs contains myTab) 46 | -- set Terminal.app window settings and window size and get to foreground 47 | set current settings of myWindow to settings set "vim" 48 | set size of myWindow to {910, 800} 49 | activate 50 | end tell 51 | return input 52 | end run 53 | --------------------------------------------------------------------------------